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-2013 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 property positions used to generate the object ids.
135 
136  This method first looks for the primary key properties defined on data set type.
137  If they do not exist it looks for the unique keys. If they do not exist then it returns all the properties.
138 
139  \param type A pointer to a valid data set type. Do not pass null.
140  \param pnames The vector that will be filled with the property names.
141  */
142  TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType* type, std::vector<std::size_t>& ppos);
143 
144  /*
145  \brief It generates the set of object ids for every element of the given dataset.
146 
147  This method tries to use the primary key properties defined on data set type to generate the object ids.
148  If they do not exist it tries to use unique keys. If they do not exist then it uses all the properties.
149 
150  \param dataset A pointer to a valid data set. Do not pass null.
151  \param type A pointer to a data set type that describes the given dataset. Do not pass null.
152 
153  \return The object id set generated from the given dataset.
154  */
155  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const DataSetType* type);
156 
157  /*
158  \brief It generates the set of object ids for every element of the given dataset using a set of attributes.
159 
160  \param dataset A pointer to a valid data set.
161  \param names A non empty vector of property names to be used to identify the elements.
162 
163  \return The object id set generated from the given dataset.
164 
165  \exception Exception It throws an exception if a property was not found in data set
166  */
167  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const std::vector<std::string>& names);
168 
169  /*
170  \brief It generates an object id for the current element of the given dataset using the informed set of attributes.
171 
172  \param dataset A pointer to a valid data set.
173  \param names A non empty vector of property names to be used to identify the element.
174 
175  \return The object id generated for the current element of the given dataset.
176  */
177  TEDATAACCESSEXPORT ObjectId* GenerateOID(DataSet* dataset, const std::vector<std::string>& names);
178 
179  /*!
180  \brief It returns the first dataset spatial property or NULL if none is found.
181 
182  \param dataset The dataset to search for a spatial property.
183 
184  \return The first dataset spatial property or NULL if none is found.
185  */
187 
188  TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype);
189 
190  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet* dataset, const std::string& name);
191 
192  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSetType* dt, const std::string& name);
193 
195 
197 
199 
200  //TEDATAACCESSEXPORT te::da::DataSetType* CreateDataSetType(const te::da::DataSet* dataset);
201 
202  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType* const dt,
203  std::vector<std::string>& pnames,
204  std::vector<int>& ptypes);
205 
206  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSet* const dataset,
207  std::vector<std::string>& pnames,
208  std::vector<int>& ptypes);
209 
210  /*!
211  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
212 
213  This function will create the dataset schema definition and will
214  save all the dataset data.
215 
216  \param t The data source transactor.
217  \param dt The source dataset definition.
218  \param d The source dataset data.
219  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
220 
221  \pre All parameters must be valid pointers.
222 
223  \post It is the caller responsability to release the dataset 'd' pointer.
224 
225  \exception Exception It throws an exception if the dataset can not be created.
226 
227  \note DataSetPersistence will start reading the dataset 'd' in the
228  current position. So, keep in mind that it is the caller responsability
229  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
230  */
231  TEDATAACCESSEXPORT void Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit = 0);
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 method will create the dataset schema definition and will
237  save all the dataset data.
238 
239  \param dt The source dataset definition.
240  \param d The source dataset data.
241  \param options A list of optional modifiers. It is driver specific.
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  \exception Exception It throws an exception if the dataset can not be created.
247 
248  \note It is the caller responsability to release the dataset 'd' pointer.
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,
255  DataSetType* dt,
256  DataSet* d,
257  const std::map<std::string, std::string>& options,
258  std::size_t limit = 0);
259 
260  TEDATAACCESSEXPORT DataSetAdapter* CreateAdapter(DataSet* ds, DataSetTypeConverter* converter, bool isOwner = false);
261 
262  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType* dt);
263 
264  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSet* dataset);
265 
266  TEDATAACCESSEXPORT std::vector<int> GetPropertyDataTypes(const te::da::DataSet* dataset);
267 
268  TEDATAACCESSEXPORT std::auto_ptr<Fields> BuildFields(const std::vector<std::string>& properties);
269 
270  TEDATAACCESSEXPORT std::auto_ptr<Expression> BuildSpatialOp(Expression* e1,
271  Expression* e2,
273 
274  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname);
275 
276  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
277  const std::string& propertyName);
278 
279  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
280  const std::vector<std::string>& properties);
281 
282  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
283  const std::vector<std::string>& properties,
284  const std::string& geometryProperty,
285  const te::gm::Envelope* e,
286  int srid,
288 
289  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
290  const std::vector<std::string>& properties,
291  const std::string& geometryProperty,
292  te::gm::Geometry* g,
294 
295  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
296  const std::vector<std::string>& properties,
297  const ObjectIdSet* oids);
298 
299 
300  TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName);
301 
302  /*!
303  \brief It checks if the name is not valid as the existence of invalid characters, reserved words, and others.
304 
305  \param name Name that will be checked.
306  \param invalidChar Records the invalid character.
307 
308  \return If the name is valid.
309  */
310  TEDATAACCESSEXPORT bool IsValidName(const std::string& name, std::string& invalidChar);
311 
312  } // end namespace da
313 } // end namespace te
314 
315 #endif // __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
316 
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:259
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:522
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:133
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:117
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:283
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:397
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
Definition: Utils.cpp:730
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
Definition: Utils.cpp:673
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
Definition: Utils.cpp:161
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:451
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
Definition: Utils.cpp:621
It models a property definition.
Definition: Property.h:59
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:870
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:413
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:577
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:210
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
Definition: Utils.cpp:197
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:363
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:150
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
Definition: Utils.cpp:659
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
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:352
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:885
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:306
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:225
#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:432
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:508
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:595
TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:67
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
Definition: Utils.cpp:720
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
Definition: Utils.cpp:485
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
Definition: Utils.cpp:549