All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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  TEDATAACCESSEXPORT ObjectIdSet* GenerateOIDSet(DataSet* dataset, const std::vector<std::string>& names);
166 
167  /*
168  \brief It generates an object id for the current element of the given dataset using the informed set of attributes.
169 
170  \param dataset A pointer to a valid data set.
171  \param names A non empty vector of property names to be used to identify the element.
172 
173  \return The object id generated for the current element of the given dataset.
174  */
175  TEDATAACCESSEXPORT ObjectId* GenerateOID(DataSet* dataset, const std::vector<std::string>& names);
176 
177  /*!
178  \brief It returns the first dataset spatial property or NULL if none is found.
179 
180  \param dataset The dataset to search for a spatial property.
181 
182  \return The first dataset spatial property or NULL if none is found.
183  */
185 
186  TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet* dataset, int datatype);
187 
188  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet* dataset, const std::string& name);
189 
190  TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSetType* dt, const std::string& name);
191 
193 
195 
197 
198  //TEDATAACCESSEXPORT te::da::DataSetType* CreateDataSetType(const te::da::DataSet* dataset);
199 
200  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType* const dt,
201  std::vector<std::string>& pnames,
202  std::vector<int>& ptypes);
203 
204  TEDATAACCESSEXPORT void GetPropertyInfo(const DataSet* const dataset,
205  std::vector<std::string>& pnames,
206  std::vector<int>& ptypes);
207 
208  /*!
209  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
210 
211  This function will create the dataset schema definition and will
212  save all the dataset data.
213 
214  \param t The data source transactor.
215  \param dt The source dataset definition.
216  \param d The source dataset data.
217  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
218 
219  \pre All parameters must be valid pointers.
220 
221  \post It is the caller responsability to release the dataset 'd' pointer.
222 
223  \exception Exception It throws an exception if the dataset can not be created.
224 
225  \note DataSetPersistence will start reading the dataset 'd' in the
226  current position. So, keep in mind that it is the caller responsability
227  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
228  */
229  TEDATAACCESSEXPORT void Create(DataSource* ds, DataSetType* dt, DataSet* d, std::size_t limit = 0);
230 
231  /*!
232  \brief It creates the dataset definition in a data source and then fill it with data from the input dataset.
233 
234  This method will create the dataset schema definition and will
235  save all the dataset data.
236 
237  \param dt The source dataset definition.
238  \param d The source dataset data.
239  \param options A list of optional modifiers. It is driver specific.
240  \param limit The number of items to be used from the input dataset. If set to 0 (default) all items are used.
241 
242  \pre All parameters must be valid pointers.
243 
244  \exception Exception It throws an exception if the dataset can not be created.
245 
246  \note It is the caller responsability to release the dataset 'd' pointer.
247 
248  \note DataSetPersistence will start reading the dataset 'd' in the
249  current position. So, keep in mind that it is the caller responsability
250  to inform the dataset 'd' in the right position (and a valid one) to start processing it.
251  */
252  TEDATAACCESSEXPORT void Create(DataSource* ds,
253  DataSetType* dt,
254  DataSet* d,
255  const std::map<std::string, std::string>& options,
256  std::size_t limit = 0);
257 
258  TEDATAACCESSEXPORT DataSetAdapter* CreateAdapter(DataSet* ds, DataSetTypeConverter* converter, bool isOwner = false);
259 
260  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType* dt);
261 
262  TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSet* dataset);
263 
264  TEDATAACCESSEXPORT std::vector<int> GetPropertyDataTypes(const te::da::DataSet* dataset);
265 
266  TEDATAACCESSEXPORT std::auto_ptr<Fields> BuildFields(const std::vector<std::string>& properties);
267 
268  TEDATAACCESSEXPORT std::auto_ptr<Expression> BuildSpatialOp(Expression* e1,
269  Expression* e2,
271 
272  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname);
273 
274  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
275  const std::string& propertyName);
276 
277  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
278  const std::vector<std::string>& properties);
279 
280  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
281  const std::vector<std::string>& properties,
282  const std::string& geometryProperty,
283  const te::gm::Envelope* e,
284  int srid,
286 
287  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
288  const std::vector<std::string>& properties,
289  const std::string& geometryProperty,
290  te::gm::Geometry* g,
292 
293  TEDATAACCESSEXPORT std::auto_ptr<Select> BuildSelect(const std::string& dsname,
294  const std::vector<std::string>& properties,
295  const ObjectIdSet* oids);
296 
297 
298  TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet* dataSet, const std::string propName);
299 
300  } // end namespace da
301 } // end namespace te
302 
303 #endif // __TERRALIB_DATAACCESS_INTERNAL_UTILS_H
304 
TEDATAACCESSEXPORT std::auto_ptr< Expression > BuildSpatialOp(Expression *e1, Expression *e2, te::gm::SpatialRelation r)
Definition: Utils.cpp:669
TEDATAACCESSEXPORT void GetDataSetNames(std::vector< std::string > &datasetNames, const std::string &datasourceId)
Definition: Utils.cpp:149
TEDATAACCESSEXPORT ObjectIdSet * GenerateOIDSet(DataSet *dataset, const DataSetType *type)
Definition: Utils.cpp:362
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:409
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:518
TEDATAACCESSEXPORT std::auto_ptr< Fields > BuildFields(const std::vector< std::string > &properties)
Definition: Utils.cpp:716
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:258
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:504
TEDATAACCESSEXPORT DataSet * GetDataSet(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:209
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
Definition: Utils.cpp:282
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:132
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
TEDATAACCESSEXPORT void GetPropertyInfo(const DataSetType *const dt, std::vector< std::string > &pnames, std::vector< int > &ptypes)
Definition: Utils.cpp:545
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
Definition: Utils.cpp:481
#define TEDATAACCESSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:135
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:393
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType *type, std::vector< std::size_t > &ppos)
Definition: Utils.cpp:351
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
TEDATAACCESSEXPORT std::vector< int > GetPropertyDataTypes(const te::da::DataSet *dataset)
Definition: Utils.cpp:655
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
TEDATAACCESSEXPORT DataSetType * GetDataSetType(const std::string &name, const std::string &datasourceId)
Definition: Utils.cpp:224
TEDATAACCESSEXPORT bool HasDataSet(const std::string &datasourceId)
Definition: Utils.cpp:196
TEDATAACCESSEXPORT std::auto_ptr< Select > BuildSelect(const std::string &dsname)
Definition: Utils.cpp:726
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:447
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEDATAACCESSEXPORT int GetPropertyIndex(te::da::DataSet *dataSet, const std::string propName)
Definition: Utils.cpp:866
TEDATAACCESSEXPORT std::string GetDataSetCategoryName(int category)
Definition: Utils.cpp:160
Raster property.
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:573
It models a property definition.
Definition: Property.h:59
TEDATAACCESSEXPORT void GetOIDPropertyNames(const DataSetType *type, std::vector< std::string > &pnames)
Definition: Utils.cpp:305
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:116
TEDATAACCESSEXPORT std::string GetSQLValueNames(const DataSetType *dt)
Definition: Utils.cpp:617
TEDATAACCESSEXPORT void LoadFull(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:66
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
Geometric property.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:111
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:591