All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.cpp
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.cpp
22 
23  \brief Utility functions for GDAL.
24 */
25 
26 // TerraLib
27 #include "../common/StringUtils.h"
28 #include "../common/Translator.h"
29 #include "../common/UnitOfMeasure.h"
30 #include "../common/UnitsOfMeasureManager.h"
31 #include "../dataaccess/dataset/DataSetType.h"
32 #include "../geometry/Coord2D.h"
33 #include "../geometry/Envelope.h"
34 #include "../geometry/Geometry.h"
35 #include "../ogr/Utils.h"
36 #include "../raster/BandProperty.h"
37 #include "../raster/Grid.h"
38 #include "../raster/RasterFactory.h"
39 #include "../raster/RasterProperty.h"
40 #include "Band.h"
41 #include "Exception.h"
42 #include "Raster.h"
43 #include "Utils.h"
44 
45 // STL
46 #include <cmath>
47 #include <vector>
48 
49 // GDAL
50 #include <gdalwarper.h>
51 #include <ogr_api.h>
52 #include <ogr_spatialref.h>
53 #include <ogrsf_frmts.h>
54 
55 // Boost
56 #include <boost/algorithm/string.hpp>
57 #include <boost/format.hpp>
58 #include <boost/filesystem/path.hpp>
59 #include <boost/filesystem/operations.hpp>
60 
61 std::string te::gdal::GetSubDataSetName(const std::string& name, const std::string& driverName)
62 {
63  std::vector<std::string> words;
64  boost::split(words, name, boost::is_any_of(":"), boost::token_compress_on);
65 
66  if (words.size() < 3)
67  return name;
68 
69  std::string sdname;
70 
71  if (driverName == "HDF4")
72  {
73  // HDF4_SDS:subdataset_type:file_name:subdataset_index
74  sdname = words[0] + ":" + words[1] + ":" + words[3];
75  if (words.size()>4)
76  sdname = sdname + ":" + words[4];
77  return sdname;
78  }
79  else if (driverName == "NITF")
80  {
81  // NITF_IM:image_index:file_name
82 
83  sdname = words[0] + ":" + words[1];
84  }
85  else if (driverName == "netCDF")
86  {
87  // NETCDF:filename:variable_name
88  sdname = words[0] + ":" + words[2];
89  }
90  else
91  {
92  // From GDAL documentation: "Currently, drivers which support subdatasets are: ADRG, ECRGTOC, GEORASTER,
93  // GTiff, HDF4, HDF5, netCDF, NITF, NTv2, OGDI, PDF, PostGISRaster, Rasterlite,
94  // RPFTOC, RS2, WCS, and WMS.". It seems that the default format is FORMAT:variable:file_name
95  for (size_t i=0; i<words.size()-1;++i)
96  sdname = sdname + ":" + words[i];
97  }
98 
99  return sdname;
100 }
101 
103 {
104  if (!gds)
105  return 0;
106 
107  int srid = TE_UNKNOWN_SRS;
108 
109  // The calling of GetProjectionRef isn't thread safe, even for distinct datasets
110  // under some linuxes
111  boost::unique_lock< boost::mutex > lockGuard( getStaticMutex() );
112  char* projWKT = (char*)gds->GetProjectionRef();
113  lockGuard.release();
114  getStaticMutex().unlock();
115 
116  if (projWKT)
117  {
118  char** projWKTPtr = &(projWKT);
119  OGRSpatialReference oSRS;
120  oSRS.importFromWkt( projWKTPtr );
121  oSRS.AutoIdentifyEPSG();
122  const char* srsAuth = oSRS.GetAuthorityCode(0);
123  if (srsAuth)
124  srid = atoi(srsAuth);
125  }
126 
127  double gtp[6];
128 
129  te::rst::Grid* grid = 0;
130 
131  if( gds->GetGeoTransform(gtp) == CE_Failure )
132  {
133  grid = new te::rst::Grid(gds->GetRasterXSize(), gds->GetRasterYSize(), 1.0, 1.0, (te::gm::Envelope*)0, srid);
134  }
135  else
136  {
137  double gridAffineParams[ 6 ];
138  gridAffineParams[ 0 ] = gtp[ 1 ];
139  gridAffineParams[ 1 ] = gtp[ 2 ];
140  gridAffineParams[ 2 ] = gtp[ 0 ];
141  gridAffineParams[ 3 ] = gtp[ 4 ];
142  gridAffineParams[ 4 ] = gtp[ 5 ];
143  gridAffineParams[ 5 ] = gtp[ 3 ];
144 
145  grid = new te::rst::Grid(gridAffineParams, gds->GetRasterXSize(),
146  gds->GetRasterYSize(), srid);
147  }
148 
149  return grid;
150 }
151 
152 void te::gdal::GetBandProperties(GDALDataset* gds, std::vector<te::rst::BandProperty*>& bprops)
153 {
154  if (!gds)
155  return;
156 
157  bprops.clear();
158 
159  int nBands = 0;
160  bool hasPalette = false;
161 
162  if( gds->GetRasterCount() > 0 )
163  {
164  if( gds->GetRasterBand(1)->GetColorInterpretation() == GCI_PaletteIndex )
165  {
166  hasPalette = true;
167  assert( gds->GetRasterBand(1)->GetColorTable() );
168 
169  switch( gds->GetRasterBand(1)->GetColorTable()->GetPaletteInterpretation() )
170  {
171  case GPI_Gray :
172  nBands = 1;
173  break;
174  case GPI_RGB : // RGBA
175  nBands = 4;
176  break;
177  case GPI_CMYK :
178  nBands = 4;
179  break;
180  case GPI_HLS :
181  nBands = 3;
182  break;
183  default :
184  throw Exception(TE_TR("invalid palette interpretation"));
185  break;
186  }
187  }
188  else
189  {
190  nBands = gds->GetRasterCount();
191  }
192  }
193 
194  // retrieve the information about each band
195  for (int rasterIdx = 0; rasterIdx < nBands ; ++rasterIdx)
196  {
197  GDALRasterBand* rasterBand = gds->GetRasterBand( hasPalette ? 1 : ( 1 + rasterIdx) );
198  bprops.push_back( GetBandProperty( rasterBand, rasterIdx ) );
199  }
200 }
201 
203  const unsigned int bandIndex )
204 {
205  if (!gband)
206  return 0;
207 
208  te::rst::BandProperty* bprop = new te::rst::BandProperty(gband->GetBand()-1, GetTeDataType(gband->GetRasterDataType()), "");
209 
210  bprop->m_idx = bandIndex;
211 
212  // te::rst::BandProperty* rb = new te::rst::BandProperty(rasterIdx, dt);
213  bprop->m_colorInterp = GetTeColorInterpretation(gband->GetColorInterpretation());
214 
215  if( bprop->m_colorInterp == te::rst::PaletteIdxCInt )
216  {
217  switch( gband->GetColorTable()->GetPaletteInterpretation() )
218  {
219  case GPI_Gray :
220  switch( bandIndex )
221  {
222  case 0 :
224  break;
225  default :
226  throw Exception(TE_TR("invalid band index"));
227  break;
228  }
229  break;
230  case GPI_RGB : // RGBA
231  switch( bandIndex )
232  {
233  case 0 :
235  break;
236  case 1 :
238  break;
239  case 2 :
241  break;
242  case 3 :
244  break;
245  default :
246  throw Exception(TE_TR("invalid band index"));
247  break;
248  }
249  break;
250  case GPI_CMYK :
251  switch( bandIndex )
252  {
253  case 0 :
255  break;
256  case 1 :
258  break;
259  case 2 :
261  break;
262  case 3 :
264  break;
265  default :
266  throw Exception(TE_TR("invalid band index"));
267  break;
268  }
269  break;
270  case GPI_HLS :
271  switch( bandIndex )
272  {
273  case 0 :
275  break;
276  case 1 :
278  break;
279  case 2 :
281  break;
282  default :
283  throw Exception(TE_TR("invalid band index"));
284  break;
285  }
286  break;
287  default :
288  throw Exception(TE_TR("invalid palette interpretation"));
289  break;
290  }
291  }
292 
293  // find if there is a no data value
294  int noDataValueIsUsed = 0;
295  double nodataval = gband->GetNoDataValue(&noDataValueIsUsed);
296  if (noDataValueIsUsed)
297  bprop->m_noDataValue = nodataval;
298 
299  std::string unitName = gband->GetUnitType();
300  if (!unitName.empty())
302  bprop->m_valuesOffset = gband->GetOffset(0);
303  bprop->m_valuesScale = gband->GetScale(0);
304 
305  gband->GetBlockSize(&bprop->m_blkw, &bprop->m_blkh);
306  bprop->m_nblocksx = (gband->GetXSize() + bprop->m_blkw - 1) / bprop->m_blkw;
307  bprop->m_nblocksy = (gband->GetYSize() + bprop->m_blkh - 1) / bprop->m_blkh;
308 
309  return bprop;
310 }
311 
312 void te::gdal::GetBands(te::gdal::Raster* rst, std::vector<te::gdal::Band*>& bands)
313 {
314  bands.clear();
315 
316  int nBands = 0;
317 
318  if( rst->getGDALDataset()->GetRasterCount() > 0 )
319  {
320  if( rst->getGDALDataset()->GetRasterBand(1)->GetColorInterpretation() == GCI_PaletteIndex )
321  {
322  if( rst->getGDALDataset()->GetRasterBand(1)->GetColorTable() == 0 )
323  {
324  throw Exception(TE_TR("invalid color table"));
325  }
326 
327  switch( rst->getGDALDataset()->GetRasterBand(1)->GetColorTable()->GetPaletteInterpretation() )
328  {
329  case GPI_Gray :
330  nBands = 1;
331  break;
332  case GPI_RGB : // RGBA
333  nBands = 4;
334  break;
335  case GPI_CMYK :
336  nBands = 4;
337  break;
338  case GPI_HLS :
339  nBands = 3;
340  break;
341  default :
342  throw Exception(TE_TR("invalid palette interpretation"));
343  break;
344  }
345  }
346  else
347  {
348  nBands = rst->getGDALDataset()->GetRasterCount();
349  }
350  }
351 
352  for (int b = 0; b < nBands; b++)
353  bands.push_back( new te::gdal::Band(rst, b) );
354 }
355 
357 {
358  GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), GA_ReadOnly);
359  if (!gds)
360  return 0;
361 
362  te::rst::Grid* grid = GetGrid(gds);
363 
364  std::vector<te::rst::BandProperty*> bprops;
365 
366  te::gdal::GetBandProperties(gds, bprops);
367 
368  std::map<std::string, std::string> rinfo;
369 
370  te::rst::RasterProperty* rp = new te::rst::RasterProperty(grid, bprops, rinfo);
371 
372  GDALClose(gds);
373 
374  return rp;
375 }
376 
377 te::gm::Envelope* te::gdal::GetExtent(std::string strAccessInfo)
378 {
379  GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), GA_ReadOnly);
380  if (!gds)
381  return 0;
382  te::rst::Grid* grid = te::gdal::GetGrid(gds);
383  GDALClose(gds);
384  te::gm::Envelope* ext = new te::gm::Envelope(*grid->getExtent());
385  delete grid;
386  return ext;
387 }
388 
389 GDALDataset* te::gdal::CreateRaster(const std::string& name, te::rst::Grid* g, const std::vector<te::rst::BandProperty*>& bands,
390  const std::map<std::string, std::string>& optParams)
391 {
392  std::string driverName = GetDriverName(name);
393 
394  GDALDriverManager* dManPtr = GetGDALDriverManager();
395 
396  GDALDriver* driverPtr = dManPtr->GetDriverByName(driverName.c_str());
397 
398  if(!driverPtr)
399  throw Exception("Could not create raster because the underlying driver was not found!");
400 
401  GDALDataType targetGDataType = te::gdal::GetGDALDataType(bands[0]->m_type);
402 
403  if (targetGDataType == GDT_Unknown)
404  throw Exception("Could not create raster because of unknown band data type!");
405 
406  char** papszOptions = 0;
407 
408  std::map<std::string, std::string>::const_iterator it = optParams.begin();
409 
410  std::map<std::string, std::string>::const_iterator it_end = optParams.end();
411 
412  while(it != it_end)
413  {
414  if(it->first == "URI" || it->first == "SOURCE")
415  {
416  ++it;
417 
418  continue;
419  }
420 
421  papszOptions = CSLSetNameValue(papszOptions, it->first.c_str(), it->second.c_str());
422 
423  ++it;
424  }
425 
426  GDALDataset* poDataset;
427 
428  if (driverName == "JPEG" || driverName == "PNG")
429  {
430  GDALDriver* tmpDriverPtr = dManPtr->GetDriverByName("MEM");
431 
432  poDataset = tmpDriverPtr->Create(name.c_str(),
433  g->getNumberOfColumns(),
434  g->getNumberOfRows(),
435  bands.size(),
436  targetGDataType,
437  papszOptions);
438  }
439  else
440  poDataset = driverPtr->Create(name.c_str(),
441  g->getNumberOfColumns(),
442  g->getNumberOfRows(),
443  bands.size(),
444  targetGDataType,
445  papszOptions);
446 
447  if(papszOptions)
448  CSLDestroy(papszOptions);
449 
450  if(poDataset == 0)
451  throw Exception("Could not create raster!");
452 
453  const double* cgt = g->getGeoreference();
454 
455  double gt[6];
456 
457  gt[0] = cgt[2];
458  gt[1] = cgt[0];
459  gt[2] = cgt[1];
460  gt[3] = cgt[5];
461  gt[4] = cgt[3];
462  gt[5] = cgt[4];
463 
464  poDataset->SetGeoTransform(gt);
465 
466  OGRSpatialReference oSRS;
467 
468  oSRS.importFromEPSG(g->getSRID());
469 
470  char* projWKTPtr = 0;
471 
472  if(oSRS.exportToWkt(&projWKTPtr) == OGRERR_NONE)
473  {
474  poDataset->SetProjection(projWKTPtr);
475  OGRFree(projWKTPtr);
476  }
477 
478  int nb = static_cast<int>(bands.size());
479 
480  for(int dIdx = 0; dIdx < nb; ++dIdx)
481  {
482  GDALRasterBand* rasterBand = poDataset->GetRasterBand(1 + dIdx);
483 
484  GDALColorInterp ci = te::gdal::GetGDALColorInterpretation(bands[dIdx]->m_colorInterp);
485 
486  rasterBand->SetColorInterpretation(ci);
487 
488  if (ci == GCI_PaletteIndex)
489  {
490  GDALColorTable* gCTablePtr = new GDALColorTable(GPI_RGB);
491  GDALColorEntry gCEntry;
492 
493  for (unsigned int pIdx=0 ; pIdx < bands[dIdx]->m_palette.size(); ++pIdx)
494  {
495  gCEntry.c1 = (short)bands[dIdx]->m_palette[pIdx].c1;
496  gCEntry.c2 = (short)bands[dIdx]->m_palette[pIdx].c2;
497  gCEntry.c3 = (short)bands[dIdx]->m_palette[pIdx].c3;
498  gCEntry.c4 = (short)bands[dIdx]->m_palette[pIdx].c4;
499  gCTablePtr->SetColorEntry(pIdx, &gCEntry);
500  }
501 
502  rasterBand->SetColorTable(gCTablePtr);
503  delete gCTablePtr;
504  }
505  rasterBand->SetNoDataValue(bands[dIdx]->m_noDataValue);
506  rasterBand->SetOffset(bands[dIdx]->m_valuesOffset.real());
507  rasterBand->SetScale(bands[dIdx]->m_valuesScale.real());
508 
509  // maybe there is something else here...
510  }
511 
512  return poDataset;
513 
514 }
515 
517  const std::vector<te::rst::BandProperty*>& bands,
518  const std::map<std::string, std::string>& optParams)
519 {
520  assert(g);
521  assert(bands.size() > 0);
522 
523  std::string accessInfo = GetGDALConnectionInfo(optParams);
524  return te::gdal::CreateRaster(accessInfo, g, bands, optParams);
525 }
526 
527 GDALDataset* te::gdal::GetRasterHandle(const std::string strAccessInfo, te::common::AccessPolicy policy)
528 {
529  GDALAccess gpolicy = GA_ReadOnly;
530 
531  if(policy == te::common::WAccess || policy == te::common::RWAccess)
532  gpolicy = GA_Update;
533 
534  GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), gpolicy);
535 
536  return gds;
537 }
538 
539 std::string te::gdal::MakePGConnectionStr(const std::map<std::string, std::string>& dsInfo)
540 {
541 /*PG":host='<host>' port:'<port>' dbname='<dbname>' user='<user>' password='<password>' [schema='<schema>'] [table='<raster_table>'] [where='<sql_where>'] [mode='<working_mode>']"*/
542 
543  std::map<std::string, std::string>::const_iterator it = dsInfo.find("host");
544  std::map<std::string, std::string>::const_iterator it_end = dsInfo.end();
545  std::string connInfo = "PG:";
546 
547  if(it != it_end)
548  connInfo += " host = " + it->second;
549 
550  it = dsInfo.find("port");
551 
552  if(it != it_end)
553  connInfo += " port = " + it->second;
554 
555  it = dsInfo.find("dbname");
556 
557  if(it != it_end)
558  connInfo += " dbname = " + it->second;
559 
560  it = dsInfo.find("user");
561 
562  if(it != it_end)
563  connInfo += " user = " + it->second;
564 
565  it = dsInfo.find("password");
566 
567  if(it != it_end)
568  connInfo += " password = " + it->second;
569 
570  it = dsInfo.find("schema");
571 
572  if(it != it_end)
573  connInfo += " schema = " + it->second;
574 
575  it = dsInfo.find("table");
576 
577  if(it != it_end)
578  connInfo += " table = " + it->second;
579 
580  it = dsInfo.find("where");
581 
582  if(it != it_end)
583  connInfo += " where = " + it->second;
584 
585  it = dsInfo.find("mode");
586 
587  if(it != it_end)
588  connInfo += " mode = " + it->second;
589 
590  return connInfo;
591 }
592 
593 bool te::gdal::RecognizesSRID(unsigned int srid)
594 {
595  OGRSpatialReference oSRS;
596  oSRS.importFromEPSG(srid);
597  char* coutWKT = NULL;
598  oSRS.exportToWkt(&coutWKT);
599  std::string outwkt(coutWKT);
600  return (!outwkt.empty());
601 }
602 
603 /* This function is based on the WARP tutorial in http://www.gdal.org */
605 {
606  assert(rin);
607  assert(rout);
608 
609  te::gdal::Raster const* grin = static_cast<te::gdal::Raster const *>(rin);
610  te::gdal::Raster* grout = static_cast<te::gdal::Raster*>(rout);
611 
612  GDALDatasetH hSrcDS = grin->getGDALDataset();
613  if (hSrcDS == 0)
614  return false;
615 
616  GDALDatasetH hDstDS = grout->getGDALDataset();
617  if (hDstDS == 0)
618  return false;
619 
620  int nBands = GDALGetRasterCount(hSrcDS);
621 
622  /* Define warp options */
623  GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();
624  psWarpOptions->hSrcDS = hSrcDS;
625  psWarpOptions->hDstDS = hDstDS;
626  psWarpOptions->nBandCount = nBands;
627  psWarpOptions->panSrcBands = (int*)CPLMalloc(sizeof(int)*psWarpOptions->nBandCount);
628  psWarpOptions->panDstBands = (int*)CPLMalloc(sizeof(int)*psWarpOptions->nBandCount);
629  for (int b = 0; b < psWarpOptions->nBandCount; b++)
630  {
631  psWarpOptions->panSrcBands[b] = b+1;
632  psWarpOptions->panDstBands[b] = b+1;
633  }
634 
635  /* Establish reprojection transformer */
636  psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer(hSrcDS, GDALGetProjectionRef(hSrcDS),
637  hDstDS, GDALGetProjectionRef(hDstDS),
638  FALSE, 0.0, 1);
639  psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
640 
641  /* Initialize and execute the warp operation */
642  GDALWarpOperation oOperation;
643  oOperation.Initialize(psWarpOptions);
644  oOperation.WarpRegion(0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS));
645 
646  /* Close transformer and images */
647  GDALDestroyGenImgProjTransformer(psWarpOptions->pTransformerArg);
648  GDALDestroyWarpOptions(psWarpOptions);
649 
650  return true;
651 }
652 
653 std::string te::gdal::GetDriverName(const std::string& name)
654 {
655 // check if it is a filename, and tries to use its extension
656  boost::filesystem::path mpath(name.c_str());
657 
658  std::string ext = te::common::Convert2UCase(mpath.extension().string());
659 
660  if(ext == ".TIF" || ext == ".TIFF")
661  return std::string("GTiff");
662 
663  if(ext == ".JPG")
664  return std::string("JPEG");
665 
666  if(ext == ".NTF")
667  return std::string("NITF");
668 
669  if(ext == ".NC")
670  return std::string("netCDF");
671 
672  if(ext == ".GRB")
673  return std::string("GRIB");
674 
675  if(ext == ".PNG")
676  return std::string("PNG");
677 
678  if(ext == ".HDF")
679  return std::string("HDF4");
680 
681  return "";
682 }
683 
684 std::string te::gdal::GetGDALConnectionInfo(const std::map<std::string, std::string>& connInfo)
685 {
686  std::map<std::string, std::string>::const_iterator it = connInfo.find("URI");
687 
688  if(it != connInfo.end())
689  return it->second;
690 
691  it = connInfo.find("SOURCE");
692 
693  if(it != connInfo.end())
694  return it->second;
695 
696  throw Exception(TE_TR("Invalid data source connection information!."));
697 }
698 
699 void te::gdal::Vectorize(GDALRasterBand* band, std::vector<te::gm::Geometry*>& geometries)
700 {
701 // create data source of geometries in memory
702  OGRSFDriver *ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory");
703 
704  OGRDataSource* ds = ogrDriver->CreateDataSource("ds_vectorize", NULL);
705 
706  OGRLayer* ogrLayer = ds->CreateLayer("vectorization", NULL, wkbMultiPolygon, NULL);
707 
708  ogrLayer->CreateField(new OGRFieldDefn("id", OFTInteger));
709 
710 // call polygonize function from gdal
711  if (GDALPolygonize(band, NULL , ogrLayer, 0, NULL, NULL, NULL) == CE_Failure)
712  return;
713 
714 // convert geometries to terralib
715  for (int g = 0; g < ogrLayer->GetFeatureCount(); g++)
716  geometries.push_back(te::ogr::Convert2TerraLib(ogrLayer->GetFeature(g)->GetGeometryRef()));
717 
718  OGRDataSource::DestroyDataSource(ds);
719 }
720 
721 void te::gdal::Rasterize(std::vector<te::gm::Geometry*> geometries, GDALDataset* outraster)
722 {
723 // define list of bands (using single band)
724  std::vector<int> bandList;
725 
726  bandList.push_back(1);
727 
728  std::vector<OGRGeometryH> ogrGeometries;
729 
730  std::vector<double> burnValues;
731 
732 // defining vector of ogr geometries and different values for each one in raster
733  int bvalue = 254;
734  for (std::size_t g = 0; g < geometries.size(); g++)
735  {
736  burnValues.push_back(bvalue % 255);
737 
738  bvalue = bvalue >= 127? bvalue - 126: bvalue > 255? 0: bvalue + 127;
739 
740  ogrGeometries.push_back(te::ogr::Convert2OGR(geometries[g]));
741  }
742 
743  GDALRasterizeGeometries(outraster, bandList.size(), &(bandList[0]), ogrGeometries.size(), &(ogrGeometries[0]), NULL, NULL, &(burnValues[0]), NULL, NULL, NULL);
744 }
745 
746 bool te::gdal::IsSubDataSet( const std::string& uri )
747 {
748  std::size_t firstIdx = uri.find( ":" );
749 
750  if( firstIdx < uri.size() )
751  {
752  std::size_t secondIdx = uri.find( ":", firstIdx + 1 );
753 
754  if( secondIdx < uri.size() )
755  {
756  return true;
757  }
758  else
759  {
760  return false;
761  }
762  }
763  else
764  {
765  return false;
766  }
767 }
768 
769 std::string te::gdal::GetParentDataSetName(const std::string& subDataSetName)
770 {
771  if( IsSubDataSet( subDataSetName ) )
772  {
773  std::size_t firstIdx = subDataSetName.find( ":" );
774 
775  if( firstIdx < subDataSetName.size() )
776  {
777  std::size_t secondIdx = subDataSetName.find( ":", firstIdx + 1 );
778 
779  if( secondIdx < subDataSetName.size() )
780  {
781  return subDataSetName.substr( secondIdx + 1, subDataSetName.size() - firstIdx - 1);
782  }
783  else
784  {
785  return subDataSetName;
786  }
787  }
788  else
789  {
790  return subDataSetName;
791  }
792  }
793  else
794  {
795  return subDataSetName;
796  }
797 }
798 
800 {
801  static boost::mutex getStaticMutexStaticMutex;
802  return getStaticMutexStaticMutex;
803 }
GDALDataset * getGDALDataset() const
Returns the raster GDAL handler.
Definition: Raster.cpp:250
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
unsigned int getNumberOfRows() const
Returns the grid number of rows.
Definition: Grid.cpp:209
Palette indexes color interpretation.
Definition: Enums.h:58
This is a class that represents a GDAL Raster.
std::size_t m_idx
The band index.
Definition: BandProperty.h:132
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
Alpha channel color interpretation.
Definition: Enums.h:62
TEOGREXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry.
Definition: Utils.cpp:79
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
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
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
A raster band description.
Definition: BandProperty.h:61
This class represents Raster data.
Definition: Raster.h:59
int getSRID() const
Returns the grid spatial reference system identifier.
Definition: Grid.cpp:265
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
It gives access to values in one band (dimension) of a raster.
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
void setUnitOfMeasure(te::common::UnitOfMeasurePtr u)
Sets the unit of measure of the values;.
Definition: BandProperty.h:125
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
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:163
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
Cyan color interpretation.
Definition: Enums.h:66
Raster property.
This class represents raster band description.
Definition: Band.h:64
int GetTeDataType(GDALDataType gt)
It translates a GDAL DataType to a TerraLib DataType.
Definition: Utils.h:58
An exception class for the GDAL module.
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
static UnitsOfMeasureManager & getInstance()
It returns a reference to the singleton instance.
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:44
An abstract class for raster data strucutures.
Definition: Raster.h:71
const double * getGeoreference() const
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
Definition: Grid.cpp:248
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name.
Definition: Utils.cpp:769
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
Definition: Utils.cpp:54
std::complex< double > m_valuesScale
Scale is the values (real and imaginary) which is multiplied to grid values for this sample dimension...
Definition: BandProperty.h:138
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
Definition: Grid.cpp:193
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
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Definition: Grid.cpp:275
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
std::complex< double > m_valuesOffset
Offset is the values (real and imaginary) to add to grid values for this sample dimension, default is 0.
Definition: BandProperty.h:137
TEGDALEXPORT void Rasterize(std::vector< te::gm::Geometry * > geometries, GDALDataset *outraster)
Rasterizes a given vector of geometries, using GDALRasterizeGeometries function.
Definition: Utils.cpp:721
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
Saturation color interpretation.
Definition: Enums.h:64
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
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
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
Green channel color interpretation.
Definition: Enums.h:60
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
TEGDALEXPORT te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset.
Definition: Utils.cpp:102
ColorInterp m_colorInterp
The color interpretation.
Definition: BandProperty.h:140
Magenta color interpretation.
Definition: Enums.h:67
te::rst::RasterProperty * GetRasterProperty(std::string strAccessInfo)
Gets the complete description from a GDAL dataset.
Definition: Utils.cpp:356