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/ogr/Utils.h
22 
23  \brief Utility functions for OGR support.
24 */
25 
26 #ifndef __TERRALIB_OGR_INTERNAL_UTILS_H
27 #define __TERRALIB_OGR_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "Config.h"
31 #include "../srs/Config.h"
32 
33 // BOOST
34 #include <boost/thread/mutex.hpp>
35 
36 // OGR
37 #include <ogr_core.h>
38 #include <ogr_feature.h>
39 #include <cpl_error.h>
40 
41 // Forward declarations
42 class GDALDataset;
43 class OGRGeometry;
44 class OGREnvelope;
45 class OGRSpatialReference;
46 class OGRFeatureDefn;
47 class OGRFieldDefn;
48 class OGRLayer;
49 
50 namespace te
51 {
52  namespace da
53  {
54  class DataSetType;
55  class FieldNameValidator;
56  }
57 
58  namespace gm
59  {
60  class Envelope;
61  class Geometry;
62  }
63 }
64 
65 namespace te
66 {
67  namespace ogr
68  {
69  /*!
70  \brief It converts the OGR Geometry to TerraLib Geometry.
71 
72  \param ogrGeom A valid OGR Geometry.
73 
74  \return A valid TerraLib Geometry.
75 
76  \exception Exception It throws an exception if the OGR geometry can not be converted.
77 
78  \note It uses the WKB to create the TerraLib Geometry
79  \note The caller of this function will take the ownership of the returned TerraLib Geometry.
80  */
82 
83  /*!
84  \brief It converts the TerraLib Geometry to OGR Geometry.
85 
86  \param teGeom A valid TerraLib Geometry.
87 
88  \return A valid TerraLib geometry.
89 
90  \exception Exception It throws an exception if the TerraLib geometry can not be converted.
91 
92  \note It uses the WKB to create the TerraLib Geometry
93  \note The caller of this function will take the ownership of the returned OGR Geometry.
94  */
95  TEOGREXPORT OGRGeometry* Convert2OGR(const te::gm::Geometry* teGeom);
96 
97  /*!
98  \brief It converts the TerraLib Geometry to OGR Geometry.
99 
100  \param teGeom A valid TerraLib Geometry.
101 
102  \param srs OGR srs information. It must not be const so it can be shared with the created geometry without the need of copying it.
103 
104  \return A valid TerraLib geometry.
105 
106  \exception Exception It throws an exception if the TerraLib geometry can not be converted.
107 
108  \note It uses the WKB to create the TerraLib Geometry
109  \note The caller of this function will take the ownership of the returned OGR Geometry.
110  */
111  TEOGREXPORT OGRGeometry* Convert2OGR(const te::gm::Geometry* teGeom, OGRSpatialReference* srs);
112 
113  /*!
114  \brief It converts the OGR Envelope to TerraLib Envelope.
115 
116  \param env A valid OGR Envelope.
117 
118  \return A valid TerraLib Envelope.
119 
120  \note The caller of this function will take the ownership of the returned TerraLib Envelope.
121  */
123 
124  /*!
125  \brief It converts the TerraLib Envelope to OGR Envelope.
126 
127  \param env A valid TerraLib Envelope.
128 
129  \return A valid OGR Envelope.
130 
131  \note The caller of this function will take the ownership of the returned OGR Envelope.
132  */
133  TEOGREXPORT OGREnvelope* Convert2OGR(const te::gm::Envelope* env);
134 
135  /*!
136  \brief It converts the OGR Projection to TerraLib Projection.
137 
138  \param osrs A valid OGR Projection.
139 
140  \return An SRS id recognized by TerraLib or an indication of unknown SRS (TE_UNKNOWN_SRS)
141  */
142  TEOGREXPORT int Convert2TerraLibProjection(OGRSpatialReference* osrs);
143 
144  /*!
145  \brief It converts the TerraLib Projection to OGR Projection.
146 
147  \param srid A valid TerraLib Projection id.
148 
149  \return A valid OGR Spatial Reference System.
150 
151  \exception Exception It throws an exception if the SRS can not be imported using OGR.
152 
153  \note The caller of this function will take the ownership of the returned OGR Spatial Reference System.
154  */
155  TEOGREXPORT OGRSpatialReference* Convert2OGRProjection(int srid);
156 
157  /*!
158  \brief It inserts a OGR Feature Definition to TerraLib DataSet Type
159 
160  \param featDef A valid OGR Feature Definition.
161  \param dt Pointer to a TerraLib dataset type previously created. Do not pass NULL.
162  \param srs Optional parameter to set the SRS of geometry fields.
163  \param geometryPropertyName A geomtry property name to force or an empty string to automatically find this value.
164 
165  \exception Exception It throws an exception if the feature definition can not be converted.
166 
167  \note It uses the method that converts a OGR Field Definition to TerraLib Property Type.
168  \note It uses the method that converts a OGR Geometry Type to TerraLib Geometry Type.
169  */
170  TEOGREXPORT void Convert2TerraLib(OGRFeatureDefn* featDef, te::da::DataSetType* dt, const std::string& geometryPropertyName, int srs=TE_UNKNOWN_SRS);
171 
172  /*!
173  \brief It converts the OGR Feature Definition to TerraLib DataSet Type
174 
175  \param featDef A valid OGR Feature Definition.
176  \param srs Optional parameter to set the SRS of geometry fields.
177  \param geometryPropertyName A geomtry property name to force or an empty string to automatically find this value.
178 
179  \return A valid TerraLib DataSet Type.
180 
181  \exception Exception It throws an exception if the feature definition can not be converted.
182 
183  \note It uses the method that converts a OGR Field Definition to TerraLib Property Type.
184  \note It uses the method that converts a OGR Geometry Type to TerraLib Geometry Type.
185  \note The caller of this function will take the ownership of the returned TerraLib DataSet Type.
186  */
187  TEOGREXPORT te::da::DataSetType* Convert2TerraLib(OGRFeatureDefn* featDef, const std::string& geometryPropertyName, int srs=TE_UNKNOWN_SRS);
188 
189  /*!
190  \brief It converts the OGR Feature Definition to TerraLib DataSet Type. It also handles FID and Geometry property issues
191 
192  \param ogrLayer A valid OGR Layer.
193  \param srs Optional parameter to set the SRS of geometry fields.
194 
195  \return A valid TerraLib DataSet Type.
196 
197  \exception Exception It throws an exception if the feature definition can not be converted.
198 
199  \note It uses the method that converts a OGR Field Definition to TerraLib Property Type.
200  \note It uses the method that converts a OGR Geometry Type to TerraLib Geometry Type.
201  \note The caller of this function will take the ownership of the returned TerraLib DataSet Type.
202  */
203  TEOGREXPORT te::da::DataSetType* GetDataSetType(GDALDataset* gdalDataSet, OGRLayer* ogrLayer, int srs = TE_UNKNOWN_SRS);
204 
205  /*!
206  \brief It converts the TerraLib DataSet Type to OGR Feature Definition
207 
208  \param dt A valid TerraLib DataSet Type.
209 
210  \return A valid OGR Feature Definition.
211 
212  \exception Exception It throws an exception if the data set type can not be converted.
213 
214  \note It uses the method that converts a TerraLib Property Type to OGR Field Definition.
215  \note The caller of this function will take the ownership of the returned OGR Feature Definition.
216  */
218 
219  /*!
220  \brief It converts the OGR Field Definition to TerraLib Property Type
221 
222  \param fieldDef A valid OGR Field Definition.
223 
224  \return A valid TerraLib Property Type.
225 
226  \exception Exception It throws an exception if the data type is not supported by TerraLib.
227 
228  \note The caller of this function will take the ownership of the returned TerraLib Property Type.
229  */
231 
232  /*!
233  \brief It converts the TerraLib Property Type to OGR Field Definition
234 
235  \param pt A valid TerraLib Property Type.
236 
237  \return A valid OGR Field Definition.
238 
239  \exception Exception It throws an exception if the data type is not supported by OGR.
240  */
242 
243  /*!
244  \brief It converts the TerraLib Geometric Property Type to OGR Geometric Field Definition
245 
246  \param pt A valid TerraLib Geometric Property Type.
247 
248  \return A valid OGR Geometric Field Definition.
249 
250  \exception Exception It throws an exception if the data type is not supported by OGR.
251  */
253 
254  /*!
255  \brief It converts the OGR Geometry Type element to TerraLib Geometry Type
256 
257  \param ogrGeomType A valid OGR Geometry Type element.
258 
259  \return A valid TerraLib Geometry Type
260  */
261  TEOGREXPORT te::gm::GeomType Convert2TerraLib(OGRwkbGeometryType ogrGeomType);
262 
263  /*!
264  \brief It converts the TerraLib Geometry Type element to OGR Geometry Type
265 
266  \param geomType A valid TerraLib Geometry Type.
267 
268  \return A valid OGR Geometry Type element
269 
270  \exception Exception It throws an exception if the geometry type is not supported by OGR.
271  */
272  TEOGREXPORT OGRwkbGeometryType Convert2OGR(te::gm::GeomType geomType);
273 
274  /*!
275  \brief It tries extract the driver name used by OGR Library based on the given path.
276 
277  \param path The path that will be consulted.
278 
279  \return The driver name used by OGR Library or an empty string if not possible extract the driver name.
280  */
281  std::string GetDriverName(const std::string& path);
282 
283  /*!
284  \brief It returns the list of OGR Drivers available.
285 
286  \param filterCreate Set TRUE to indicate to retrieve only the drivers with the capacity to create datasources.
287 
288  \return A list of OGR Drivers.
289  */
290  TEOGREXPORT std::vector<std::string> GetOGRDrivers(bool filterCreate = false);
291 
292  TEOGREXPORT std::string GetOGRConnectionInfo(const std::map<std::string, std::string>& connInfo);
293 
294  TEOGREXPORT std::string RemoveSpatialSql(const std::string& sql);
295 
296  /*!
297  \brief Returns a reference to a static mutex initialized when this module is initialized.
298  \return Returns a reference to a static mutex initialized when this module is initialized.
299  */
300  TEOGREXPORT boost::mutex& getStaticMutex();
301 
302  void OGRErrorHandler(CPLErr eErrClass, int errNo, const char* msg);
303 
304  /*!
305  \brief Creates a GDALDataSet based on the given fileName and open flags. The main purpose of this function is to ensure that known files with extensions will be open if the correct drivers
306  */
307  TEOGREXPORT GDALDataset* CreateGDALDataSet(const std::string& pszFilename, unsigned int nOpenFlags);
308 
309  /*!
310  \brief Executes the given query in the given dataSet. Throws exception in case of error. It also handles FID for specific drivers
311  */
312  TEOGREXPORT OGRLayer* ExecuteSQL(GDALDataset* gdalDataSet, const std::string& query);
313 
314  //!< Initialized the validator from the given layer definition
315  bool InitValidator(te::da::FieldNameValidator& validator, OGRFeatureDefn* ogrFeatureDefinition);
316 
317  } // end namespace ogr
318 } // end namespace te
319 
320 #endif // __TERRALIB_OGR_INTERNAL_UTILS_H
A class that models the description of a dataset.
Definition: DataSetType.h:73
This class is responsible for validating field names. It can handle size limits, fix special characte...
It models a property definition.
Definition: Property.h:60
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:52
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:78
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:42
TEOGREXPORT std::vector< std::string > GetOGRDrivers(bool filterCreate=false)
It returns the list of OGR Drivers available.
TEOGREXPORT int Convert2TerraLibProjection(OGRSpatialReference *osrs)
It converts the OGR Projection to TerraLib Projection.
TEOGREXPORT boost::mutex & getStaticMutex()
Returns a reference to a static mutex initialized when this module is initialized.
TEOGREXPORT OGRLayer * ExecuteSQL(GDALDataset *gdalDataSet, const std::string &query)
Executes the given query in the given dataSet. Throws exception in case of error. It also handles FID...
TEOGREXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry.
TEOGREXPORT OGRSpatialReference * Convert2OGRProjection(int srid)
It converts the TerraLib Projection to OGR Projection.
TEOGREXPORT std::string GetOGRConnectionInfo(const std::map< std::string, std::string > &connInfo)
bool InitValidator(te::da::FieldNameValidator &validator, OGRFeatureDefn *ogrFeatureDefinition)
TEOGREXPORT GDALDataset * CreateGDALDataSet(const std::string &pszFilename, unsigned int nOpenFlags)
Creates a GDALDataSet based on the given fileName and open flags. The main purpose of this function i...
TEOGREXPORT std::string RemoveSpatialSql(const std::string &sql)
TEOGREXPORT OGRGeomFieldDefn * Convert2OGRGeom(te::dt::Property *p)
It converts the TerraLib Geometric Property Type to OGR Geometric Field Definition.
void OGRErrorHandler(CPLErr eErrClass, int errNo, const char *msg)
std::string GetDriverName(const std::string &path)
It tries extract the driver name used by OGR Library based on the given path.
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
TEOGREXPORT te::da::DataSetType * GetDataSetType(GDALDataset *gdalDataSet, OGRLayer *ogrLayer, int srs=TE_UNKNOWN_SRS)
It converts the OGR Feature Definition to TerraLib DataSet Type. It also handles FID and Geometry pro...
TerraLib.
#define TEOGREXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:79
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:43
Proxy configuration file for TerraView (see terraview_config.h).