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"
50 #include <gdalwarper.h>
52 #include <ogr_spatialref.h>
53 #include <ogrsf_frmts.h>
56 #include <boost/algorithm/string.hpp>
57 #include <boost/format.hpp>
58 #include <boost/filesystem/path.hpp>
59 #include <boost/filesystem/operations.hpp>
63 std::vector<std::string> words;
64 boost::split(words, name, boost::is_any_of(
":"), boost::token_compress_on);
71 if (driverName ==
"HDF4")
74 sdname = words[0] +
":" + words[1] +
":" + words[3];
76 sdname = sdname +
":" + words[4];
79 else if (driverName ==
"NITF")
83 sdname = words[0] +
":" + words[1];
85 else if (driverName ==
"netCDF")
88 sdname = words[0] +
":" + words[2];
95 for (
size_t i=0; i<words.size()-1;++i)
96 sdname = sdname +
":" + words[i];
108 char* projWKT = (
char*)gds->GetProjectionRef();
111 char** projWKTPtr = &(projWKT);
112 OGRSpatialReference oSRS;
113 oSRS.importFromWkt( projWKTPtr );
114 oSRS.AutoIdentifyEPSG();
115 const char* srsAuth = oSRS.GetAuthorityCode(0);
117 srid = atoi(srsAuth);
121 if( gds->GetGeoTransform(gtp) == CE_Failure )
132 gds->GetRasterYSize(), srid);
145 bool hasPalette =
false;
147 if( gds->GetRasterCount() > 0 )
149 if( gds->GetRasterBand(1)->GetColorInterpretation() == GCI_PaletteIndex )
152 assert( gds->GetRasterBand(1)->GetColorTable() );
154 switch( gds->GetRasterBand(1)->GetColorTable()->GetPaletteInterpretation() )
169 throw Exception(
TR_GDAL(
"invalid palette interpretation"));
175 nBands = gds->GetRasterCount();
180 for (
int rasterIdx = 0; rasterIdx < nBands ; ++rasterIdx)
182 GDALRasterBand* rasterBand = gds->GetRasterBand( hasPalette ? 1 : ( 1 + rasterIdx) );
188 const unsigned int bandIndex )
195 bprop->
m_idx = bandIndex;
202 switch( gband->GetColorTable()->GetPaletteInterpretation() )
211 throw Exception(
TR_GDAL(
"invalid band index"));
231 throw Exception(
TR_GDAL(
"invalid band index"));
251 throw Exception(
TR_GDAL(
"invalid band index"));
268 throw Exception(
TR_GDAL(
"invalid band index"));
273 throw Exception(
TR_GDAL(
"invalid palette interpretation"));
279 int noDataValueIsUsed = 0;
280 double nodataval = gband->GetNoDataValue(&noDataValueIsUsed);
281 if (noDataValueIsUsed)
284 std::string unitName = gband->GetUnitType();
285 if (!unitName.empty())
305 if( rst->
getGDALDataset()->GetRasterBand(1)->GetColorInterpretation() == GCI_PaletteIndex )
307 if( rst->
getGDALDataset()->GetRasterBand(1)->GetColorTable() == 0 )
309 throw Exception(
TR_GDAL(
"invalid color table"));
312 switch( rst->
getGDALDataset()->GetRasterBand(1)->GetColorTable()->GetPaletteInterpretation() )
327 throw Exception(
TR_GDAL(
"invalid palette interpretation"));
337 for (
int b = 0; b < nBands; b++)
343 GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), GA_ReadOnly);
349 std::vector<te::rst::BandProperty*> bprops;
353 std::map<std::string, std::string> rinfo;
364 GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), GA_ReadOnly);
375 const std::map<std::string, std::string>& optParams)
379 GDALDriverManager* dManPtr = GetGDALDriverManager();
381 GDALDriver* driverPtr = dManPtr->GetDriverByName(driverName.c_str());
384 throw Exception(
"Could not create raster because the underlying driver was not found!");
388 if (targetGDataType == GDT_Unknown)
389 throw Exception(
"Could not create raster because of unknown band data type!");
391 char** papszOptions = 0;
393 std::map<std::string, std::string>::const_iterator it = optParams.begin();
395 std::map<std::string, std::string>::const_iterator it_end = optParams.end();
399 if(it->first ==
"URI" || it->first ==
"SOURCE")
406 papszOptions = CSLSetNameValue(papszOptions, it->first.c_str(), it->second.c_str());
411 GDALDataset* poDataset;
413 if (driverName ==
"JPEG" || driverName ==
"PNG")
415 GDALDriver* tmpDriverPtr = dManPtr->GetDriverByName(
"MEM");
417 poDataset = tmpDriverPtr->Create(name.c_str(),
425 poDataset = driverPtr->Create(name.c_str(),
433 CSLDestroy(papszOptions);
436 throw Exception(
"Could not create raster!");
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];
444 poDataset->SetGeoTransform(gt);
446 OGRSpatialReference oSRS;
448 oSRS.importFromEPSG(g->
getSRID());
450 char* projWKTPtr = 0;
452 if(oSRS.exportToWkt(&projWKTPtr) == OGRERR_NONE)
454 poDataset->SetProjection(projWKTPtr);
458 int nb =
static_cast<int>(bands.size());
460 for(
int dIdx = 0; dIdx < nb; ++dIdx)
462 GDALRasterBand* rasterBand = poDataset->GetRasterBand(1 + dIdx);
466 rasterBand->SetColorInterpretation(ci);
468 if (ci == GCI_PaletteIndex)
470 GDALColorTable* gCTablePtr =
new GDALColorTable(GPI_RGB);
471 GDALColorEntry gCEntry;
473 for (
unsigned int pIdx=0 ; pIdx < bands[dIdx]->m_palette.size(); ++pIdx)
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);
482 rasterBand->SetColorTable(gCTablePtr);
485 rasterBand->SetNoDataValue(bands[dIdx]->m_noDataValue);
486 rasterBand->SetOffset(bands[dIdx]->m_valuesOffset.real());
487 rasterBand->SetScale(bands[dIdx]->m_valuesScale.real());
497 const std::vector<te::rst::BandProperty*>& bands,
498 const std::map<std::string, std::string>& optParams)
501 assert(bands.size() > 0);
509 GDALAccess gpolicy = GA_ReadOnly;
514 GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), gpolicy);
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:";
528 connInfo +=
" host = " + it->second;
530 it = dsInfo.find(
"port");
533 connInfo +=
" port = " + it->second;
535 it = dsInfo.find(
"dbname");
538 connInfo +=
" dbname = " + it->second;
540 it = dsInfo.find(
"user");
543 connInfo +=
" user = " + it->second;
545 it = dsInfo.find(
"password");
548 connInfo +=
" password = " + it->second;
550 it = dsInfo.find(
"schema");
553 connInfo +=
" schema = " + it->second;
555 it = dsInfo.find(
"table");
558 connInfo +=
" table = " + it->second;
560 it = dsInfo.find(
"where");
563 connInfo +=
" where = " + it->second;
565 it = dsInfo.find(
"mode");
568 connInfo +=
" mode = " + it->second;
575 OGRSpatialReference oSRS;
576 oSRS.importFromEPSG(srid);
577 char* coutWKT = NULL;
578 oSRS.exportToWkt(&coutWKT);
579 std::string outwkt(coutWKT);
580 return (!outwkt.empty());
600 int nBands = GDALGetRasterCount(hSrcDS);
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++)
611 psWarpOptions->panSrcBands[b] = b+1;
612 psWarpOptions->panDstBands[b] = b+1;
616 psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer(hSrcDS, GDALGetProjectionRef(hSrcDS),
617 hDstDS, GDALGetProjectionRef(hDstDS),
619 psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
622 GDALWarpOperation oOperation;
623 oOperation.Initialize(psWarpOptions);
624 oOperation.WarpRegion(0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS));
627 GDALDestroyGenImgProjTransformer(psWarpOptions->pTransformerArg);
628 GDALDestroyWarpOptions(psWarpOptions);
636 boost::filesystem::path mpath(name.c_str());
640 if(ext ==
".TIF" || ext ==
".TIFF")
641 return std::string(
"GTiff");
644 return std::string(
"JPEG");
647 return std::string(
"NITF");
650 return std::string(
"netCDF");
653 return std::string(
"GRIB");
656 return std::string(
"PNG");
659 return std::string(
"HDF4");
666 std::map<std::string, std::string>::const_iterator it = connInfo.find(
"URI");
668 if(it != connInfo.end())
671 it = connInfo.find(
"SOURCE");
673 if(it != connInfo.end())
676 throw Exception(
TR_GDAL(
"Invalid data source connection information!."));
682 OGRSFDriver *ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
"Memory");
684 OGRDataSource* ds = ogrDriver->CreateDataSource(
"ds_vectorize", NULL);
686 OGRLayer* ogrLayer = ds->CreateLayer(
"vectorization", NULL, wkbMultiPolygon, NULL);
688 ogrLayer->CreateField(
new OGRFieldDefn(
"id", OFTInteger));
691 if (GDALPolygonize(band, NULL , ogrLayer, 0, NULL, NULL, NULL) == CE_Failure)
695 for (
int g = 0; g < ogrLayer->GetFeatureCount(); g++)
698 OGRDataSource::DestroyDataSource(ds);
704 std::vector<int> bandList;
706 bandList.push_back(1);
708 std::vector<OGRGeometryH> ogrGeometries;
710 std::vector<double> burnValues;
714 for (std::size_t g = 0; g < geometries.size(); g++)
716 burnValues.push_back(bvalue % 255);
718 bvalue = bvalue >= 127? bvalue - 126: bvalue > 255? 0: bvalue + 127;
723 GDALRasterizeGeometries(outraster, bandList.size(), &(bandList[0]), ogrGeometries.size(), &(ogrGeometries[0]), NULL, NULL, &(burnValues[0]), NULL, NULL, NULL);
728 std::size_t firstIdx = uri.find(
":" );
730 if( firstIdx < uri.size() )
732 std::size_t secondIdx = uri.find(
":", firstIdx + 1 );
734 if( secondIdx < uri.size() )
753 std::size_t firstIdx = subDataSetName.find(
":" );
755 if( firstIdx < subDataSetName.size() )
757 std::size_t secondIdx = subDataSetName.find(
":", firstIdx + 1 );
759 if( secondIdx < subDataSetName.size() )
761 return subDataSetName.substr( secondIdx + 1, subDataSetName.size() - firstIdx - 1);
765 return subDataSetName;
770 return subDataSetName;
775 return subDataSetName;
std::complex< double > m_valuesOffset
Offset is the values (real and imaginary) to add to grid values for this sample dimension, default is 0.
int m_blkh
Block height (pixels).
te::rst::RasterProperty * GetRasterProperty(std::string strAccessInfo)
Gets the complete description from a GDAL dataset.
Palette indexes color interpretation.
std::string GetGDALConnectionInfo(const std::map< std::string, std::string > &connInfo)
It returns a GDAL connection string from the given map.
bool RecognizesSRID(unsigned int srid)
It returns true if GDAL recognizes the given SRS id.
Key (black) color interpretation.
Blue channel color interpretation.
Lightness color interpretation.
te::rst::ColorInterp GetTeColorInterpretation(GDALColorInterp gci)
It translates a GDAL ColorInterpretation to a TerraLib ColorInterpretation.
GDALDataType GetGDALDataType(int tet)
It translates a TerraLib DataType to a GDAL DataType.
ColorInterp m_colorInterp
The color interpretation.
This class represents Raster data.
void GetBands(te::gdal::Raster *rst, std::vector< te::gdal::Band * > &bands)
Gets the list of bands from a GDAL dataset.
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.
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.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Alpha channel color interpretation.
int m_nblocksy
The number of blocks in y.
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.
Cyan color interpretation.
unsigned int getNumberOfRows() const
Returns the grid number of rows.
GDALDataset * GetRasterHandle(std::string strAccessInfo, te::common::AccessPolicy policy=te::common::RAccess)
Get a handle to a raster file.
AccessPolicy
Supported data access policies (can be used as bitfield).
Hue channel color interpretation.
Red channel color interpretation.
A rectified grid is the spatial support for raster data.
int m_blkw
Block width (pixels).
TEGDALEXPORT void Vectorize(GDALRasterBand *band, std::vector< te::gm::Geometry * > &geometries)
Vectorizes a given raster band, using GDALPolygonize function.
TEOGREXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Yellow color interpretation.
bool IsSubDataSet(const std::string &uri)
Returns true if the given URI is related to a sub-dataset.
te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset.
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.
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 ...
This class represents raster band description.
std::size_t m_idx
The band index.
std::complex< double > m_valuesScale
Scale is the values (real and imaginary) which is multiplied to grid values for this sample dimension...
A raster band description.
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
This is a class that represents a GDAL Raster.
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
bool ReprojectRaster(te::rst::Raster const *const rin, te::rst::Raster *rout)
Reprojects a raster to another SRS.
Saturation color interpretation.
Magenta color interpretation.
int m_nblocksx
The number of blocks in x.
An abstract class for raster data strucutures.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
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.
int getSRID() const
Returns the grid spatial reference system identifier.
int GetTeDataType(GDALDataType gt)
It translates a GDAL DataType to a TerraLib DataType.
GDALDataset * getGDALDataset() const
Returns the raster GDAL handler.
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.
te::rst::BandProperty * GetBandProperty(GDALRasterBand *gband, const unsigned int bandIndex)
Gets the properties of a single band from a GDAL dataset.
An Envelope defines a 2D rectangular region.
TEGDALEXPORT void Rasterize(std::vector< te::gm::Geometry * > geometries, GDALDataset *outraster)
Rasterizes a given vector of geometries, using GDALRasterizeGeometries function.
void setUnitOfMeasure(te::common::UnitOfMeasurePtr u)
Sets the unit of measure of the values;.
Index into a lookup table.
Green channel color interpretation.
te::gm::Envelope * GetExtent(std::string strAccessInfo)
Gets the extent of a raster data decoded by GDAL.
const double * getGeoreference() const
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
#define TR_GDAL(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...