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