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 <gdal_priv.h>
54 #include <ogr_spatialref.h>
57 #include <boost/lexical_cast.hpp>
82 throw Exception(
TR_GDAL(
"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;
146 std::string driverName =
GetDriverName(m_gdataset->GetDescription());
148 if ((driverName ==
"PNG" || driverName ==
"JPEG") &&
151 char** papszOptions = 0;
153 GDALDriver* driverPtr = GetGDALDriverManager()->GetDriverByName(driverName.c_str());
155 GDALDataset* poDataset = driverPtr->CreateCopy(m_gdataset->GetDescription(),
156 m_gdataset, 0, papszOptions,
159 GDALClose(poDataset);
162 GDALClose(m_gdataset);
174 std::map<std::string, std::string>::const_iterator it = rinfo.find(
"URI");
177 if(it == rinfo.end())
179 it = rinfo.find(
"SOURCE");
181 if(it == rinfo.end())
182 throw Exception(
TR_GDAL(
"At least the URI or SOURCE parameter must be informed!"));
185 m_myURI = it->second;
194 throw Exception(
TR_GDAL(
"Data file can not be accessed."));
202 m_name = m_gdataset->GetDescription();
207 return std::map<std::string, std::string>();
212 return m_bands.size();
217 assert(i < m_bands.size());
219 return m_bands[i]->getProperty()->getType();
224 assert(i < m_bands.size());
231 assert(i < m_bands.size());
238 assert(i < m_bands.size());
245 assert(i < m_bands.size());
274 if (!(scale < 0 && method == 3))
280 std::vector<te::rst::BandProperty*> bands;
282 for (std::size_t b = 0; b < getNumberOfBands(); b++)
287 int overviewScale[1] = { -scale };
291 GDALDataset* outds =
static_cast<te::gdal::Raster*
>(rout)->getGDALDataset();
293 int overviewIndex = -1;
294 for (
int ov = 0; ov < inds->GetRasterBand(1)->GetOverviewCount(); ov++)
295 if (inds->GetRasterBand(1)->GetOverview(ov)->GetXSize() == outds->GetRasterBand(1)->GetXSize())
298 ov = inds->GetRasterBand(1)->GetOverviewCount();
301 if (overviewIndex == -1)
303 inds->BuildOverviews(
"CUBIC", 1, overviewScale, 0, NULL, GDALDummyProgress, NULL);
305 overviewIndex = inds->GetRasterBand(1)->GetOverviewCount() - 1;
308 GByte* buffer = (GByte*) malloc(outds->GetRasterXSize() * outds->GetRasterYSize() *
sizeof(GByte*));
311 outds->GetGeoTransform(geoT);
312 outds->SetGeoTransform(geoT);
314 for (
int b = 0; b < inds->GetRasterCount(); b++)
316 GDALRasterBand* outband = outds->GetRasterBand(b + 1);
318 GDALRasterBand* inband = inds->GetRasterBand(b + 1)->GetOverview(overviewIndex);
320 inband->RasterIO(GF_Read, 0, 0, inband->GetXSize(), inband->GetYSize(),
321 buffer, inband->GetXSize(), inband->GetYSize(), GDT_Byte, 0, 0);
323 outband->RasterIO(GF_Write, 0, 0, inband->GetXSize(), inband->GetYSize(),
324 buffer, inband->GetXSize(), inband->GetYSize(), GDT_Byte, 0, 0);
333 std::map<std::string, std::string>::const_iterator it = rinfo.find(
"USE_TERRALIB_REPROJECTION");
335 if((it != rinfo.end()) &&
338 std::map<std::string, std::string> irinfo(rinfo);
339 std::map<std::string, std::string>::iterator iit = irinfo.find(
"USE_TERRALIB_REPROJECTION");
346 if (srid == getSRID())
350 throw Exception(
TR_GDAL(
"Output SRID not recognized! Expecting a EPSG SRS id."));
352 unsigned int ncols = getNumberOfColumns();
353 unsigned int nrows = getNumberOfRows();
364 ncols =
static_cast<unsigned int>((urx-llx)/getResolutionX())+1;
366 nrows =
static_cast<unsigned int>((ury-lly)/getResolutionY())+1;
372 if (resx == 0 || resy == 0)
380 ncols =
static_cast<unsigned int>(env->
getWidth()/resx) + 1;
382 nrows =
static_cast<unsigned int>(env->
getHeight()/resy) + 1;
388 std::vector<te::rst::BandProperty*> bands;
389 for (
unsigned int b=0; b<this->getNumberOfBands(); ++b)
417 const std::vector<te::rst::BandProperty*> bands,
418 const std::map<std::string, std::string>& rinfo,
419 void* h,
void (*deleter)(
void*))
430 intptr_t buffaddress = (intptr_t) h;
432 std::string memraster =
"MEM:::DATAPOINTER=";
433 memraster += boost::lexical_cast<std::string>(buffaddress);
434 memraster +=
",PIXELS=";
436 memraster +=
",LINES=";
438 memraster +=
",BANDS=";
439 memraster += boost::lexical_cast<std::string>(bands.size());
440 memraster +=
",DATATYPE=";
441 memraster += GDALGetDataTypeName(
GetGDALDataType(bands[0]->getType()));
447 std::map<std::string, std::string>::const_iterator it = rinfo.find(
"URI");
448 if(it == rinfo.end())
450 it = rinfo.find(
"SOURCE");
452 if(it == rinfo.end())
453 throw Exception(
TR_GDAL(
"At least the URI or SOURCE parameter must be informed!"));
456 m_myURI = it->second;
469 std::string mess =
TR_GDAL(
"Raster couldn't be created:");
471 throw Exception(mess);
Raster & operator=(const Raster &rhs)
bool isValid() const
It tells if the rectangle is valid or not.
bool RecognizesSRID(unsigned int srid)
It returns true if GDAL recognizes the given SRS id.
int getBandDataType(std::size_t i) const
Returns the data type in a particular band (or dimension).
double getWidth() const
It returns the envelope width.
GDALDataType GetGDALDataType(int tet)
It translates a TerraLib DataType to a GDAL DataType.
This class represents Raster data.
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...
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.
static Raster * make()
It creates and returns an empty raster with default raster driver.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Grid * m_grid
The spatial support for raster data.
double getHeight() const
It returns the envelope height.
GDAL data set use counter.
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).
std::string m_myURI
This instance URI;.
te::rst::Raster * resample(int method, int scale, const std::map< std::string, std::string > &rinfo)
Resample raster.
A raster band description.
A rectified grid is the spatial support for raster data.
std::map< std::string, std::string > getInfo() const
It returns additional information about the raster.
~Raster()
Virtual destructor.
std::auto_ptr< DataSetUseCounter > m_dsUseCounterPtr
Dataset use counter pointer.
std::vector< Band * > m_bands
The vector of available bands in the raster.
const te::rst::Band & operator[](std::size_t i) const
Access band in i position.
GDALDataset * m_gdataset
Gdal data set handler.
te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset.
bool IsSubDataSet(const std::string &uri)
Returns true if the given URI is related to a sub-dataset.
An exception class for the GDAL module.
This class represents raster band description.
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::Interpolator::NearestNeighbor)
Reprojects a raster to another SRS.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
virtual Raster & operator=(const Raster &rhs)
Assignment operator.
void open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
Opens a raster.
A base class for values that can be retrieved from the data access module.
A raster band description.
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.
This is a class that represents a GDAL Raster.
const te::rst::Band * getBand(std::size_t i) const
Returns the raster i-th band.
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.
te::common::AccessPolicy m_policy
The access policy, can be te::common::{NoAccess, RAccess, RWAccess, WAccess}.
An abstract class for raster data strucutures.
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)
Resample a subset of the raster, given a box.
std::size_t getNumberOfBands() const
Returns the number of bands (dimension of cells attribute values) in the raster.
GDALDataset * getGDALDataset() const
Returns the raster GDAL handler.
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 *))
std::string GetDriverName(const std::string &dsName)
It returns the GDAL driver name associated to a data source name.
An Envelope defines a 2D rectangular region.
te::dt::AbstractData * clone() const
It returns a clone of this object.
#define TR_GDAL(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
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