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