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 DataAccess;
63  class DataSet;
64  class DataSetAdapter;
65  class DataSetType;
66  class DataSetTypeConverter;
67  class DataSourceCatalogLoader;
68  class DataSourceTransactor;
69  class ObjectId;
70  class ObjectIdSet;
71 
72  TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType* dataset, const std::string& datasourceId);
73 
74 // TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType* dataset, te::da::DataSource* datasource);
75 
76  TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType* dataset, const std::string& datasourceId);
77 
78  /*!
79  \return The data extent considering the informed property. The caller will take the ownership of the returned box.
80 
81  \exception Exception It throws an exception if the extent can not be determined.
82  */
83  TEDATAACCESSEXPORT te::gm::Envelope* GetExtent(const std::string& datasetName,
84  const std::string& propertyName,
85  const std::string& datasourceId);
86 
87  TEDATAACCESSEXPORT void GetDataSetNames(std::vector<std::string>& datasetNames, const std::string& datasourceId);
88 
89  TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category);
90 
91  TEDATAACCESSEXPORT bool HasDataSet(const std::string& datasourceId);
92 
93  TEDATAACCESSEXPORT DataSet* GetDataSet(const std::string& name, const std::string& datasourceId);
94 
95  TEDATAACCESSEXPORT DataSetType* GetDataSetType(const std::string& name, const std::string& datasourceId);
96 
97  /*!
98  \brief Search for a data source with the informed id in the DataSourceManager.
99 
100  If the data source is not found in that manager it will try to get some information
101  in the DataSourceInfoManager and will create a new data source instance,
102  storing a reference to the new data source in the manager.
103 
104  \param datasourceId The data source to look for.
105  \param opened Automatically open the data source.
106 
107  \return A data source with the informed id.
108 
109  \exception Exception It throws an exception if the informed data source could not be retrieved.
110  */
111  TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string& datasourceId, const bool opened = true);
112 
113  /*!
114  \brief Returns an empty ObjectIdSet, with the definitions of fields that compose it.
115 
116  \param type[in] A pointer to a valid data set type. Do not pass null.
117 
118  \param set[out] The ObjectIdSet containing only the the fields definitions. This pointer will be instantiated inside the method.
119 
120  \note The caller WILL TAKE the ownership of \a set.
121  */
122  TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType* type, ObjectIdSet*& set);
123 
124  /*
125  \brief It returns the property names used to generate the object ids.
126 
127  This method first looks for the primary key properties defined on data set type.
128  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
129 
130  \param type A pointer to a valid data set type. Do not pass null.
131  \param pnames The vector that will be filled with the property names.
132  */
133  TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType* type, std::vector<std::string>& pnames);
134 
135  /*
136  \brief It returns the name of the base dataset of a linked dataset
137  and how many of it's properties are a part of the objectId
138 
139  \param type A pointer to a valid data set type. Do not pass null.
140  \param pnames The pair that will be filled with the dataset name and the number of properties.
141  */
142  TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType* type, std::pair<std::string, int>& dsProps);
143 
144  /*
145  \brief It returns the property positions used to generate the object ids.
146 
147  This method first looks for the primary key properties defined on data set type.
148  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
149 
150  \param type A pointer to a valid data set type. Do not pass null.
151  \param pnames The vector that will be filled with the property names.
152  */
153  TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType* type, std::vector<std::size_t>& ppos);
154 
155  /*
156  \function getBasePkey
157  \brief Function used to acquire the string that represents the objectId of the base dataset.
158 
159  \param oid The objectId as returned from the dataset;
160  \param dsProps A pair that indicates the name of the base dataset and how many key properties it's got
161 
162  \note Will return the whole objectId as a string unless the dataset of interest is based on a linked layer,
163  otherwise it returns a string that represents the objectId of the base Dataset.
164  \note It will not take ownership of the given objectId pointer
165  */
166  TEDATAACCESSEXPORT std::string getBasePkey(te::da::ObjectId* oid, std::pair<std::string, int>& dsProps);
167 
168  /*
169  \brief It generates the set of object ids for every element of the given dataset.
170 
171  This method tries to use the primary key properties defined on data set type to generate the object ids.
172  If they do not exist it tries to use unique keys. If they do not exist then it uses all the properties.
173 
174  \param dataset A pointer to a valid data set. Do not pass null.
175  \param type A pointer to a data set type that describes the given dataset. Do not pass null.
176 
177  \return The object id set generated from the given dataset.
178  */
179  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const DataSetType* type);
180 
181  /*
182  \brief It generates the set of object ids for every element of the given dataset using a set of attributes.
183 
184  \param dataset A pointer to a valid data set.
185  \param names A non empty vector of property names to be used to identify the elements.
186 
187  \return The object id set generated from the given dataset.
188 
189  \exception Exception It throws an exception if a property was not found in data set
190  */
191  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const std::vector<std::string>& names);
192 
193  /*
194  \brief It generates an object id for the current element of the given dataset using the informed set of attributes.
195 
196  \param dataset A pointer to a valid data set.
197  \param names A non empty vector of property names to be used to identify the element.
198 
199  \return The object id generated for the current element of the given dataset.
200  */
201  TEDATAACCESSEXPORT ObjectId* GenerateOID(DataSet* dataset, const std::vector<std::string>& names);
202 
203  /*!
204  \brief It returns the first dataset spatial property or NULL if none is found.
205 
206  \param dataset The dataset to search for a spatial property.
207 
208  \return The first dataset spatial property or NULL if none is found.
209  */
211 
212  TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype);
213 
214  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet* dataset, const std::string& name);
215 
216  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSetType* dt, const std::string& name);
217 
219 
221 
223 
224  //TEDATAACCESSEXPORT te::da::DataSetType* CreateDataSetType(const te::da::DataSet* dataset);
225 
226  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType* const dt,
227  std::vector<std::string>& pnames,
228  std::vector<int>& ptypes);
229 
230  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSet* const dataset,
231  std::vector<std::string>& pnames,
232  std::vector<int>& ptypes);
233 
234  /*!
235  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
236 
237  This function will create the dataset schema definition and will
238  save all the dataset data.
239 
240  \param t The data source transactor.
241  \param dt The source dataset definition.
242  \param d The source dataset data.
243  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
244 
245  \pre All parameters must be valid pointers.
246 
247  \post It is the caller responsability to release the dataset 'd' pointer.
248 
249  \exception Exception It throws an exception if the dataset can not be created.
250 
251  \note DataSetPersistence will start reading the dataset 'd' in the
252  current position. So, keep in mind that it is the caller responsability
253  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
254  */
255  TEDATAACCESSEXPORT void Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit = 0);
256 
257  /*!
258  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
259 
260  This method will create the dataset schema definition and will
261  save all the dataset data.
262 
263  \param dt The source dataset definition.
264  \param d The source dataset data.
265  \param options A list of optional modifiers. It is driver specific.
266  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
267 
268  \pre All parameters must be valid pointers.
269 
270  \exception Exception It throws an exception if the dataset can not be created.
271 
272  \note It is the caller responsability to release the dataset 'd' pointer.
273 
274  \note DataSetPersistence will start reading the dataset 'd' in the
275  current position. So, keep in mind that it is the caller responsability
276  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
277  */
278  TEDATAACCESSEXPORT void Create(DataSource* ds,
279  DataSetType* dt,
280  DataSet* d,
281  const std::map<std::string, std::string>& options,
282  std::size_t limit = 0);
283 
284  TEDATAACCESSEXPORT DataSetAdapter* CreateAdapter(DataSet* ds, DataSetTypeConverter* converter, bool isOwner = false);
285 
286  TEDATAACCESSEXPORT void AssociateDataSetTypeConverterSRID(DataSetTypeConverter* converter, const int& inputSRID, const int& outputSRID = TE_UNKNOWN_SRS);
287 
288  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType* dt);
289 
290  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSet* dataset);
291 
292  TEDATAACCESSEXPORT std::vector<int> GetPropertyDataTypes(const te::da::DataSet* dataset);
293 
294  TEDATAACCESSEXPORT std::unique_ptr<Fields> BuildFields(const std::vector<std::string>& properties);
295 
296  TEDATAACCESSEXPORT std::unique_ptr<Expression> BuildSpatialOp(Expression* e1,
297  Expression* e2,
299 
300  TEDATAACCESSEXPORT std::unique_ptr<Select> BuildSelect(const std::string& dsname);
301 
302  TEDATAACCESSEXPORT std::unique_ptr<Select> BuildSelect(const std::string& dsname,
303  const std::string& propertyName);
304 
305  TEDATAACCESSEXPORT std::unique_ptr<Select> BuildSelect(const std::string& dsname,
306  const std::vector<std::string>& properties);
307 
308  TEDATAACCESSEXPORT std::unique_ptr<Select> BuildSelect(const std::string& dsname,
309  const std::vector<std::string>& properties,
310  const std::string& geometryProperty,
311  const te::gm::Envelope* e,
312  int srid,
314 
315  TEDATAACCESSEXPORT std::unique_ptr<Select> BuildSelect(const std::string& dsname,
316  const std::vector<std::string>& properties,
317  const std::string& geometryProperty,
318  te::gm::Geometry* g,
320 
321  TEDATAACCESSEXPORT std::unique_ptr<Select> BuildSelect(const std::string& dsname,
322  const std::vector<std::string>& properties,
323  const ObjectIdSet* oids);
324 
325 
326  TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName);
327 
328  /*!
329  \brief It checks if the name is not valid as the existence of invalid characters, reserved words, and others.
330 
331  \param name Name that will be checked.
332  \param invalidChar Records the invalid character.
333 
334  \return If the name is valid.
335  */
336  TEDATAACCESSEXPORT bool IsValidName(const std::string& name, std::string& invalidChar);
337 
338  /*!
339  \brief It checks if the datasettype has a linked table.
340 
341  \param type The Datasettype.
342 
343  \return True if has linked table.
344  */
346 
347  /*!
348  \brief It hide columns of a DataSet using DataSetAdapter.
349 
350  \param ds The DataSet that will have hidden columns.
351  \param dst The DataSetType used to hide columns.
352  \param columns names to be hidden.
353 
354  \return The DataSet result. The caller will take the ownership of the returned DataSet.
355  */
356  TEDATAACCESSEXPORT std::unique_ptr<te::da::DataSet> HideColumns(te::da::DataSet* ds, te::da::DataSetType* dst, const std::vector<std::string>& columns);
357 
358  /*!
359  \brief It gets the summarized value.
360 
361  \param values The input values.
362  \param summary The summary mode. It can be: "MIN", "MAX", "SUM", "AVERAGE", "MEDIAN", "STDDEV" or "VARIANCE"
363 
364  \return The summarized value.
365  */
366  TEDATAACCESSEXPORT double GetSummarizedValue(std::vector<double>& values, const std::string& summary);
367 
368  /*!
369  \brief It gets the summarized value.
370 
371  \param values The input values.
372  \param summary The summary mode. It can be: "MIN" or "MAX"
373 
374  \return The summarized value.
375  */
376  TEDATAACCESSEXPORT std::string GetSummarizedValue(const std::vector<std::string>& values, const std::string& sumary);
377 
378  /*!
379  \brief It gets the round value.
380 
381  \param value The input value.
382 
383  \return The rounded value.
384  */
385  TEDATAACCESSEXPORT double Round(const double& value, const size_t& precision);
386 
387  /*!
388  \brief It gets the value as double.
389 
390  \param ds The input dataset.
391  \param pos The column position.
392 
393  \return The double value. Default value is 0.
394  */
395  TEDATAACCESSEXPORT double GetValueAsDouble(const te::da::DataSet* ds, const size_t pos);
396 
397  /*!
398  \brief Get alias based on a input name
399 
400  \param fullName name composed by table/schema.name
401 
402  \return string of the right side of dot "."
403  */
404  TEDATAACCESSEXPORT std::string GetAliasName(std::string fullName);
405 
406 
407  /*!
408  \brief Creates the concrete literal class based on the given abstract data
409 
410  \param abstractData The abstract data to be converted into a Literal
411 
412  \return The concrete literal class based on the given abstract data
413  */
415 
416  TEDATAACCESSEXPORT std::unique_ptr<te::da::DataSet> CreateAdapterToHandleAutoNumber(const te::da::DataAccess* dataAccess, te::da::DataSet* dataSet);
417 
418  } // end namespace da
419 } // end namespace te
420 
421 #endif // __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
422 
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
TEDATAACCESSEXPORT te::da::Literal * GetLiteralFromAbstractData(const te::dt::AbstractData *abstractData)
Creates the concrete literal class based on the given abstract data.
Geometric property.
TEDATAACCESSEXPORT std::unique_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
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:1449
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::unique_ptr< Select > BuildSelect(const std::string &dsname)
TEDATAACCESSEXPORT std::unique_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 std::string GetDataSetCategoryName(int category)
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
TEDATAACCESSEXPORT std::string GetAliasName(std::string fullName)
Get alias based on a input name.
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 models a literal value.
Definition: Literal.h:53
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...
TerraLib.
TEDATAACCESSEXPORT std::unique_ptr< te::da::DataSet > CreateAdapterToHandleAutoNumber(const te::da::DataAccess *dataAccess, te::da::DataSet *dataSet)
TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType *type, std::pair< std::string, int > &dsProps)
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)
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:55
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:77
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:113
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::unique_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)
Class used to centralize and control access to data in terralib. It aims to create a high-level inter...
Definition: DataAccess.h:73
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)