27 #include "../common/STLUtils.h" 28 #include "../common/StringUtils.h" 29 #include "../core/translator/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> 90 const std::vector<te::rst::BandProperty*>& bprops,
91 const std::map<std::string, std::string>& optParams,
96 create(grid, bprops, optParams,
nullptr,
nullptr);
167 if ((driverName ==
"PNG" || driverName ==
"JPEG") &&
170 char** papszOptions =
nullptr;
172 GDALDriver* driverPtr = GetGDALDriverManager()->GetDriverByName(driverName.c_str());
174 GDALDataset* poDataset = driverPtr->CreateCopy(
m_gdataset->GetDescription(),
178 GDALClose(poDataset);
193 std::map<std::string, std::string>::const_iterator it = rinfo.find(
"URI");
196 if(it == rinfo.end())
198 it = rinfo.find(
"SOURCE");
200 if(it == rinfo.end())
201 throw Exception(
TE_TR(
"At least the URI or SOURCE parameter must be informed!"));
226 std::map<std::string, std::string> info;
242 return m_bands[i]->getProperty()->getType();
297 if (!(scale < 0 && method == 3))
303 std::vector<te::rst::BandProperty*>
bands;
310 int overviewScale[1] = { -scale };
316 int overviewIndex = -1;
317 for (
int ov = 0; ov < inds->GetRasterBand(1)->GetOverviewCount(); ov++)
318 if (inds->GetRasterBand(1)->GetOverview(ov)->GetXSize() == outds->GetRasterBand(1)->GetXSize())
321 ov = inds->GetRasterBand(1)->GetOverviewCount();
324 if (overviewIndex == -1)
326 inds->BuildOverviews(
"CUBIC", 1, overviewScale, 0,
nullptr, GDALDummyProgress,
nullptr);
328 overviewIndex = inds->GetRasterBand(1)->GetOverviewCount() - 1;
331 GByte* buffer =
reinterpret_cast<GByte*
>(malloc(static_cast<size_t>(outds->GetRasterXSize() * outds->GetRasterYSize()) *
sizeof(GByte*)));
334 outds->GetGeoTransform(geoT);
335 outds->SetGeoTransform(geoT);
337 for (
int b = 0;
b < inds->GetRasterCount();
b++)
339 GDALRasterBand* outband = outds->GetRasterBand(
b + 1);
341 GDALRasterBand* inband = inds->GetRasterBand(
b + 1)->GetOverview(overviewIndex);
343 CPLErr error = inband->RasterIO(GF_Read, 0, 0, inband->GetXSize(), inband->GetYSize(),
344 buffer, inband->GetXSize(), inband->GetYSize(), GDT_Byte, 0, 0);
346 if(error == CE_Failure || error == CE_Fatal)
351 error = outband->RasterIO(GF_Write, 0, 0, inband->GetXSize(), inband->GetYSize(),
352 buffer, inband->GetXSize(), inband->GetYSize(), GDT_Byte, 0, 0);
354 if(error == CE_Failure || error == CE_Fatal)
365 std::map<std::string, std::string>::const_iterator it = rinfo.find(
"USE_TERRALIB_REPROJECTION");
375 ( !sridIsRecognized )
381 std::map<std::string, std::string> irinfo(rinfo);
382 std::map<std::string, std::string>::iterator iit = irinfo.find(
"USE_TERRALIB_REPROJECTION");
412 if (resx == 0 || resy == 0)
420 ncols =
static_cast<unsigned int>(env->
getWidth()/resx) + 1;
422 nrows =
static_cast<unsigned int>(env->
getHeight()/resy) + 1;
428 std::vector<te::rst::BandProperty*>
bands;
457 const std::vector<te::rst::BandProperty*>
bands,
458 const std::map<std::string, std::string>& rinfo,
459 void* h,
void (*deleter)(
void*))
470 intptr_t buffaddress =
reinterpret_cast<intptr_t
>(h);
472 std::string memraster =
"MEM:::DATAPOINTER=";
473 memraster += boost::lexical_cast<std::string>(buffaddress);
474 memraster +=
",PIXELS=";
476 memraster +=
",LINES=";
478 memraster +=
",BANDS=";
479 memraster += boost::lexical_cast<std::string>(bands.size());
480 memraster +=
",DATATYPE=";
481 memraster += GDALGetDataTypeName(
GetGDALDataType(bands[0]->getType()));
487 std::map<std::string, std::string>::const_iterator it = rinfo.find(
"URI");
488 if(it == rinfo.end())
490 it = rinfo.find(
"SOURCE");
492 if(it == rinfo.end())
493 throw Exception(
TE_TR(
"At least the URI or SOURCE parameter must be informed!"));
509 std::string mess =
TE_TR(
"Raster couldn't be created:");
531 boost::scoped_array< int > overviewsIndexes(
new int[ levels ] );
532 for(
unsigned int overViewIdx = 0 ; overViewIdx < levels ; ++overViewIdx )
534 overviewsIndexes[ overViewIdx ] =
static_cast<int>(overViewIdx) + 1;
541 CPLErr returnValue =
m_gdataset->BuildOverviews(
554 static_cast<int>(levels),
555 overviewsIndexes.get(),
565 if( returnValue == CE_Failure )
591 if(
m_gdataset->GetRasterBand( 1 )->GetOverviewCount() > 0 )
593 CPLErr returnValue =
m_gdataset->BuildOverviews(
604 if( returnValue == CE_Failure )
635 return static_cast<unsigned int>(
m_gdataset->GetRasterBand( 1 )->GetOverviewCount());
654 if( level <= (static_cast<unsigned int>(
m_gdataset->GetRasterBand( 1 )->GetOverviewCount())) )
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.
unsigned int getNumberOfRows() const
Returns the grid number of rows.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
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.
A raster band description.
This class represents Raster data.
Base exception class for plugin module.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
const te::rst::Band & operator[](std::size_t i) const
Access band in i position.
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.
This is a class that represents a GDAL Raster.
#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.
std::string m_name
The raster name.
Grid * m_grid
The spatial support for raster data.
Grid * getResampledGrid(int scale) const
Return the raster grid for a specific scale.
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.
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.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
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 getNumberOfColumns() const
Returns the grid number of columns.
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
void(* m_deleter)(void *)
A pointer to a deleter function, if the buffer needs to be deleted by this object.
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.
Grid * getGrid()
It returns the raster grid.
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.
int getSRID() const
Returns the raster spatial reference system identifier.
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.
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
std::unique_ptr< DataSetUseCounter > m_dsUseCounterPtr
Dataset use counter pointer.
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.
It gives access to values in one band (dimension) of a raster.
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.
An exception class for the GDAL module.
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.
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
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.