All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../datasource/DataSource.h"
32 #include "../query/Expression.h"
33 #include "../query/Fields.h"
34 #include "../query/Select.h"
35 
36 // Boost
37 #include <boost/ptr_container/ptr_vector.hpp>
38 
39 // STL
40 #include <memory>
41 #include <string>
42 #include <vector>
43 
44 namespace te
45 {
46 // Forward declaration
47  namespace dt { class Property; }
48 
49  namespace gm
50  {
51  class Envelope;
52  class Geometry;
53  class GeometryProperty;
54  }
55 
56  namespace rst { class RasterProperty; }
57 
58  namespace da
59  {
60 // Forward declaration
61  class DataSet;
62  class DataSetAdapter;
63  class DataSetType;
64  class DataSetTypeConverter;
65  class DataSourceCatalogLoader;
66  class DataSourceTransactor;
67  class ObjectId;
68  class ObjectIdSet;
69 
70  TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId);
71 
72 // TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType* dataset, te::da::DataSource* datasource);
73 
74  TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType* dataset, const std::string& datasourceId);
75 
76  /*!
77  \return The data extent considering the informed property. The caller will take the ownership of the returned box.
78 
79  \exception Exception It throws an exception if the extent can not be determined.
80  */
81  TEDATAACCESSEXPORT te::gm::Envelope* GetExtent(const std::string& datasetName,
82  const std::string& propertyName,
83  const std::string& datasourceId);
84 
85  TEDATAACCESSEXPORT void GetDataSetNames(std::vector<std::string>& datasetNames, const std::string& datasourceId);
86 
87  TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category);
88 
89  TEDATAACCESSEXPORT bool HasDataSet(const std::string& datasourceId);
90 
91  TEDATAACCESSEXPORT DataSet* GetDataSet(const std::string& name, const std::string& datasourceId);
92 
93  TEDATAACCESSEXPORT DataSetType* GetDataSetType(const std::string& name, const std::string& datasourceId);
94 
95  /*!
96  \brief Search for a data source with the informed id in the DataSourceManager.
97 
98  If the data source is not found in that manager it will try to get some information
99  in the DataSourceInfoManager and will create a new data source instance,
100  storing a reference to the new data source in the manager.
101 
102  \param datasourceId The data source to look for.
103  \param opened Automatically open the data source.
104 
105  \return A data source with the informed id.
106 
107  \exception Exception It throws an exception if the informed data source could not be retrieved.
108  */
109  TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string& datasourceId, const bool opened = true);
110 
111  /*!
112  \brief Returns an empty ObjectIdSet, with the definitions of fields that compose it.
113 
114  \param type[in] A pointer to a valid data set type. Do not pass null.
115 
116  \param set[out] The ObjectIdSet containing only the the fields definitions. This pointer will be instantiated inside the method.
117 
118  \note The caller WILL TAKE the ownership of \a set.
119  */
120  TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType* type, ObjectIdSet*& set);
121 
122  /*
123  \brief It returns the property names used to generate the object ids.
124 
125  This method first looks for the primary key properties defined on data set type.
126  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
127 
128  \param type A pointer to a valid data set type. Do not pass null.
129  \param pnames The vector that will be filled with the property names.
130  */
131  TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType* type, std::vector<std::string>& pnames);
132 
133  /*
134  \brief It returns the name of the base dataset of a linked dataset
135  and how many of it's properties are a part of the objectId
136 
137  \param type A pointer to a valid data set type. Do not pass null.
138  \param pnames The pair that will be filled with the dataset name and the number of properties.
139  */
140  TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType* type, std::pair<std::string, int>& dsProps);
141 
142  /*
143  \brief It returns the property positions used to generate the object ids.
144 
145  This method first looks for the primary key properties defined on data set type.
146  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
147 
148  \param type A pointer to a valid data set type. Do not pass null.
149  \param pnames The vector that will be filled with the property names.
150  */
151  TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType* type, std::vector<std::size_t>& ppos);
152 
153  /*
154  \function getBasePkey
155  \brief Function used to acquire the string that represents the objectId of the base dataset.
156 
157  \param oid The objectId as returned from the dataset;
158  \param dsProps A pair that indicates the name of the base dataset and how many key properties it's got
159 
160  \note Will return the whole objectId as a string unless the dataset of interest is based on a linked layer,
161  otherwise it returns a string that represents the objectId of the base Dataset.
162  \note It will not take ownership of the given objectId pointer
163  */
164  TEDATAACCESSEXPORT std::string getBasePkey(te::da::ObjectId* oid, std::pair<std::string, int>& dsProps);
165 
166  /*
167  \brief It generates the set of object ids for every element of the given dataset.
168 
169  This method tries to use the primary key properties defined on data set type to generate the object ids.
170  If they do not exist it tries to use unique keys. If they do not exist then it uses all the properties.
171 
172  \param dataset A pointer to a valid data set. Do not pass null.
173  \param type A pointer to a data set type that describes the given dataset. Do not pass null.
174 
175  \return The object id set generated from the given dataset.
176  */
177  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const DataSetType* type);
178 
179  /*
180  \brief It generates the set of object ids for every element of the given dataset using a set of attributes.
181 
182  \param dataset A pointer to a valid data set.
183  \param names A non empty vector of property names to be used to identify the elements.
184 
185  \return The object id set generated from the given dataset.
186 
187  \exception Exception It throws an exception if a property was not found in data set
188  */
189  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const std::vector<std::string>& names);
190 
191  /*
192  \brief It generates an object id for the current element of the given dataset using the informed set of attributes.
193 
194  \param dataset A pointer to a valid data set.
195  \param names A non empty vector of property names to be used to identify the element.
196 
197  \return The object id generated for the current element of the given dataset.
198  */
199  TEDATAACCESSEXPORT ObjectId* GenerateOID(DataSet* dataset, const std::vector<std::string>& names);
200 
201  /*!
202  \brief It returns the first dataset spatial property or NULL if none is found.
203 
204  \param dataset The dataset to search for a spatial property.
205 
206  \return The first dataset spatial property or NULL if none is found.
207  */
209 
210  TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype);
211 
212  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet* dataset, const std::string& name);
213 
214  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSetType* dt, const std::string& name);
215 
217 
219 
221 
222  //TEDATAACCESSEXPORT te::da::DataSetType* CreateDataSetType(const te::da::DataSet* dataset);
223 
224  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType* const dt,
225  std::vector<std::string>& pnames,
226  std::vector<int>& ptypes);
227 
228  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSet* const dataset,
229  std::vector<std::string>& pnames,
230  std::vector<int>& ptypes);
231 
232  /*!
233  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
234 
235  This function will create the dataset schema definition and will
236  save all the dataset data.
237 
238  \param t The data source transactor.
239  \param dt The source dataset definition.
240  \param d The source dataset data.
241  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
242 
243  \pre All parameters must be valid pointers.
244 
245  \post It is the caller responsability to release the dataset 'd' pointer.
246 
247  \exception Exception It throws an exception if the dataset can not be created.
248 
249  \note DataSetPersistence will start reading the dataset 'd' in the
250  current position. So, keep in mind that it is the caller responsability
251  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
252  */
253  TEDATAACCESSEXPORT void Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit = 0);
254 
255  /*!
256  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
257 
258  This method will create the dataset schema definition and will
259  save all the dataset data.
260 
261  \param dt The source dataset definition.
262  \param d The source dataset data.
263  \param options A list of optional modifiers. It is driver specific.
264  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
265 
266  \pre All parameters must be valid pointers.
267 
268  \exception Exception It throws an exception if the dataset can not be created.
269 
270  \note It is the caller responsability to release the dataset 'd' pointer.
271 
272  \note DataSetPersistence will start reading the dataset 'd' in the
273  current position. So, keep in mind that it is the caller responsability
274  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
275  */
276  TEDATAACCESSEXPORT void Create(DataSource* ds,
277  DataSetType* dt,
278  DataSet* d,
279  const std::map<std::string, std::string>& options,
280  std::size_t limit = 0);
281 
282  TEDATAACCESSEXPORT DataSetAdapter* CreateAdapter(DataSet* ds, DataSetTypeConverter* converter, bool isOwner = false);
283 
284  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType* dt);
285 
286  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSet* dataset);
287 
288  TEDATAACCESSEXPORT std::vector<int> GetPropertyDataTypes(const te::da::DataSet* dataset);
289 
290  TEDATAACCESSEXPORT std::auto_ptr<Fields> BuildFields(const std::vector<std::string>& properties);
291 
292  TEDATAACCESSEXPORT std::auto_ptr<Expression> BuildSpatialOp(Expression* e1,
293  Expression* e2,
295 
296  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname);
297 
298  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
299  const std::string& propertyName);
300 
301  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
302  const std::vector<std::string>& properties);
303 
304  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
305  const std::vector<std::string>& properties,
306  const std::string& geometryProperty,
307  const te::gm::Envelope* e,
308  int srid,
310 
311  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
312  const std::vector<std::string>& properties,
313  const std::string& geometryProperty,
314  te::gm::Geometry* g,
316 
317  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
318  const std::vector<std::string>& properties,
319  const ObjectIdSet* oids);
320 
321 
322  TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName);
323 
324  /*!
325  \brief It checks if the name is not valid as the existence of invalid characters, reserved words, and others.
326 
327  \param name Name that will be checked.
328  \param invalidChar Records the invalid character.
329 
330  \return If the name is valid.
331  */
332  TEDATAACCESSEXPORT bool IsValidName(const std::string& name, std::string& invalidChar);
333 
334  /*!
335  \brief It checks if the datasettype has a linked table.
336 
337  \param type The Datasettype.
338 
339  \return True if has linked table.
340  */
342 
343  /*!
344  \brief It gets the summarized value.
345 
346  \param values The input values.
347  \param summary The summary mode. It can be: "MIN", "MAX", "SUM", "AVERAGE", "MEDIAN", "STDDEV" or "VARIANCE"
348 
349  \return The summarized value.
350  */
351  TEDATAACCESSEXPORT double GetSummarizedValue(std::vector<double>& values, const std::string& summary);
352 
353  /*!
354  \brief It gets the summarized value.
355 
356  \param values The input values.
357  \param summary The summary mode. It can be: "MIN" or "MAX"
358 
359  \return The summarized value.
360  */
361  TEDATAACCESSEXPORT std::string GetSummarizedValue(const std::vector<std::string>& values, const std::string& sumary);
362 
363  /*!
364  \brief It gets the round value.
365 
366  \param value The input value.
367 
368  \return The rounded value.
369  */
370  TEDATAACCESSEXPORT double Round(const double& value, const size_t& precision);
371 
372  /*!
373  \brief It gets the value as double.
374 
375  \param ds The input dataset.
376  \param pos The column position.
377 
378  \return The double value. Default value is 0.
379  */
380  TEDATAACCESSEXPORT double GetValueAsDouble(const te::da::DataSet* ds, const size_t pos);
381 
382  } // end namespace da
383 } // end namespace te
384 
385 #endif // __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
386 
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:571
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:136
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:120
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:286
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:446
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
Definition: Utils.cpp:779
TEDATAACCESSEXPORT bool HasLinkedTable(te::da::DataSetType *type)
It checks if the datasettype has a linked table.
Definition: Utils.cpp:1175
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
Definition: Utils.cpp:722
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
Definition: Utils.cpp:164
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:500
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
Definition: Utils.cpp:670
TEDATAACCESSEXPORT double GetValueAsDouble(const te::da::DataSet *ds, const size_t pos)
It gets the value as double.
Definition: Utils.cpp:1337
TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType *type, std::pair< std::string, int > &dsProps)
Definition: Utils.cpp:355
It models a property definition.
Definition: Property.h:59
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:919
Raster property.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
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...
Definition: Utils.cpp:626
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:213
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
Definition: Utils.cpp:200
TEDATAACCESSEXPORT double GetSummarizedValue(std::vector< double > &values, const std::string &summary)
It gets the summarized value.
Definition: Utils.cpp:1196
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:412
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:153
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
Definition: Utils.cpp:708
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
TEDATAACCESSEXPORT double Round(const double &value, const size_t &precision)
It gets the round value.
Definition: Utils.cpp:1329
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType *type, std::vector< std::size_t > &ppos)
Definition: Utils.cpp:401
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...
Definition: Utils.cpp:934
TEDATAACCESSEXPORT std::string getBasePkey(te::da::ObjectId *oid, std::pair< std::string, int > &dsProps)
Definition: Utils.cpp:388
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:309
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:228
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:100
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:70
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
Definition: Utils.cpp:769
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
Definition: Utils.cpp:534
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
Definition: Utils.cpp:598