Utils.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/dataaccess/utils/Utils.h
22 
23  \brief Utility functions for the data access module.
24 */
25 
26 #ifndef __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
27 #define __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "../../geometry/Enums.h"
31 #include "../../srs/Config.h"
32 #include "../datasource/DataSource.h"
33 #include "../query/Expression.h"
34 #include "../query/Fields.h"
35 #include "../query/Select.h"
36 
37 // Boost
38 #include <boost/ptr_container/ptr_vector.hpp>
39 
40 // STL
41 #include <memory>
42 #include <string>
43 #include <vector>
44 
45 namespace te
46 {
47 // Forward declaration
48  namespace core { class URI; }
49  namespace dt { class Property; }
50  namespace gm
51  {
52  class Envelope;
53  class Geometry;
54  class GeometryProperty;
55  }
56 
57  namespace rst { class RasterProperty; }
58 
59  namespace da
60  {
61 // Forward declaration
62  class DataSet;
63  class DataSetAdapter;
64  class DataSetType;
65  class DataSetTypeConverter;
66  class DataSourceCatalogLoader;
67  class DataSourceTransactor;
68  class ObjectId;
69  class ObjectIdSet;
70 
71  TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId);
72 
73 // TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType* dataset, te::da::DataSource* datasource);
74 
75  TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType* dataset, const std::string& datasourceId);
76 
77  /*!
78  \return The data extent considering the informed property. The caller will take the ownership of the returned box.
79 
80  \exception Exception It throws an exception if the extent can not be determined.
81  */
82  TEDATAACCESSEXPORT te::gm::Envelope* GetExtent(const std::string& datasetName,
83  const std::string& propertyName,
84  const std::string& datasourceId);
85 
86  TEDATAACCESSEXPORT void GetDataSetNames(std::vector<std::string>& datasetNames, const std::string& datasourceId);
87 
88  TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category);
89 
90  TEDATAACCESSEXPORT bool HasDataSet(const std::string& datasourceId);
91 
92  TEDATAACCESSEXPORT DataSet* GetDataSet(const std::string& name, const std::string& datasourceId);
93 
94  TEDATAACCESSEXPORT DataSetType* GetDataSetType(const std::string& name, const std::string& datasourceId);
95 
96  /*!
97  \brief Search for a data source with the informed id in the DataSourceManager.
98 
99  If the data source is not found in that manager it will try to get some information
100  in the DataSourceInfoManager and will create a new data source instance,
101  storing a reference to the new data source in the manager.
102 
103  \param datasourceId The data source to look for.
104  \param opened Automatically open the data source.
105 
106  \return A data source with the informed id.
107 
108  \exception Exception It throws an exception if the informed data source could not be retrieved.
109  */
110  TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string& datasourceId, const bool opened = true);
111 
112  /*!
113  \brief Returns an empty ObjectIdSet, with the definitions of fields that compose it.
114 
115  \param type[in] A pointer to a valid data set type. Do not pass null.
116 
117  \param set[out] The ObjectIdSet containing only the the fields definitions. This pointer will be instantiated inside the method.
118 
119  \note The caller WILL TAKE the ownership of \a set.
120  */
121  TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType* type, ObjectIdSet*& set);
122 
123  /*
124  \brief It returns the property names used to generate the object ids.
125 
126  This method first looks for the primary key properties defined on data set type.
127  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
128 
129  \param type A pointer to a valid data set type. Do not pass null.
130  \param pnames The vector that will be filled with the property names.
131  */
132  TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType* type, std::vector<std::string>& pnames);
133 
134  /*
135  \brief It returns the name of the base dataset of a linked dataset
136  and how many of it's properties are a part of the objectId
137 
138  \param type A pointer to a valid data set type. Do not pass null.
139  \param pnames The pair that will be filled with the dataset name and the number of properties.
140  */
141  TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType* type, std::pair<std::string, int>& dsProps);
142 
143  /*
144  \brief It returns the property positions used to generate the object ids.
145 
146  This method first looks for the primary key properties defined on data set type.
147  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
148 
149  \param type A pointer to a valid data set type. Do not pass null.
150  \param pnames The vector that will be filled with the property names.
151  */
152  TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType* type, std::vector<std::size_t>& ppos);
153 
154  /*
155  \function getBasePkey
156  \brief Function used to acquire the string that represents the objectId of the base dataset.
157 
158  \param oid The objectId as returned from the dataset;
159  \param dsProps A pair that indicates the name of the base dataset and how many key properties it's got
160 
161  \note Will return the whole objectId as a string unless the dataset of interest is based on a linked layer,
162  otherwise it returns a string that represents the objectId of the base Dataset.
163  \note It will not take ownership of the given objectId pointer
164  */
165  TEDATAACCESSEXPORT std::string getBasePkey(te::da::ObjectId* oid, std::pair<std::string, int>& dsProps);
166 
167  /*
168  \brief It generates the set of object ids for every element of the given dataset.
169 
170  This method tries to use the primary key properties defined on data set type to generate the object ids.
171  If they do not exist it tries to use unique keys. If they do not exist then it uses all the properties.
172 
173  \param dataset A pointer to a valid data set. Do not pass null.
174  \param type A pointer to a data set type that describes the given dataset. Do not pass null.
175 
176  \return The object id set generated from the given dataset.
177  */
178  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const DataSetType* type);
179 
180  /*
181  \brief It generates the set of object ids for every element of the given dataset using a set of attributes.
182 
183  \param dataset A pointer to a valid data set.
184  \param names A non empty vector of property names to be used to identify the elements.
185 
186  \return The object id set generated from the given dataset.
187 
188  \exception Exception It throws an exception if a property was not found in data set
189  */
190  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const std::vector<std::string>& names);
191 
192  /*
193  \brief It generates an object id for the current element of the given dataset using the informed set of attributes.
194 
195  \param dataset A pointer to a valid data set.
196  \param names A non empty vector of property names to be used to identify the element.
197 
198  \return The object id generated for the current element of the given dataset.
199  */
200  TEDATAACCESSEXPORT ObjectId* GenerateOID(DataSet* dataset, const std::vector<std::string>& names);
201 
202  /*!
203  \brief It returns the first dataset spatial property or NULL if none is found.
204 
205  \param dataset The dataset to search for a spatial property.
206 
207  \return The first dataset spatial property or NULL if none is found.
208  */
210 
211  TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype);
212 
213  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet* dataset, const std::string& name);
214 
215  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSetType* dt, const std::string& name);
216 
218 
220 
222 
223  //TEDATAACCESSEXPORT te::da::DataSetType* CreateDataSetType(const te::da::DataSet* dataset);
224 
225  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType* const dt,
226  std::vector<std::string>& pnames,
227  std::vector<int>& ptypes);
228 
229  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSet* const dataset,
230  std::vector<std::string>& pnames,
231  std::vector<int>& ptypes);
232 
233  /*!
234  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
235 
236  This function will create the dataset schema definition and will
237  save all the dataset data.
238 
239  \param t The data source transactor.
240  \param dt The source dataset definition.
241  \param d The source dataset data.
242  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
243 
244  \pre All parameters must be valid pointers.
245 
246  \post It is the caller responsability to release the dataset 'd' pointer.
247 
248  \exception Exception It throws an exception if the dataset can not be created.
249 
250  \note DataSetPersistence will start reading the dataset 'd' in the
251  current position. So, keep in mind that it is the caller responsability
252  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
253  */
254  TEDATAACCESSEXPORT void Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit = 0);
255 
256  /*!
257  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
258 
259  This method will create the dataset schema definition and will
260  save all the dataset data.
261 
262  \param dt The source dataset definition.
263  \param d The source dataset data.
264  \param options A list of optional modifiers. It is driver specific.
265  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
266 
267  \pre All parameters must be valid pointers.
268 
269  \exception Exception It throws an exception if the dataset can not be created.
270 
271  \note It is the caller responsability to release the dataset 'd' pointer.
272 
273  \note DataSetPersistence will start reading the dataset 'd' in the
274  current position. So, keep in mind that it is the caller responsability
275  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
276  */
277  TEDATAACCESSEXPORT void Create(DataSource* ds,
278  DataSetType* dt,
279  DataSet* d,
280  const std::map<std::string, std::string>& options,
281  std::size_t limit = 0);
282 
283  TEDATAACCESSEXPORT DataSetAdapter* CreateAdapter(DataSet* ds, DataSetTypeConverter* converter, bool isOwner = false);
284 
285  TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter* converter, const int& inputSRID, const int& outputSRID = TE_UNKNOWN_SRS);
286 
287  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType* dt);
288 
289  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSet* dataset);
290 
291  TEDATAACCESSEXPORT std::vector<int> GetPropertyDataTypes(const te::da::DataSet* dataset);
292 
293  TEDATAACCESSEXPORT std::auto_ptr<Fields> BuildFields(const std::vector<std::string>& properties);
294 
295  TEDATAACCESSEXPORT std::auto_ptr<Expression> BuildSpatialOp(Expression* e1,
296  Expression* e2,
298 
299  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname);
300 
301  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
302  const std::string& propertyName);
303 
304  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
305  const std::vector<std::string>& properties);
306 
307  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
308  const std::vector<std::string>& properties,
309  const std::string& geometryProperty,
310  const te::gm::Envelope* e,
311  int srid,
313 
314  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
315  const std::vector<std::string>& properties,
316  const std::string& geometryProperty,
317  te::gm::Geometry* g,
319 
320  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
321  const std::vector<std::string>& properties,
322  const ObjectIdSet* oids);
323 
324 
325  TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName);
326 
327  /*!
328  \brief It checks if the name is not valid as the existence of invalid characters, reserved words, and others.
329 
330  \param name Name that will be checked.
331  \param invalidChar Records the invalid character.
332 
333  \return If the name is valid.
334  */
335  TEDATAACCESSEXPORT bool IsValidName(const std::string& name, std::string& invalidChar);
336 
337  /*!
338  \brief It checks if the datasettype has a linked table.
339 
340  \param type The Datasettype.
341 
342  \return True if has linked table.
343  */
345 
346  /*!
347  \brief It hide columns of a DataSet using DataSetAdapter.
348 
349  \param ds The DataSet that will have hidden columns.
350  \param dst The DataSetType used to hide columns.
351  \param columns names to be hidden.
352 
353  \return The DataSet result. The caller will take the ownership of the returned DataSet.
354  */
355  TEDATAACCESSEXPORT std::auto_ptr<te::da::DataSet> HideColumns(te::da::DataSet* ds, te::da::DataSetType* dst, const std::vector<std::string>& columns);
356 
357  /*!
358  \brief It gets the summarized value.
359 
360  \param values The input values.
361  \param summary The summary mode. It can be: "MIN", "MAX", "SUM", "AVERAGE", "MEDIAN", "STDDEV" or "VARIANCE"
362 
363  \return The summarized value.
364  */
365  TEDATAACCESSEXPORT double GetSummarizedValue(std::vector<double>& values, const std::string& summary);
366 
367  /*!
368  \brief It gets the summarized value.
369 
370  \param values The input values.
371  \param summary The summary mode. It can be: "MIN" or "MAX"
372 
373  \return The summarized value.
374  */
375  TEDATAACCESSEXPORT std::string GetSummarizedValue(const std::vector<std::string>& values, const std::string& sumary);
376 
377  /*!
378  \brief It gets the round value.
379 
380  \param value The input value.
381 
382  \return The rounded value.
383  */
384  TEDATAACCESSEXPORT double Round(const double& value, const size_t& precision);
385 
386  /*!
387  \brief It gets the value as double.
388 
389  \param ds The input dataset.
390  \param pos The column position.
391 
392  \return The double value. Default value is 0.
393  */
394  TEDATAACCESSEXPORT double GetValueAsDouble(const te::da::DataSet* ds, const size_t pos);
395 
396  } // end namespace da
397 } // end namespace te
398 
399 #endif // __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
400 
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Geometric property.
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1438
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:127
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
TEDATAACCESSEXPORT double GetValueAsDouble(const te::da::DataSet *ds, const size_t pos)
It gets the value as double.
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
It models a property definition.
Definition: Property.h:59
Raster property.
TEDATAACCESSEXPORT bool HasLinkedTable(te::da::DataSetType *type)
It checks if the datasettype has a linked table.
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:41
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
TEDATAACCESSEXPORT bool IsValidName(const std::string &name, std::string &invalidChar)
It checks if the name is not valid as the existence of invalid characters, reserved words...
URI C++ Library.
TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType *type, std::pair< std::string, int > &dsProps)
TEDATAACCESSEXPORT std::auto_ptr< te::da::DataSet > HideColumns(te::da::DataSet *ds, te::da::DataSetType *dst, const std::vector< std::string > &columns)
It hide columns of a DataSet using DataSetAdapter.
TEDATAACCESSEXPORT double Round(const double &value, const size_t &precision)
It gets the round value.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:74
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType *type, std::vector< std::size_t > &ppos)
TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter *converter, const int &inputSRID, const int &outputSRID=TE_UNKNOWN_SRS)
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:97
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
TEDATAACCESSEXPORT std::string getBasePkey(te::da::ObjectId *oid, std::pair< std::string, int > &dsProps)
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
TEDATAACCESSEXPORT double GetSummarizedValue(std::vector< double > &values, const std::string &summary)
It gets the summarized value.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType *dataset, const std::string &datasourceId)