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) 2001-2009 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/gdal/Utils.h
22 
23  \brief Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
24  */
25 
26 #ifndef __TERRALIB_GDAL_INTERNAL_UTILS_H
27 #define __TERRALIB_GDAL_INTERNAL_UTILS_H
28 
29 // TerraLib
30 #include "../raster/BandProperty.h"
31 #include "../raster/Enums.h"
32 #include "../raster/Raster.h"
33 #include "Config.h"
34 #include "Band.h"
35 #include "Raster.h"
36 
37 // GDAL
38 #include <gdal_priv.h>
39 
40 // STL
41 #include <map>
42 #include <string>
43 
44 #include <boost/filesystem.hpp>
45 #include <boost/thread/mutex.hpp>
46 
47 namespace te
48 {
49  namespace gm {class Envelope; class Geometry;}
50 
51  namespace rst {class Grid; class RasterProperty; }
52 
53  namespace gdal
54  {
55  /*!
56  \brief It translates a GDAL DataType to a TerraLib DataType.
57  */
58  inline int GetTeDataType(GDALDataType gt)
59  {
60  switch(gt)
61  {
62  case GDT_Byte : return te::dt::UCHAR_TYPE;
63  case GDT_UInt16 : return te::dt::UINT16_TYPE;
64  case GDT_Int16 : return te::dt::INT16_TYPE;
65  case GDT_UInt32 : return te::dt::UINT32_TYPE;
66  case GDT_Int32 : return te::dt::INT32_TYPE;
67  case GDT_Float32 : return te::dt::FLOAT_TYPE;
68  case GDT_Float64 : return te::dt::DOUBLE_TYPE;
69 
70  case GDT_CInt16 : return te::dt::CINT16_TYPE;
71  case GDT_CInt32 : return te::dt::CINT32_TYPE;
72  case GDT_CFloat32 : return te::dt::CFLOAT_TYPE;
73  case GDT_CFloat64 : return te::dt::CDOUBLE_TYPE;
74 
75  default : return te::dt::UNKNOWN_TYPE;
76  }
77  }
78 
79  /*!
80  \brief It translates a TerraLib DataType to a GDAL DataType.
81  */
82  inline GDALDataType GetGDALDataType(int tet)
83  {
84  switch(tet)
85  {
86  case te::dt::UCHAR_TYPE : return GDT_Byte;
87  case te::dt::CHAR_TYPE : return GDT_Byte;
88  case te::dt::UINT16_TYPE : return GDT_UInt16;
89  case te::dt::INT16_TYPE : return GDT_Int16;
90  case te::dt::UINT32_TYPE : return GDT_UInt32;
91  case te::dt::INT32_TYPE : return GDT_Int32;
92  case te::dt::FLOAT_TYPE : return GDT_Float32;
93  case te::dt::DOUBLE_TYPE : return GDT_Float64;
94 
95  case te::dt::CINT16_TYPE : return GDT_CInt16;
96  case te::dt::CINT32_TYPE : return GDT_CInt32;
97  case te::dt::CFLOAT_TYPE : return GDT_CFloat32;
98  case te::dt::CDOUBLE_TYPE : return GDT_CFloat64;
99 
100  default : return GDT_Unknown;
101  }
102  }
103 
104  /*!
105  \brief It translates a GDAL ColorInterpretation to a TerraLib ColorInterpretation.
106  */
107  inline te::rst::ColorInterp GetTeColorInterpretation(GDALColorInterp gci)
108  {
109  switch(gci)
110  {
111  case GCI_GrayIndex : return te::rst::GrayIdxCInt;
112  case GCI_PaletteIndex : return te::rst::PaletteIdxCInt;
113  case GCI_RedBand : return te::rst::RedCInt;
114  case GCI_GreenBand : return te::rst::GreenCInt;
115  case GCI_BlueBand : return te::rst::BlueCInt;
116  case GCI_AlphaBand : return te::rst::AlphaCInt;
117  case GCI_HueBand : return te::rst::HueCInt;
118  case GCI_SaturationBand : return te::rst::SatCInt;
119  case GCI_LightnessBand : return te::rst::LigCInt;
120  case GCI_CyanBand : return te::rst::CyanCInt;
121  case GCI_MagentaBand : return te::rst::MagentaCInt;
122  case GCI_YellowBand : return te::rst::YellowCInt;
123  case GCI_BlackBand : return te::rst::KeyCInt;
124  case GCI_YCbCr_YBand : return te::rst::YCInt;
125  case GCI_YCbCr_CbBand : return te::rst::CbCInt;
126  case GCI_YCbCr_CrBand : return te::rst::CrCInt;
127  default : return te::rst::UndefCInt;
128  }
129  }
130 
131  /*!
132  \brief It translates a TerraLib ColorInterpretation to a GDAL ColorInterpretation.
133  */
135  {
136  switch(ci)
137  {
138  case te::rst::GrayIdxCInt : return GCI_GrayIndex;
139  case te::rst::PaletteIdxCInt : return GCI_PaletteIndex;
140  case te::rst::RedCInt : return GCI_RedBand;
141  case te::rst::GreenCInt : return GCI_GreenBand;
142  case te::rst::BlueCInt : return GCI_BlueBand;
143  case te::rst::AlphaCInt : return GCI_AlphaBand;
144  case te::rst::HueCInt : return GCI_HueBand;
145  case te::rst::SatCInt : return GCI_SaturationBand;
146  case te::rst::LigCInt : return GCI_LightnessBand;
147  case te::rst::CyanCInt : return GCI_CyanBand;
148  case te::rst::MagentaCInt : return GCI_MagentaBand;
149  case te::rst::YellowCInt : return GCI_YellowBand;
150  case te::rst::KeyCInt : return GCI_BlackBand;
151  case te::rst::YCInt : return GCI_YCbCr_YBand;
152  case te::rst::CbCInt : return GCI_YCbCr_CbBand;
153  case te::rst::CrCInt : return GCI_YCbCr_CrBand;
154  default : return GCI_Undefined;
155  }
156  }
157 
158  /*!
159  \brief It translates a GDAL Pallete Interpretation to a TerraLib Pallete Interpretation.
160  */
162  {
163  switch (gpi)
164  {
165  case GPI_Gray : return te::rst::GrayPalInt;
166  case GPI_RGB : return te::rst::RGBPalInt;
167  case GPI_CMYK : return te::rst::CMYKPalInt;
168  case GPI_HLS : return te::rst::HSLPalInt;
169  default : return te::rst::UndefPalInt;
170  }
171  }
172  /*!
173  \brief Gets the grid definition from a GDAL dataset.
174  \param gds A pointer to a GDAL dataset.
175  \return A pointer to the grid definition from a GDAL dataset. Caller takes its ownership.
176  */
177  TEGDALEXPORT te::rst::Grid* GetGrid(GDALDataset* gds);
178 
179  /*!
180  \brief Gets the list of bands definition from a GDAL dataset.
181  \param gds A pointer to a GDAL dataset.
182  \param bprops A reference to a vector to be filled with the bands description extracted from a dataset.
183  \note The caller of this method must take the ownership of the returned properties.
184  */
185  TEGDALEXPORT void GetBandProperties(GDALDataset* gds, std::vector<te::rst::BandProperty*>& bprops);
186 
187  /*!
188  \brief Gets the properties of a single band from a GDAL dataset.
189 
190  \param gband A pointer to a GDAL Raster Band.
191 
192  \param bandIndex The band index (starting from 0).
193 
194  \return A band property.
195 
196  \note The caller of this method must take the ownership of the returned properties.
197  */
198  te::rst::BandProperty* GetBandProperty(GDALRasterBand* gband, const unsigned int bandIndex );
199 
200  /*!
201  \brief Gets the list of bands from a GDAL dataset.
202 
203  \param rst A pointer to the raster.
204  \param bands A reference to a vector to be filled with the bands extracted from a dataset.
205  \note The caller of this method must take the ownership of the returned properties.
206  */
207  void GetBands(te::gdal::Raster* rst, std::vector<te::gdal::Band*>& bands);
208 
209  /*!
210  \brief Gets the complete description from a GDAL dataset.
211  \param strAccessInfo A a string to be used by GDAL to access the raster.
212  \return A pointer to the raster description from a GDAL dataset. Caller takes its ownership.
213  */
214  te::rst::RasterProperty* GetRasterProperty(std::string strAccessInfo);
215 
216  /*!
217  \brief Creates a raster data using GDAL.
218 
219  \param g Raster grid info.
220  \param bands Band info.
221  \param optParams A vector of optional parameters that are valid only for some data formats.
222 
223  \return A pointer to a GDALDataset if it succeeds or a NULL pointer otherwise. Caller is responsible for closing it.
224 
225  \exception Exception It throws an exception if the raster can not be created.
226  */
227  GDALDataset* CreateRaster(te::rst::Grid* g, const std::vector<te::rst::BandProperty*>& bands, const std::map<std::string, std::string>& optParams);
228 
229  /*!
230  \brief Creates a raster data using GDAL.
231  \param name The name of the dataset to create. UTF-8 encoded.
232  \param g Raster grid info.
233  \param bands Band info.
234  \param optParams A vector of optional parameters that are valid only for some data formats.
235 
236  \return A pointer to a GDALDataset if it succeeds or a NULL pointer otherwise. Caller is responsible for closing it.
237 
238  \exception Exception It throws an exception if the raster can not be created.
239  */
240  GDALDataset* CreateRaster(const std::string& name, te::rst::Grid* g, const std::vector<te::rst::BandProperty*>& bands, const std::map<std::string, std::string>& optParams);
241 
242  /*!
243  \brief Gets the extent of a raster data decoded by GDAL.
244  \param strAccessInfo A a string to be used by GDAL to access the raster.
245  \return A pointer to raster extent. Caller takes its ownership.
246  */
247  te::gm::Envelope* GetExtent(std::string strAccessInfo);
248 
249  /*!
250  \brief Get a handle to a raster file.
251  \param filename File name (path included).
252  \param policy The access permission requested.
253  \return A pointer to GDAL dataset if it succeeds or a NULL pointer otherwise.
254  */
255  GDALDataset* GetRasterHandle(std::string strAccessInfo, te::common::AccessPolicy policy = te::common::RAccess);
256 
257  /*!
258  \brief Returns a PostGIS connection string from the set connection information.
259  The connection string is to be used as a dataset name in GDAL data model. See also http://trac.osgeo.org/gdal/wiki/frmts_wtkraster.html.
260  \param connInfo The connection parameters.
261  \return Returns a PostGIS connection string from the set connection information.
262  */
263  std::string MakePGConnectionStr(const std::map<std::string, std::string>& dsInfo);
264 
265  /*!
266  \brief Reprojects a raster to another SRS.
267  \param rin The input raster file. Do not pass a null pointer.
268  \param rout The new output raster. Do not pass a null pointer.
269  \return true If the reprojection was done or false otherwise.
270  */
271  bool ReprojectRaster(te::rst::Raster const * const rin, te::rst::Raster* rout);
272 
273  /*!
274  \brief It returns the GDAL driver name associated to a data source name.
275 
276  \param dsName the name of the file that represents the data source.
277 
278  \return the GDAL driver name, or its identifier if succeeds and a null string otherwise.
279  */
280  std::string GetDriverName(const std::string& dsName);
281 
282  /*!
283  \brief It returns a GDAL connection string from the given map.
284 
285  \param connInfo An associative conteiner with data source connection info.
286 
287  \return a GDAL connection string from the given map.
288 
289  \exception Exception It throws an exception if no connection info exists in the input map.
290  */
291  std::string GetGDALConnectionInfo(const std::map<std::string, std::string>& connInfo);
292 
293  /*!
294  \brief It returns the Sub DataSet name from the given name or the same name.
295 
296  \param name The Full SubDataSet string name.
297 
298  \param driverName The driver name.
299 
300  \return The Sub DataSet name from the given name.
301  */
302  std::string GetSubDataSetName(const std::string& name, const std::string& driverName);
303 
304  /*!
305  \brief It returns true if GDAL recognizes the given SRS id.
306 
307  \param srid The SRS identifier.
308 
309  \return true if GDAL recognizes the given SRS id or false otherwise.
310  */
311  bool RecognizesSRID(unsigned int srid);
312 
313  /*!
314  \brief Vectorizes a given raster band, using GDALPolygonize function.
315 
316  \param band The band to vectorize.
317  \param geometries A reference to a vector of geometries. Will be filled with geometries found in band.
318  */
319  TEGDALEXPORT void Vectorize(GDALRasterBand* band, std::vector<te::gm::Geometry*>& geometries);
320 
321  /*!
322  \brief Rasterizes a given vector of geometries, using GDALRasterizeGeometries function.
323 
324  \param geometries A vector of geometries to be rasterized.
325  \param outraster A reference to the GDAL dataset where the rasterized geometries will be drawn.
326  */
327  TEGDALEXPORT void Rasterize(std::vector<te::gm::Geometry*> geometries, GDALDataset* outraster);
328 
329  /*!
330  \brief Returns true if the given URI is related to a sub-dataset.
331  \param uri The given URI.
332  \return true if the given URI is related to a sub-dataset.
333  */
334  bool IsSubDataSet( const std::string& uri );
335 
336  /*!
337  \brief It returns the parent dataset name from a Sub DataSet name.
338  \param subDataSetName The Full SubDataSet string name.
339  \return the parent dataset name from a Sub DataSet name.
340  \note If the given name does not refers to a sub-dataset it will be returned.
341  */
342  std::string GetParentDataSetName(const std::string& subDataSetName);
343 
344  /*!
345  \brief Returns a reference to a static mutex initialized when this module is initialized.
346  \return Returns a reference to a static mutex initialized when this module is initialized.
347  */
348  TEGDALEXPORT boost::mutex& getStaticMutex();
349 
350  } // end namespace gdal
351 } // end namespace te
352 #endif
GDALColorInterp GetGDALColorInterpretation(te::rst::ColorInterp ci)
It translates a TerraLib ColorInterpretation to a GDAL ColorInterpretation.
Definition: Utils.h:134
TEGDALEXPORT void Vectorize(GDALRasterBand *band, std::vector< te::gm::Geometry * > &geometries)
Vectorizes a given raster band, using GDALPolygonize function.
Definition: Utils.cpp:699
HSL indexed palette interpretation.
Definition: Enums.h:86
Palette indexes color interpretation.
Definition: Enums.h:58
This is a class that represents a GDAL Raster.
GDALDataType GetGDALDataType(int tet)
It translates a TerraLib DataType to a GDAL DataType.
Definition: Utils.h:82
Hue channel color interpretation.
Definition: Enums.h:63
te::rst::PaletteInterpretation GetTePaletteInterpretation(GDALPaletteInterp gpi)
It translates a GDAL Pallete Interpretation to a TerraLib Pallete Interpretation. ...
Definition: Utils.h:161
Alpha channel color interpretation.
Definition: Enums.h:62
void GetBands(te::gdal::Raster *rst, std::vector< te::gdal::Band * > &bands)
Gets the list of bands from a GDAL dataset.
Definition: Utils.cpp:312
Index into a lookup table.
Definition: Enums.h:57
Lightness color interpretation.
Definition: Enums.h:65
te::rst::ColorInterp GetTeColorInterpretation(GDALColorInterp gci)
It translates a GDAL ColorInterpretation to a TerraLib ColorInterpretation.
Definition: Utils.h:107
TEGDALEXPORT void GetBandProperties(GDALDataset *gds, std::vector< te::rst::BandProperty * > &bprops)
Gets the list of bands definition from a GDAL dataset.
Definition: Utils.cpp:152
ColorInterp
Color model component use.
Definition: Enums.h:54
A raster band description.
Definition: BandProperty.h:61
RGB indexed palette interpretation.
Definition: Enums.h:84
This class represents Raster data.
Definition: Raster.h:59
Gray indexed palette interpretation.
Definition: Enums.h:83
It gives access to values in one band (dimension) of a raster.
#define TEGDALEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:69
Red channel color interpretation.
Definition: Enums.h:59
te::rst::BandProperty * GetBandProperty(GDALRasterBand *gband, const unsigned int bandIndex)
Gets the properties of a single band from a GDAL dataset.
Definition: Utils.cpp:202
Cyan color interpretation.
Definition: Enums.h:66
Raster property.
int GetTeDataType(GDALDataType gt)
It translates a GDAL DataType to a TerraLib DataType.
Definition: Utils.h:58
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
GDALDataset * CreateRaster(te::rst::Grid *g, const std::vector< te::rst::BandProperty * > &bands, const std::map< std::string, std::string > &optParams)
Creates a raster data using GDAL.
Definition: Utils.cpp:516
GDALDataset * GetRasterHandle(std::string strAccessInfo, te::common::AccessPolicy policy=te::common::RAccess)
Get a handle to a raster file.
Definition: Utils.cpp:527
te::gm::Envelope * GetExtent(std::string strAccessInfo)
Gets the extent of a raster data decoded by GDAL.
Definition: Utils.cpp:377
YCbCr Cr Band color interpretation.
Definition: Enums.h:72
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
An abstract class for raster data strucutures.
Definition: Raster.h:71
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name.
Definition: Utils.cpp:769
No color interpretation is associated with the band.
Definition: Enums.h:56
Undefined palette interpretation.
Definition: Enums.h:82
Key (black) color interpretation.
Definition: Enums.h:69
Yellow color interpretation.
Definition: Enums.h:68
std::string GetDriverName(const std::string &dsName)
It returns the GDAL driver name associated to a data source name.
Definition: Utils.cpp:653
bool IsSubDataSet(const std::string &uri)
Returns true if the given URI is related to a sub-dataset.
Definition: Utils.cpp:746
bool ReprojectRaster(te::rst::Raster const *const rin, te::rst::Raster *rout)
Reprojects a raster to another SRS.
Definition: Utils.cpp:604
bool RecognizesSRID(unsigned int srid)
It returns true if GDAL recognizes the given SRS id.
Definition: Utils.cpp:593
std::string GetGDALConnectionInfo(const std::map< std::string, std::string > &connInfo)
It returns a GDAL connection string from the given map.
Definition: Utils.cpp:684
std::string MakePGConnectionStr(const std::map< std::string, std::string > &dsInfo)
Returns a PostGIS connection string from the set connection information. The connection string is to ...
Definition: Utils.cpp:539
YCbCr Y Band color interpretation.
Definition: Enums.h:70
TEGDALEXPORT void Rasterize(std::vector< te::gm::Geometry * > geometries, GDALDataset *outraster)
Rasterizes a given vector of geometries, using GDALRasterizeGeometries function.
Definition: Utils.cpp:721
CMYK indexed palette interpretation.
Definition: Enums.h:85
Saturation color interpretation.
Definition: Enums.h:64
TEGDALEXPORT boost::mutex & getStaticMutex()
Returns a reference to a static mutex initialized when this module is initialized.
Definition: Utils.cpp:799
Blue channel color interpretation.
Definition: Enums.h:61
PaletteInterpretation
Palette interpratation types.
Definition: Enums.h:80
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
std::string GetSubDataSetName(const std::string &name, const std::string &driverName)
It returns the Sub DataSet name from the given name or the same name.
Definition: Utils.cpp:61
Green channel color interpretation.
Definition: Enums.h:60
TEGDALEXPORT te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset.
Definition: Utils.cpp:102
Magenta color interpretation.
Definition: Enums.h:67
YCbCr Cb Band color interpretation.
Definition: Enums.h:71
Configuration flags for the GDAL Driver of TerraLib.
te::rst::RasterProperty * GetRasterProperty(std::string strAccessInfo)
Gets the complete description from a GDAL dataset.
Definition: Utils.cpp:356