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];
 
  112   char* projWKT = (
char*)gds->GetProjectionRef();
 
  118     char** projWKTPtr = &(projWKT);
 
  119     OGRSpatialReference oSRS;
 
  120     oSRS.importFromWkt( projWKTPtr );
 
  121     oSRS.AutoIdentifyEPSG();
 
  122     const char* srsAuth = oSRS.GetAuthorityCode(0);
 
  124       srid = atoi(srsAuth);
 
  131   if( gds->GetGeoTransform(gtp) == CE_Failure )
 
  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 ];
 
  145     grid = 
new te::rst::Grid(gridAffineParams, gds->GetRasterXSize(),
 
  146       gds->GetRasterYSize(), srid);    
 
  160   bool hasPalette = 
false;
 
  162   if( gds->GetRasterCount() > 0 )
 
  164     if( gds->GetRasterBand(1)->GetColorInterpretation() == GCI_PaletteIndex )
 
  167       assert( gds->GetRasterBand(1)->GetColorTable() );
 
  169       switch( gds->GetRasterBand(1)->GetColorTable()->GetPaletteInterpretation() )
 
  184           throw Exception(
TE_TR(
"invalid palette interpretation"));
 
  190       nBands = gds->GetRasterCount();
 
  195   for (
int rasterIdx = 0; rasterIdx < nBands ; ++rasterIdx)
 
  197     GDALRasterBand* rasterBand = gds->GetRasterBand( hasPalette ? 1 : ( 1 + rasterIdx) );
 
  203    const unsigned int bandIndex )
 
  210   bprop->
m_idx = bandIndex;
 
  217     switch( gband->GetColorTable()->GetPaletteInterpretation() )
 
  226             throw Exception(
TE_TR(
"invalid band index"));
 
  246             throw Exception(
TE_TR(
"invalid band index"));
 
  266             throw Exception(
TE_TR(
"invalid band index"));
 
  283             throw Exception(
TE_TR(
"invalid band index"));
 
  288         throw Exception(
TE_TR(
"invalid palette interpretation"));
 
  294   int noDataValueIsUsed = 0;
 
  295   double nodataval = gband->GetNoDataValue(&noDataValueIsUsed);
 
  296   if (noDataValueIsUsed) 
 
  299   std::string unitName = gband->GetUnitType();
 
  300   if (!unitName.empty())
 
  320     if( rst->
getGDALDataset()->GetRasterBand(1)->GetColorInterpretation() == GCI_PaletteIndex )
 
  322       if( rst->
getGDALDataset()->GetRasterBand(1)->GetColorTable() == 0 )
 
  324         throw Exception(
TE_TR(
"invalid color table"));
 
  327       switch( rst->
getGDALDataset()->GetRasterBand(1)->GetColorTable()->GetPaletteInterpretation() )
 
  342           throw Exception(
TE_TR(
"invalid palette interpretation"));
 
  352   for (
int b = 0; b < nBands; b++)
 
  358   GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), GA_ReadOnly);
 
  364   std::vector<te::rst::BandProperty*> bprops;
 
  368   std::map<std::string, std::string> rinfo;
 
  379   GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), GA_ReadOnly);
 
  390                           const std::map<std::string, std::string>& optParams)
 
  394   GDALDriverManager* dManPtr = GetGDALDriverManager();
 
  396   GDALDriver* driverPtr = dManPtr->GetDriverByName(driverName.c_str());
 
  399     throw Exception(
"Could not create raster because the underlying driver was not found!");
 
  403   if (targetGDataType == GDT_Unknown)
 
  404     throw Exception(
"Could not create raster because of unknown band data type!");
 
  406   char** papszOptions = 0;
 
  408   std::map<std::string, std::string>::const_iterator it = optParams.begin();
 
  410   std::map<std::string, std::string>::const_iterator it_end = optParams.end();
 
  414     if(it->first == 
"URI" || it->first == 
"SOURCE")
 
  421     papszOptions = CSLSetNameValue(papszOptions, it->first.c_str(), it->second.c_str());
 
  426   GDALDataset* poDataset;
 
  428   if (driverName == 
"JPEG" || driverName == 
"PNG")
 
  430     GDALDriver* tmpDriverPtr = dManPtr->GetDriverByName(
"MEM");
 
  432     poDataset = tmpDriverPtr->Create(name.c_str(),
 
  440     poDataset = driverPtr->Create(name.c_str(),
 
  448     CSLDestroy(papszOptions);
 
  451     throw Exception(
"Could not create raster!");
 
  464   poDataset->SetGeoTransform(gt);
 
  466   OGRSpatialReference oSRS;
 
  468   oSRS.importFromEPSG(g->
getSRID());
 
  470   char* projWKTPtr = 0;
 
  472   if(oSRS.exportToWkt(&projWKTPtr) == OGRERR_NONE)
 
  474     poDataset->SetProjection(projWKTPtr);
 
  478   int nb = 
static_cast<int>(bands.size());
 
  480   for(
int dIdx = 0; dIdx < nb; ++dIdx)
 
  482     GDALRasterBand* rasterBand = poDataset->GetRasterBand(1 + dIdx);
 
  486     rasterBand->SetColorInterpretation(ci);
 
  488     if (ci == GCI_PaletteIndex)
 
  490       GDALColorTable* gCTablePtr = 
new GDALColorTable(GPI_RGB);
 
  491       GDALColorEntry gCEntry;
 
  493       for (
unsigned int pIdx=0 ; pIdx < bands[dIdx]->m_palette.size(); ++pIdx)
 
  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);
 
  502       rasterBand->SetColorTable(gCTablePtr);
 
  505     rasterBand->SetNoDataValue(bands[dIdx]->m_noDataValue);
 
  506     rasterBand->SetOffset(bands[dIdx]->m_valuesOffset.real());
 
  507     rasterBand->SetScale(bands[dIdx]->m_valuesScale.real());
 
  517                                     const std::vector<te::rst::BandProperty*>& bands,
 
  518                                     const std::map<std::string, std::string>& optParams)
 
  521   assert(bands.size() > 0);
 
  529   GDALAccess gpolicy = GA_ReadOnly;
 
  534   GDALDataset* gds = (GDALDataset*) GDALOpen(strAccessInfo.c_str(), gpolicy);
 
  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:";
 
  548     connInfo += 
