27 #include "../common/STLUtils.h" 
   28 #include "../common/StringUtils.h" 
   29 #include "../common/Translator.h" 
   30 #include "../datatype/Enums.h" 
   31 #include "../datatype/SimpleData.h" 
   32 #include "../geometry/Coord2D.h" 
   33 #include "../geometry/Envelope.h" 
   34 #include "../raster/Grid.h" 
   35 #include "../raster/RasterProperty.h" 
   36 #include "../raster/RasterFactory.h" 
   37 #include "../raster/Utils.h" 
   38 #include "../raster/Reprojection.h" 
   39 #include "../srs/Converter.h" 
   53 #include <ogr_spatialref.h> 
   56 #include <boost/lexical_cast.hpp> 
   57 #include <boost/scoped_array.hpp> 
   82     throw Exception(
TE_TR(
"Data file can not be accessed."));
 
   90                          const std::vector<te::rst::BandProperty*>& bprops,
 
   91                          const std::map<std::string, std::string>& optParams,
 
   93   : te::rst::
Raster(grid, p),
 
   96   create(grid, bprops, optParams, 0, 0);
 
  119     GDALDriver* driverPtr = rhs.
m_gdataset->GetDriver();
 
  121     char** papszOptions = 0;
 
  142   : te::rst::
Raster( 0, policy ),
 
  156     throw Exception(
TE_TR(
"Data file can not be accessed.")); 
 
  169     std::string driverName = 
