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