" host = " + it->second;
 
  550   it = dsInfo.find(
"port");
 
  553     connInfo += 
" port = " + it->second;
 
  555   it = dsInfo.find(
"dbname");
 
  558     connInfo += 
" dbname = " + it->second;
 
  560   it = dsInfo.find(
"user");
 
  563     connInfo += 
" user = " + it->second;
 
  565   it = dsInfo.find(
"password");
 
  568     connInfo += 
" password = " + it->second;
 
  570   it = dsInfo.find(
"schema");
 
  573     connInfo += 
" schema = " + it->second;
 
  575   it = dsInfo.find(
"table");
 
  578     connInfo += 
" table = " + it->second;
 
  580   it = dsInfo.find(
"where");
 
  583     connInfo += 
" where = " + it->second;
 
  585   it = dsInfo.find(
"mode");
 
  588     connInfo += 
" mode = " + it->second;
 
  595   OGRSpatialReference oSRS;
 
  596   oSRS.importFromEPSG(srid);
 
  597   char* coutWKT = NULL;
 
  598   oSRS.exportToWkt(&coutWKT);
 
  599   std::string outwkt(coutWKT);
 
  600   return (!outwkt.empty());
 
  620   int nBands = GDALGetRasterCount(hSrcDS);
 
  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++)
 
  631     psWarpOptions->panSrcBands[b] = b+1;
 
  632     psWarpOptions->panDstBands[b] = b+1;
 
  636   psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer(hSrcDS, GDALGetProjectionRef(hSrcDS),
 
  637                                                                    hDstDS, GDALGetProjectionRef(hDstDS),
 
  639   psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
 
  642   GDALWarpOperation oOperation;
 
  643   oOperation.Initialize(psWarpOptions);
 
  644   oOperation.WarpRegion(0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS));
 
  647   GDALDestroyGenImgProjTransformer(psWarpOptions->pTransformerArg);
 
  648   GDALDestroyWarpOptions(psWarpOptions);
 
  656   boost::filesystem::path mpath(name.c_str());
 
  660   if(ext == 
".TIF" || ext == 
".TIFF")
 
  661     return std::string(
"GTiff");
 
  664     return std::string(
"JPEG");
 
  667     return std::string(
"NITF");
 
  670     return std::string(
"netCDF");
 
  673     return std::string(
"GRIB");
 
  676     return std::string(
"PNG");
 
  679      return std::string(
"HDF4");
 
  686   std::map<std::string, std::string>::const_iterator it = connInfo.find(
"URI");
 
  688   if(it != connInfo.end())
 
  691   it = connInfo.find(
"SOURCE");
 
  693   if(it != connInfo.end())
 
  696   throw Exception(
TE_TR(
"Invalid data source connection information!."));
 
  702   OGRSFDriver *ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