GetDriverName(m_gdataset->GetDescription());
 
  171     if ((driverName == 
"PNG" || driverName == 
"JPEG") &&
 
  174       char** papszOptions = 0;
 
  176       GDALDriver* driverPtr = GetGDALDriverManager()->GetDriverByName(driverName.c_str());
 
  178       GDALDataset* poDataset = driverPtr->CreateCopy(m_gdataset->GetDescription(),
 
  179                                                      m_gdataset, 0, papszOptions,
 
  182       GDALClose(poDataset);
 
  185     GDALClose(m_gdataset);
 
  197   std::map<std::string, std::string>::const_iterator it = rinfo.find(
"URI");
 
  200   if(it == rinfo.end())
 
  202     it = rinfo.find(
"SOURCE");
 
  204     if(it == rinfo.end())
 
  205       throw Exception(
TE_TR(
"At least the URI or SOURCE parameter must be informed!"));
 
  208   m_myURI = it->second;
 
  217     throw Exception(
TE_TR(
"Data file can not be accessed."));
 
  225   m_name = m_gdataset->GetDescription();
 
  230   std::map<std::string, std::string> info;
 
  232   info[
"URI"] = m_myURI;
 
  239   return m_bands.size();
 
  244   assert(i < m_bands.size());
 
  246   return m_bands[i]->getProperty()->getType();
 
  251   assert(i < m_bands.size());
 
  258   assert(i < m_bands.size());
 
  265   assert(i < m_bands.size());
 
  272   assert(i < m_bands.size());
 
  301   if (!(scale < 0 && method == 3))
 
  307   std::vector<te::rst::BandProperty*> bands;
 
  309   for (std::size_t b = 0; b < getNumberOfBands(); b++)
 
  314   int overviewScale[1] = { -scale };
 
  316   GDALDataset* inds = getGDALDataset();
 
  318   GDALDataset* outds = 
static_cast<te::gdal::Raster*
>(rout)->getGDALDataset();
 
  320   int overviewIndex = -1;
 
  321   for (
int ov = 0; ov < inds->GetRasterBand(1)->GetOverviewCount(); ov++)
 
  322     if (inds->GetRasterBand(1)->GetOverview(ov)->GetXSize() == outds->GetRasterBand(1)->GetXSize())
 
  325       ov = inds->GetRasterBand(1)->GetOverviewCount();
 
  328   if (overviewIndex == -1)
 
  330     inds->BuildOverviews(
"CUBIC", 1, overviewScale, 0, NULL, GDALDummyProgress, NULL);
 
  332     overviewIndex = inds->GetRasterBand(1)->GetOverviewCount() - 1;
 
  335   GByte* buffer = (GByte*) malloc(outds->GetRasterXSize() * outds->GetRasterYSize() * 
sizeof(GByte*));
 
  338   outds->GetGeoTransform(geoT);
 
  339   outds->SetGeoTransform(geoT);
 
  341   for (
int b = 0; b < inds->GetRasterCount(); b++)
 
  343     GDALRasterBand* outband = outds->GetRasterBand(b + 1);
 
  345     GDALRasterBand* inband = inds->GetRasterBand(b + 1)->GetOverview(overviewIndex);
 
  347     inband->RasterIO(GF_Read, 0, 0, inband->GetXSize(), inband->GetYSize(),
 
  348                       buffer, inband->GetXSize(), inband->GetYSize(), GDT_Byte, 0, 0);
 
  350     outband->RasterIO(GF_Write, 0, 0, inband->GetXSize(), inband->GetYSize(),
 
  351                       buffer, inband->GetXSize(), inband->GetYSize(), GDT_Byte, 0, 0);
 
  360   std::map<std::string, std::string>::const_iterator it = rinfo.find(
"USE_TERRALIB_REPROJECTION");
 
  362   if((it != rinfo.end()) &&
 
  365     std::map<std::string, std::string> irinfo(rinfo);
 
  366     std::map<std::string, std::string>::iterator iit = irinfo.find(
"USE_TERRALIB_REPROJECTION");
 
  373   if (srid == getSRID())
 
  377     throw Exception(
TE_TR(
"Output SRID not recognized! Expecting a EPSG SRS id."));
 
  379   unsigned int ncols = getNumberOfColumns();
 
  380   unsigned int nrows = getNumberOfRows();
 
  391     ncols = 
static_cast<unsigned int>((urx-llx)/getResolutionX())+1;
 
  393     nrows = 
static_cast<unsigned int>((ury-lly)/getResolutionY())+1;
 
  399   if (resx == 0 || resy == 0) 
 
  407     ncols = 
static_cast<unsigned int>(env->
getWidth()/resx) + 1;
 
  409     nrows = 
static_cast<unsigned int>(env->
getHeight()/resy) + 1;
 
  415   std::vector<te::rst::BandProperty*> bands;
 
  416   for (
unsigned int b=0; b<this->getNumberOfBands(); ++b)
 
  444                              const std::vector<te::rst::BandProperty*> bands,
 
  445                              const std::map<std::string, std::string>& rinfo,
 
  446                              void* h, 
void (*deleter)(
void*))
 
  457     intptr_t buffaddress = (intptr_t) h;
 
  459     std::string memraster  = 
"MEM:::DATAPOINTER=";
 
  460                 memraster += boost::lexical_cast<std::string>(buffaddress);
 
  461                 memraster += 
",PIXELS=";
 
  463                 memraster += 
",LINES=";
 
  465                 memraster += 
",BANDS=";
 
  466                 memraster += boost::lexical_cast<std::string>(bands.size());
 
  467                 memraster += 
",DATATYPE=";
 
  468                 memraster += GDALGetDataTypeName(
GetGDALDataType(bands[0]->getType()));
 
  474     std::map<std::string, std::string>::const_iterator it = rinfo.find(
"URI");
 
  475     if(it == rinfo.end())
 
  477       it = rinfo.find(
"SOURCE");
 
  479       if(it == rinfo.end())
 
  480         throw Exception(
TE_TR(
"At least the URI or SOURCE parameter must be informed!"));
 
  483     m_myURI = it->second;
 
  496       std::string mess = 
TE_TR(
"Raster couldn't be created:");
 
  498       throw Exception(mess);
 
  508   if( m_gdataset == 0 )
 
  518      boost::scoped_array< int > overviewsIndexes( 
new int[ levels ] );
 
  519       for( 
unsigned int overViewIdx = 0 ; overViewIdx < levels ; ++overViewIdx )
 
  521         overviewsIndexes[ overViewIdx ] = overViewIdx + 1;
 
  526      m_gdataset->FlushCache();
 
  528      CPLErr returnValue = m_gdataset->BuildOverviews( 
 
  537      m_gdataset->FlushCache();
 
  539      returnValue = m_gdataset->BuildOverviews( 
 
  542        overviewsIndexes.get(),
 
  548      m_gdataset->FlushCache();
 
  550      m_dsUseCounterPtr->changeAccessType( oldAccessType );
 
  552      if( returnValue == CE_Failure )
 
  570   if( m_gdataset == 0 )
 
  576     if( m_gdataset->GetRasterCount() > 0 )
 
  578       if( m_gdataset->GetRasterBand( 1 )->GetOverviewCount() > 0 )
 
  580          CPLErr returnValue = m_gdataset->BuildOverviews( 
 
  589          m_gdataset->FlushCache();
 
  591          if( returnValue == CE_Failure )
 
  614   if( m_gdataset == 0 )
 
  620     if( m_gdataset->GetRasterCount() > 0 )
 
  622       return (
unsigned int)m_gdataset->GetRasterBand( 1 )->GetOverviewCount();
 
  633   if( m_gdataset == 0 )
 
  639     if( m_gdataset->GetRasterCount() > 0 )
 
  641       if( level <= ((
unsigned int)m_gdataset->GetRasterBand( 1 )->GetOverviewCount()) )
 
  643         return new Raster( level, m_myURI, m_policy );
 
GDALDataset * getGDALDataset() const 
Returns the raster GDAL handler. 
 
void open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
Opens a raster. 
 
This is a class that represents a GDAL Raster. 
 
GDALDataType GetGDALDataType(int tet)
It translates a TerraLib DataType to a GDAL DataType. 
 
Near neighborhood interpolation method. 
 
void GetBands(te::gdal::Raster *rst, std::vector< te::gdal::Band * > &bands)
Gets the list of bands from a GDAL dataset. 
 
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts. 
 
A raster band description. 
 
This class represents Raster data. 
 
const te::rst::Band & operator[](std::size_t i) const 
Access band in i position. 
 
It gives access to values in one band (dimension) of a raster. 
 
virtual Raster * resample(int method, unsigned int drow, unsigned int dcolumn, unsigned int height, unsigned int width, unsigned int newheight, unsigned int newwidth, const std::map< std::string, std::string > &rinfo) const 
Resample a subset of the raster, given a box. 
 
std::string Convert2UCase(const std::string &value)
It converts a string to upper case. 
 
double getWidth() const 
It returns the envelope width. 
 
~Raster()
Virtual destructor. 
 
te::rst::Raster * getMultiResLevel(const unsigned int level) const 
Returns the required level of a multi-resolution pyramid or NULL if that level does not exists...
 
Raster & operator=(const Raster &rhs)
 
te::rst::Raster * resample(int method, int scale, const std::map< std::string, std::string > &rinfo) const 
Resample raster. 
 
te::rst::Raster * transform(int srid, double llx, double lly, double urx, double ury, double resx, double resy, const std::map< std::string, std::string > &rinfo, int m=0) const 
 
bool removeMultiResolution()
Remove/Destroy a sub-sampled multi-resolution pyramid, if there is one. 
 
#define TE_TR(message)
It marks a string in order to get translated. 
 
InterpolationMethod
Allowed interpolation methods. 
 
GDALDataset * m_gdataset
Gdal data set handler. 
 
te::dt::AbstractData * clone() const 
It returns a clone of this object. 
 
std::string GetGDALRessamplingMethod(te::rst::InterpolationMethod interpolationMethod)
It translates a TerraLib interpolation method into a GDAL ressampling method name string...
 
This class represents raster band description. 
 
An exception class for the GDAL module. 
 
Grid * m_grid
The spatial support for raster data. 
 
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. 
 
std::auto_ptr< DataSetUseCounter > m_dsUseCounterPtr
Dataset use counter pointer. 
 
GDALDataset * GetRasterHandle(std::string strAccessInfo, te::common::AccessPolicy policy=te::common::RAccess)
Get a handle to a raster file. 
 
std::size_t getNumberOfBands() const 
Returns the number of bands (dimension of cells attribute values) in the raster. 
 
An Envelope defines a 2D rectangular region. 
 
bool createMultiResolution(const unsigned int levels, const te::rst::InterpolationMethod interpMethod)
Create a sub-sampled multi-resolution pyramid. 
 
An abstract class for raster data strucutures. 
 
std::map< std::string, std::string > getInfo() const 
It returns additional information about the raster. 
 
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name. 
 
std::string m_myURI
This instance URI;. 
 
unsigned int getMultiResLevelsCount() const 
Returns the current number of multi-resolution pyramid levels. 
 
A base class for values that can be retrieved from the data access module. 
 
A raster band description. 
 
virtual Raster & operator=(const Raster &rhs)
Assignment operator. 
 
int getBandDataType(std::size_t i) const 
Returns the data type in a particular band (or dimension). 
 
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. 
 
TERASTEREXPORT te::rst::Raster * Reproject(te::rst::Raster const *const rin, int srid, const std::map< std::string, std::string > &routinfo, int m=te::rst::NearestNeighbor)
Reprojects a raster to another SRS. 
 
bool RecognizesSRID(unsigned int srid)
It returns true if GDAL recognizes the given SRS id. 
 
static Raster * make()
It creates and returns an empty raster with default raster driver. 
 
GDAL data set use counter. 
 
std::vector< Band * > m_bands
The vector of available bands in the raster. 
 
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string. 
 
void create(te::rst::Grid *g, const std::vector< te::rst::BandProperty * > bands, const std::map< std::string, std::string > &rinfo, void *h, void(*deleter)(void *))
 
A rectified grid is the spatial support for raster data. 
 
TEGDALEXPORT te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset. 
 
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
 
double getHeight() const 
It returns the envelope height. 
 
te::common::AccessPolicy m_policy
The access policy, can be te::common::{NoAccess, RAccess, RWAccess, WAccess}. 
 
bool isValid() const 
It tells if the rectangle is valid or not. 
 
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver. 
 
const te::rst::Band * getBand(std::size_t i) const 
Returns the raster i-th band.