26 #include <boost/algorithm/string.hpp>
27 #include <boost/format.hpp>
28 #include <boost/filesystem.hpp>
31 #include "../common/StringUtils.h"
32 #include "../common/Translator.h"
33 #include "../dataaccess/dataset/DataSetType.h"
34 #include "../dataaccess/query/DataSetName.h"
35 #include "../dataaccess/query/Select.h"
36 #include "../dataaccess/query/From.h"
37 #include "../dataaccess/query/FromItem.h"
38 #include "../geometry/Envelope.h"
39 #include "../raster/Grid.h"
40 #include "../raster/Raster.h"
41 #include "../raster/RasterProperty.h"
50 #include <gdal_priv.h>
59 GDALDataset* ds =
static_cast<GDALDataset*
>(GDALOpen(dsfullname.c_str(), GA_ReadOnly));
62 return std::auto_ptr<te::da::DataSetType>();
70 std::vector<te::rst::BandProperty*> bprops;
76 for (
size_t i=0; i<bprops.size(); ++i)
83 return std::auto_ptr<te::da::DataSetType>(ptr);
87 m_path (boost::filesystem::path(accessInfo))
102 if (boost::filesystem::is_regular_file(path))
106 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
110 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
113 dsnames.push_back(path.leaf().string());
118 for(
char** i = subdatasets; *i != 0; ++i)
120 std::map<std::string, std::string> sdsmap;
124 if(sdsmap.begin()->first.find(
"_NAME") != std::string::npos)
126 std::string fullName = sdsmap.begin()->second;
128 dsnames.push_back(subdsname);
135 for (boost::filesystem::directory_iterator it(path), itEnd; it !=itEnd; ++it)
136 getDataSetNames(*it,dsnames);
143 std::vector<std::string> dsnames;
145 getDataSetNames(m_path,dsnames);
152 if (boost::filesystem::is_regular_file(path))
156 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
164 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
165 if (hasDataSets(*it))
173 return hasDataSets(m_path);
179 if (boost::filesystem::is_regular_file(path))
183 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
186 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
192 for(
char** i = subdatasets; *i != 0; ++i, ++nds);
199 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
200 nds+= getNumberOfDataSets(*it);
207 return getNumberOfDataSets(m_path);
213 return getDataSetType(m_path,name,uri);
218 if (boost::filesystem::is_regular_file(path))
220 if (path.leaf() == name)
223 std::auto_ptr<te::da::DataSetType> dsty = getType(path.string());
225 dsty->setTitle(name);
234 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
236 return std::auto_ptr<te::da::DataSetType>();
238 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
242 return std::auto_ptr<te::da::DataSetType>();
245 for(
char** i = subdatasets; *i != 0; i=i+2)
247 std::string sds_name = std::string(*i);
248 std::string sds_desc = std::string(*(i+1));
250 unsigned pos = sds_name.find(
"=");
251 std::string val = sds_name.substr(++pos);
257 std::auto_ptr<te::da::DataSetType> dsty = getType(val);
260 pos = sds_desc.find(
"=");
261 val = sds_desc.substr(++pos);
272 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
274 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(*it,name,uri);
279 return std::auto_ptr<te::da::DataSetType>();
289 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(m_path,name,uri);
291 return std::auto_ptr<te::da::DataSet>();
293 return std::auto_ptr<te::da::DataSet>(
new DataSet(dsty,accessPolicy, uri));
304 return getDataSet(name, travType, connected, accessPolicy);
315 return getDataSet(name, travType, connected, accessPolicy);
324 return getDataSet(name, travType, connected, accessPolicy);
330 boost::ptr_vector<te::dt::Property> properties;
332 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
334 return boost::ptr_vector<te::dt::Property>();
336 const std::vector<te::dt::Property*>& props = dsty->getProperties();
337 for(std::size_t i = 0; i < props.size(); ++i)
338 properties.push_back(props[i]->clone());
345 boost::ptr_vector<te::dt::Property> properties;
347 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
349 return std::auto_ptr<te::dt::Property>();
351 const std::vector<te::dt::Property*>& props = dsty->getProperties();
352 for(std::size_t i = 0; i < props.size(); ++i)
354 if (props[i]->getName() == name)
355 return std::auto_ptr<te::dt::Property>(props[i]->clone());
358 return std::auto_ptr<te::dt::Property>();
363 boost::ptr_vector<te::dt::Property> properties;
365 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
367 return std::auto_ptr<te::dt::Property>();
369 const std::vector<te::dt::Property*>& props = dsty->getProperties();
370 if (propertyPos<props.size() && propertyPos>0)
371 return std::auto_ptr<te::dt::Property>(props[propertyPos]->clone());
373 return std::auto_ptr<te::dt::Property>();
378 std::vector<std::string> pNames;
380 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
383 const std::vector<te::dt::Property*>& props = dsty->getProperties();
384 for(std::size_t i = 0; i < props.size(); ++i)
385 pNames.push_back(props[i]->getName());
392 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
394 return dsty->getProperties().size();
401 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
404 const std::vector<te::dt::Property*>& props = dsty->getProperties();
405 for(std::size_t i = 0; i < props.size(); ++i)
406 if(props[i]->getName()==name)
414 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
416 return (propertyPos < dsty->getProperties().size() && propertyPos>0);
429 throw Exception(
TE_TR(
"Can not process the Select object."));
434 throw Exception(
TE_TR(
"Can not process the Select object."));
436 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(dsname->
getName());
441 throw Exception(
TE_TR(
"Can not process the Select object: dataset not found."));
443 std::string uri = dsty->getTitle();
444 return std::auto_ptr<te::da::DataSet>(
new DataSet(dsty,accessPolicy,uri));
452 std::vector<std::string> words;
454 boost::split(words, s, boost::is_any_of(
", "), boost::token_compress_on);
456 std::vector<std::string>::const_iterator it = std::find(words.begin(), words.end(),
"FROM");
457 if (it== words.end())
458 it = std::find(words.begin(), words.end(),
"from");
460 if (it== words.end())
461 throw Exception(
TE_TR(
"Can not process the query expression."));
464 if (it== words.end())
465 throw Exception(
TE_TR(
"Can not process the query expression."));
467 std::string dsname = *it;
469 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(dsname);
471 throw Exception(
TE_TR(
"Can not process the Select object: dataset not found."));
473 std::string uri = dsty->getTitle();
474 return std::auto_ptr<te::da::DataSet>(
new DataSet(dsty,accessPolicy,uri));
479 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
481 return (dsty.get() != 0);
486 std::auto_ptr<te::dt::Property> pp = getProperty(datasetName,propertyPos);
495 return std::auto_ptr<te::gm::Envelope>();
500 std::auto_ptr<te::dt::Property> pp = getProperty(datasetName,propertyName);
509 return std::auto_ptr<te::gm::Envelope>();
513 const std::map<std::string, std::string>& options)
515 if (!boost::filesystem::is_directory(m_path))
516 throw Exception(
TE_TR(
"Create operation supported just on directory data sources."));
520 boost::filesystem::path paux(m_path);
523 if (boost::filesystem::exists(paux))
524 throw Exception((boost::format(
TE_TR(
"The datasource already has a dataset with this name (\"%1%\")!")) % dt->
getName()).str());
531 throw Exception(
TE_TR(
"GDAL driver couldn't persist the raster file."));
537 const std::string& cloneName,
538 const std::map<std::string, std::string>& options)
540 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
543 throw Exception(
TE_TR(
"Dataset does not exist."));
545 boost::filesystem::path mpath(dsty->getTitle());
547 if (!boost::filesystem::is_regular_file(mpath))
548 throw Exception(
TE_TR(
"Can not clone a dataset that it is not a raster file."));
550 boost::filesystem::path newpath(mpath.parent_path() /= cloneName);
551 boost::filesystem::copy_file(mpath, newpath);
557 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
560 throw Exception(
TE_TR(
"Dataset does not exist."));
562 boost::filesystem::path mpath(dsty->getTitle());
563 if (!boost::filesystem::is_regular_file(mpath))
564 throw Exception(
TE_TR(
"Can not drop a dataset that it is not a raster file."));
566 boost::filesystem::remove(mpath.string());
571 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
574 throw Exception(
TE_TR(
"Dataset does not exist."));
576 boost::filesystem::path mpath(dsty->getTitle());
577 if (!boost::filesystem::is_regular_file(mpath))
578 throw Exception(
TE_TR(
"Can not rename a dataset that it is not a raster file."));
580 boost::filesystem::path newpath(mpath.parent_path() /= newName);
581 boost::filesystem::rename(mpath, newpath);
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
Property * getProperty(std::size_t i) const
It returns the i-th property.
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.
CharEncoding
Supported charsets (character encoding).
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
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.
A class that models the description of a dataset.
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
void ExtractKVP(const std::string &kvpStr, std::map< std::string, std::string > &kvp, const std::string &kvpDelimiter="&", const std::string &kvDelimiter="=", bool toUpper=false)
It extracts a key-value map from a string.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
SpatialRelation
Spatial relations between geometric objects.
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. ...
std::auto_ptr< te::da::DataSetType > getType(const std::string &dsfullname)
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.
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.
An exception class for the GDAL module.
te::common::CharEncoding getEncoding()
It return the DataSource current encoding.
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.
bool propertyExists(const std::string &datasetName, const std::string &name)
It checks if a property with the given name exists in the dataset.
std::auto_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.
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::auto_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...
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name.
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
std::auto_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.
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.
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.
void add(Constraint *c)
It adds a new constraint.
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.
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.
A GDAL data set gives access to a raster file.
const std::string & getName() const
It returns the dataset name.
std::vector< te::rst::BandProperty * > & getBandProperties()
Returns a reference to the list of bands definitions.
std::auto_ptr< te::dt::Property > getProperty(const std::string &datasetName, const std::string &name)
It retrieves the property with the given name from the dataset.
A rectified grid is the spatial support for raster data.
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.
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
The implementation of a DataSource that consists of datasets that can be decoded by the GDAL Library...
Select & from(const FromItem &item)
bool hasDataSets()
It checks if the data source has any dataset.
const std::string & getName() const
It returns the property name.
std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
Transactor(const std::string &accessInfo)