"Memory");
 
  704   OGRDataSource* ds = ogrDriver->CreateDataSource(
"ds_vectorize", NULL);
 
  706   OGRLayer* ogrLayer = ds->CreateLayer(
"vectorization", NULL, wkbMultiPolygon, NULL);
 
  708   ogrLayer->CreateField(
new OGRFieldDefn(
"id", OFTInteger));
 
  711   if (GDALPolygonize(band, NULL , ogrLayer, 0, NULL, NULL, NULL) == CE_Failure)
 
  715   for (
int g = 0; g < ogrLayer->GetFeatureCount(); g++)
 
  718   OGRDataSource::DestroyDataSource(ds);
 
  724   std::vector<int> bandList;
 
  726   bandList.push_back(1);
 
  728   std::vector<OGRGeometryH> ogrGeometries;
 
  730   std::vector<double> burnValues;
 
  734   for (std::size_t g = 0; g < geometries.size(); g++)
 
  736     burnValues.push_back(bvalue % 255);
 
  738     bvalue = bvalue >= 127? bvalue - 126: bvalue > 255? 0: bvalue + 127;
 
  743   GDALRasterizeGeometries(outraster, bandList.size(), &(bandList[0]), ogrGeometries.size(), &(ogrGeometries[0]), NULL, NULL, &(burnValues[0]), NULL, NULL, NULL);
 
  748   std::size_t firstIdx = uri.find( 
":" );
 
  750   if( firstIdx < uri.size() )
 
  752     std::size_t secondIdx = uri.find( 
":", firstIdx + 1 );
 
  754     if( secondIdx < uri.size() )
 
  773     std::size_t firstIdx = subDataSetName.find( 
":" );
 
  775     if( firstIdx < subDataSetName.size() )
 
  777       std::size_t secondIdx = subDataSetName.find( 
":", firstIdx + 1 );
 
  779       if( secondIdx < subDataSetName.size() )
 
  781         return subDataSetName.substr( secondIdx + 1, subDataSetName.size() - firstIdx - 1);
 
  785         return subDataSetName;
 
  790       return subDataSetName;
 
  795     return subDataSetName;
 
  801   static boost::mutex getStaticMutexStaticMutex;
 
  802   return getStaticMutexStaticMutex;
 
GDALDataset * getGDALDataset() const 
Returns the raster GDAL handler. 
 
GDALColorInterp GetGDALColorInterpretation(te::rst::ColorInterp ci)
It translates a TerraLib ColorInterpretation to a GDAL ColorInterpretation. 
 
TEGDALEXPORT void Vectorize(GDALRasterBand *band, std::vector< te::gm::Geometry * > &geometries)
Vectorizes a given raster band, using GDALPolygonize function. 
 
unsigned int getNumberOfRows() const 
Returns the grid number of rows. 
 
Palette indexes color interpretation. 
 
This is a class that represents a GDAL Raster. 
 
std::size_t m_idx
The band index. 
 
GDALDataType GetGDALDataType(int tet)
It translates a TerraLib DataType to a GDAL DataType. 
 
Hue channel color interpretation. 
 
Alpha channel color interpretation. 
 
TEOGREXPORT OGRGeometry * Convert2OGR(const te::gm::Geometry *teGeom)
It converts the TerraLib Geometry to OGR Geometry. 
 
void GetBands(te::gdal::Raster *rst, std::vector< te::gdal::Band * > &bands)
Gets the list of bands from a GDAL dataset. 
 
