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