26 #include <boost/algorithm/string.hpp> 27 #include <boost/format.hpp> 28 #include <boost/filesystem.hpp> 31 #include "../common/StringUtils.h" 32 #include "../core/filesystem/FileSystem.h" 33 #include "../core/translator/Translator.h" 34 #include "../dataaccess/dataset/DataSetType.h" 35 #include "../dataaccess/query/DataSetName.h" 36 #include "../dataaccess/query/Select.h" 37 #include "../dataaccess/query/From.h" 38 #include "../dataaccess/query/FromItem.h" 39 #include "../geometry/Envelope.h" 40 #include "../raster/Grid.h" 41 #include "../raster/Raster.h" 42 #include "../raster/RasterProperty.h" 51 #include <gdal_priv.h> 60 GDALDataset*
ds =
static_cast<GDALDataset*
>(GDALOpen(dsfullname.c_str(), GA_ReadOnly));
63 return std::unique_ptr<te::da::DataSetType>(
nullptr);
67 ptr->setTitle(
"raster");
71 std::vector<te::rst::BandProperty*> bprops;
77 for (
size_t i=0; i<bprops.size(); ++i)
84 return std::unique_ptr<te::da::DataSetType>(ptr);
104 boost::filesystem::path path(source);
107 if( upcaseExtension[ 0 ] ==
'.' ) upcaseExtension = upcaseExtension.substr( 1, upcaseExtension.size() - 1);
109 std::multimap< std::string, std::string > extensionsMap =
112 std::pair< std::multimap< std::string, std::string >::const_iterator,
113 std::multimap< std::string, std::string >::const_iterator > extensionsRangeIts =
114 extensionsMap.equal_range( upcaseExtension );
116 if( extensionsRangeIts.first != extensionsRangeIts.second )
118 bool subDatasetsSupport =
false;
119 std::map< std::string, DriverMetadata >::const_iterator metaIt;
120 while( extensionsRangeIts.first != extensionsRangeIts.second )
124 if( metaIt->second.m_subDatasetsSupport )
126 subDatasetsSupport =
true;
129 ++extensionsRangeIts.first;
132 if( subDatasetsSupport )
136 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
140 char** subDataSetsMetadataPtr = gds->GetMetadata(
"SUBDATASETS");
141 if(subDataSetsMetadataPtr ==
nullptr)
143 dsnames.push_back(path.filename().string());
148 const int subDataSetsMetadataSize = CSLCount( subDataSetsMetadataPtr );
149 const char* valuePtr =
nullptr;
150 char* namePtr =
nullptr;
152 std::string valueStr;
153 std::string subdsname ;
155 for(
int subDataSetsMetadataIdx = 0 ; subDataSetsMetadataIdx < subDataSetsMetadataSize ;
156 ++subDataSetsMetadataIdx )
158 valuePtr = CPLParseNameValue( subDataSetsMetadataPtr[ subDataSetsMetadataIdx ],
161 nameStr = std::string( namePtr );
163 if( nameStr.find(
"_NAME" ) == ( nameStr.size() - 5 ) )
165 valueStr = std::string( valuePtr );
167 dsnames.push_back(subdsname);
175 dsnames.push_back(path.leaf().string());
182 boost::filesystem::path path(source);
184 for (boost::filesystem::directory_iterator it(path), itEnd; it !=itEnd; ++it)
191 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(source.c_str(), GA_ReadOnly));
195 dsnames.push_back(
"pg_raster");
206 std::vector<std::string> dsnames;
225 std::vector<std::string> dsnames;
227 return dsnames.size();
245 boost::filesystem::path path(source);
247 if (path.leaf() == name)
250 std::unique_ptr<te::da::DataSetType> dsty =
getType(path.string());
254 dsty->setTitle(name);
259 return std::unique_ptr<te::da::DataSetType>(
nullptr);
264 if( upcaseExtension[ 0 ] ==
'.' ) upcaseExtension = upcaseExtension.substr( 1, upcaseExtension.size() - 1);
266 std::multimap< std::string, std::string > extensionsMap =
269 std::pair< std::multimap< std::string, std::string >::const_iterator,
270 std::multimap< std::string, std::string >::const_iterator > extensionsRangeIts =
271 extensionsMap.equal_range( upcaseExtension );
273 if( extensionsRangeIts.first != extensionsRangeIts.second )
275 bool subDatasetsSupport =
false;
276 std::map< std::string, DriverMetadata >::const_iterator metaIt;
277 while( extensionsRangeIts.first != extensionsRangeIts.second )
281 if( metaIt->second.m_subDatasetsSupport )
283 subDatasetsSupport =
true;
286 ++extensionsRangeIts.first;
289 if( subDatasetsSupport )
294 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
296 return std::unique_ptr<te::da::DataSetType>(
nullptr);
298 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
299 if(subdatasets ==
nullptr)
302 return std::unique_ptr<te::da::DataSetType>(
nullptr);
305 for(
char** i = subdatasets; *i !=
nullptr; i=i+2)
307 std::string sds_name = std::string(*i);
308 std::string sds_desc = std::string(*(i+1));
310 unsigned pos = (unsigned)sds_name.find(
"=");
311 std::string val = sds_name.substr(++pos);
317 std::unique_ptr<te::da::DataSetType> dsty =
getType(val);
320 pos = (unsigned)sds_desc.find(
"=");
321 val = sds_desc.substr(++pos);
331 return std::unique_ptr<te::da::DataSetType>(
nullptr);
336 return std::unique_ptr<te::da::DataSetType>(
nullptr);
342 boost::filesystem::path path(source);
344 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
346 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(it->path().string(),name,uri);
353 std::unique_ptr<te::da::DataSetType> dsty =
getType(source);
357 dsty->setTitle(name);
362 return std::unique_ptr<te::da::DataSetType>(
nullptr);
365 return std::unique_ptr<te::da::DataSetType>(
nullptr);
375 return std::unique_ptr<te::da::DataSet>(
nullptr);
377 return std::unique_ptr<te::da::DataSet>(
new DataSet(std::move(dsty),accessPolicy, uri));
388 return getDataSet(name, travType, connected, accessPolicy);
399 return getDataSet(name, travType, connected, accessPolicy);
408 return getDataSet(name, travType, connected, accessPolicy);
414 boost::ptr_vector<te::dt::Property> properties;
416 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
418 return boost::ptr_vector<te::dt::Property>();
420 const std::vector<te::dt::Property*>& props = dsty->getProperties();
421 for(std::size_t i = 0; i < props.size(); ++i)
422 properties.push_back(props[i]->clone());
429 boost::ptr_vector<te::dt::Property> properties;
431 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
433 return std::unique_ptr<te::dt::Property>(
nullptr);
435 const std::vector<te::dt::Property*>& props = dsty->getProperties();
436 for(std::size_t i = 0; i < props.size(); ++i)
438 if (props[i]->getName() == name)
439 return std::unique_ptr<te::dt::Property>(props[i]->clone());
442 return std::unique_ptr<te::dt::Property>(
nullptr);
447 boost::ptr_vector<te::dt::Property> properties;
449 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
451 return std::unique_ptr<te::dt::Property>(
nullptr);
453 const std::vector<te::dt::Property*>& props = dsty->getProperties();
454 if (propertyPos<props.size() && propertyPos>0)
455 return std::unique_ptr<te::dt::Property>(props[propertyPos]->clone());
457 return std::unique_ptr<te::dt::Property>(
nullptr);
462 std::vector<std::string> pNames;
464 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
467 const std::vector<te::dt::Property*>& props = dsty->getProperties();
468 for(std::size_t i = 0; i < props.size(); ++i)
469 pNames.push_back(props[i]->getName());
476 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
478 return dsty->getProperties().size();
485 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
488 const std::vector<te::dt::Property*>& props = dsty->getProperties();
489 for(std::size_t i = 0; i < props.size(); ++i)
490 if(props[i]->getName()==name)
498 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(datasetName);
500 return (propertyPos < dsty->
getProperties().size() && propertyPos>0);
525 throw Exception(
TE_TR(
"Can not process the Select object: dataset not found."));
527 std::string uri = dsty->getTitle();
528 return std::unique_ptr<te::da::DataSet>(
new DataSet(std::move(dsty),accessPolicy,uri));
535 std::vector<std::string> words;
537 boost::split(words, s, boost::is_any_of(
", "), boost::token_compress_on);
539 std::vector<std::string>::const_iterator it = std::find(words.begin(), words.end(),
"FROM");
540 if (it== words.end())
541 it = std::find(words.begin(), words.end(),
"from");
543 if (it== words.end())
547 if (it== words.end())
550 std::string dsname = *it;
552 std::unique_ptr<te::da::DataSetType> dsty =
getDataSetType(dsname);
554 throw Exception(
TE_TR(
"Can not process the Select object: dataset not found."));
556 std::string uri = dsty->getTitle();
557 return std::unique_ptr<te::da::DataSet>(
new DataSet(std::move(dsty),accessPolicy,uri));
564 return (dsty.get() !=
nullptr);
569 std::unique_ptr<te::dt::Property> pp =
getProperty(datasetName,propertyPos);
578 return std::unique_ptr<te::gm::Envelope>(
nullptr);
583 std::unique_ptr<te::dt::Property> pp =
getProperty(datasetName,propertyName);
592 return std::unique_ptr<te::gm::Envelope>(
nullptr);
596 const std::map<std::string, std::string>& options)
599 throw Exception(
TE_TR(
"Create operation supported just on directory data sources."));
603 boost::filesystem::path paux(
m_source);
607 throw Exception((boost::format(
TE_TR(
"This is not a valid file: (\"%1%\")!")) % dt->
getName()).str());
610 throw Exception((boost::format(
TE_TR(
"The datasource already has a dataset with this name (\"%1%\")!")) % dt->
getName()).str());
617 throw Exception(
TE_TR(
"GDAL driver couldn't persist the raster file."));
623 const std::string& name,
const std::string& cloneName,
624 const std::map<std::string, std::string>& )
631 boost::filesystem::path mpath(dsty->getTitle());
634 throw Exception(
TE_TR(
"Can not clone a dataset that it is not a raster file."));
636 boost::filesystem::path newpath(mpath.parent_path() /= cloneName);
648 boost::filesystem::path mpath(dsty->getTitle());
650 throw Exception(
TE_TR(
"Can not drop a dataset that it is not a raster file."));
662 boost::filesystem::path mpath(dsty->getTitle());
664 throw Exception(
TE_TR(
"Can not rename a dataset that it is not a raster file."));
666 boost::filesystem::path newpath(mpath.parent_path() /= newName);
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
Property * getProperty(std::size_t i) const
It returns the i-th property.
The implementation of a DataSource that consists of datasets that can be decoded by the GDAL Library...
std::multimap< std::string, std::string > GetGDALAllDriversUCaseExt2DriversMap(const bool creationSupport)
Returns a map of all GDAL supported Upper-case ( vector and raster ) extensions to their respective d...
std::unique_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
void set(te::rst::Grid *grid)
Sets the definition of the raster grid support.
void cloneDataSet(const std::string &name, const std::string &cloneName, const std::map< std::string, std::string > &options)
It clones the dataset in the data source.
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
std::map< std::string, DriverMetadata > & GetGDALDriversMetadata()
Returns metadata from all registered GDAL drivers (key: driver name).
A class that models the name of a dataset used in a From clause.
TEGDALEXPORT void GetBandProperties(GDALDataset *gds, std::vector< te::rst::BandProperty * > &bprops)
Gets the list of bands definition from a GDAL dataset.
Base exception class for plugin module.
A class that models the description of a dataset.
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
std::unique_ptr< te::da::DataSetType > getType(const std::string &dsfullname)
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
SpatialRelation
Spatial relations between geometric objects.
static te::dt::Date ds(2010, 01, 01)
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
#define TE_TR(message)
It marks a string in order to get translated.
std::unique_ptr< te::da::DataSet > query(const te::da::Select &q, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It executes a query that may return some data using a generic query. A dataset can be connected or di...
void createDataSet(te::da::DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
static void rename(const std::string &old_p, const std::string &new_p)
Renames a file or directory from a given path in UTF-8.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
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.
void add(te::rst::BandProperty *b)
It adds a new band information to the property.
static bool remove(const std::string &path)
Removes a file or directory from a given path in UTF-8.
bool propertyExists(const std::string &datasetName, const std::string &name)
It checks if a property with the given name exists in the dataset.
An Envelope defines a 2D rectangular region.
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name.
static te::dt::TimeDuration dt(20, 30, 50, 11)
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
te::da::DataSource * getDataSource() const
It returns the parent data source of the transactor.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A Select models a query to be used when retrieving data from a DataSource.
static void copyFile(const std::string &from, const std::string &to)
Copies a file.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
std::string GetDriverName(const std::string &dsName)
It returns the GDAL driver name associated to a data source name.
te::rst::Grid * getGrid()
Returns the definition of the raster grid support.
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
std::unique_ptr< te::gm::Envelope > getExtent(const std::string &datasetName, const std::string &propertyName)
It retrieves the bounding rectangle of the spatial property for the given dataset.
std::unique_ptr< te::da::DataSet > getDataSet(const std::string &name, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It gets the dataset identified by the given name. A dataset can be connected or disconnected. A connected dataset, after its creation through the data source transactor, continues to depend on the connection given by its associated data source. Differently, a disconnected dataset, after its creation, no more depends of the connection given by the data source, and it continues to live after the connection has been released to the data source.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
GDAL data set use counter.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
const std::string & getName() const
It returns the dataset name.
std::unique_ptr< te::dt::Property > getProperty(const std::string &datasetName, const std::string &name)
It retrieves the property with the given name from the dataset.
std::vector< te::rst::BandProperty * > & getBandProperties()
Returns a reference to the list of bands definitions.
A rectified grid is the spatial support for raster data.
An exception class for the GDAL module.
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.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
TEGDALEXPORT te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset.
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
Select & from(const FromItem &item)
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
bool hasDataSets()
It checks if the data source has any dataset.
const std::string & getName() const
It returns the property name.
Transactor(const std::string &accessInfo)