Index into a lookup table. 
 
Lightness color interpretation. 
 
te::rst::ColorInterp GetTeColorInterpretation(GDALColorInterp gci)
It translates a GDAL ColorInterpretation to a TerraLib ColorInterpretation. 
 
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. 
 
A raster band description. 
 
This class represents Raster data. 
 
int getSRID() const 
Returns the grid spatial reference system identifier. 
 
int m_nblocksx
The number of blocks in x. 
 
It gives access to values in one band (dimension) of a raster. 
 
int m_nblocksy
The number of blocks in y. 
 
void setUnitOfMeasure(te::common::UnitOfMeasurePtr u)
Sets the unit of measure of the values;. 
 
Red channel color interpretation. 
 
te::rst::BandProperty * GetBandProperty(GDALRasterBand *gband, const unsigned int bandIndex)
Gets the properties of a single band from a GDAL dataset. 
 
std::string Convert2UCase(const std::string &value)
It converts a string to upper case. 
 
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max(). 
 
#define TE_TR(message)
It marks a string in order to get translated. 
 
Cyan color interpretation. 
 
This class represents raster band description. 
 
int GetTeDataType(GDALDataType gt)
It translates a GDAL DataType to a TerraLib DataType. 
 
An exception class for the GDAL module. 
 
AccessPolicy
Supported data access policies (can be used as bitfield). 
 
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. 
 
GDALDataset * GetRasterHandle(std::string strAccessInfo, te::common::AccessPolicy policy=te::common::RAccess)
Get a handle to a raster file. 
 
te::gm::Envelope * GetExtent(std::string strAccessInfo)
Gets the extent of a raster data decoded by GDAL. 
 
static UnitsOfMeasureManager & getInstance()
It returns a reference to the singleton instance. 
 
An Envelope defines a 2D rectangular region. 
 
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib. 
 
An abstract class for raster data strucutures. 
 
const double * getGeoreference() const 
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
 
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name. 
 
TEOGREXPORT te::gm::Geometry * Convert2TerraLib(OGRGeometry *ogrGeom)
It converts the OGR Geometry to TerraLib Geometry. 
 
std::complex< double > m_valuesScale
Scale is the values (real and imaginary) which is multiplied to grid values for this sample dimension...
 
int m_blkw
Block width (pixels). 
 
unsigned int getNumberOfColumns() const 
Returns the grid number of columns. 
 
Key (black) color interpretation. 
 
Yellow color interpretation. 
 
std::string GetDriverName(const std::string &dsName)
It returns the GDAL driver name associated to a data source name. 
 
bool IsSubDataSet(const std::string &uri)
Returns true if the given URI is related to a sub-dataset. 
 
bool ReprojectRaster(te::rst::Raster const *const rin, te::rst::Raster *rout)
Reprojects a raster to another SRS. 
 
bool RecognizesSRID(unsigned int srid)
It returns true if GDAL recognizes the given SRS id. 
 
std::string GetGDALConnectionInfo(const std::map< std::string, std::string > &connInfo)
It returns a GDAL connection string from the given map. 
 
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid. 
 
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 ...
 
std::complex< double > m_valuesOffset
Offset is the values (real and imaginary) to add to grid values for this sample dimension, default is 0. 
 
TEGDALEXPORT void Rasterize(std::vector< te::gm::Geometry * > geometries, GDALDataset *outraster)
Rasterizes a given vector of geometries, using GDALRasterizeGeometries function. 
 
UnitOfMeasurePtr find(unsigned int id) const 
Returns a unit of measure identified by its identificaton. 
 
Saturation color interpretation. 
 
int m_blkh
Block height (pixels). 
 
TEGDALEXPORT boost::mutex & getStaticMutex()
Returns a reference to a static mutex initialized when this module is initialized. 
 
Blue channel color interpretation. 
 
A rectified grid is the spatial support for raster data. 
 
Green channel color interpretation. 
 
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. 
 
TEGDALEXPORT te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset. 
 
ColorInterp m_colorInterp
The color interpretation. 
 
Magenta color interpretation. 
 
te::rst::RasterProperty * GetRasterProperty(std::string strAccessInfo)
Gets the complete description from a GDAL dataset.