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))
112 if (boost::filesystem::is_regular_file(path))
116 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
120 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
123 dsnames.push_back(path.leaf().string());
128 for(
char** i = subdatasets; *i != 0; ++i)
130 std::map<std::string, std::string> sdsmap;
134 if(sdsmap.begin()->first.find(
"_NAME") != std::string::npos)
136 std::string fullName = sdsmap.begin()->second;
138 dsnames.push_back(subdsname);
145 for (boost::filesystem::directory_iterator it(path), itEnd; it !=itEnd; ++it)
146 getDataSetNames(*it,dsnames);
153 std::vector<std::string> dsnames;
155 getDataSetNames(m_path,dsnames);
162 if (boost::filesystem::is_regular_file(path))
166 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
174 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
175 if (hasDataSets(*it))
183 return hasDataSets(m_path);
189 if (boost::filesystem::is_regular_file(path))
193 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
196 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
202 for(
char** i = subdatasets; *i != 0; ++i, ++nds);
209 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
210 nds+= getNumberOfDataSets(*it);
217 return getNumberOfDataSets(m_path);
223 return getDataSetType(m_path,name,uri);
228 if (boost::filesystem::is_regular_file(path))
230 if (path.leaf() == name)
233 std::auto_ptr<te::da::DataSetType> dsty = getType(path.string());
235 dsty->setTitle(name);
244 GDALDataset* gds =
static_cast<GDALDataset*
>(GDALOpen(path.string().c_str(), GA_ReadOnly));
246 return std::auto_ptr<te::da::DataSetType>();
248 char** subdatasets = gds->GetMetadata(
"SUBDATASETS");
250 return std::auto_ptr<te::da::DataSetType>();
252 for(
char** i = subdatasets; *i != 0; i=i+2)
254 std::string sds_name = std::string(*i);
255 std::string sds_desc = std::string(*(i+1));
257 unsigned pos = sds_name.find(
"=");
258 std::string val = sds_name.substr(++pos);
264 std::auto_ptr<te::da::DataSetType> dsty = getType(val);
267 pos = sds_desc.find(
"=");
268 val = sds_desc.substr(++pos);
279 for (boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
281 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(*it,name,uri);
286 return std::auto_ptr<te::da::DataSetType>();
296 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(m_path,name,uri);
298 return std::auto_ptr<te::da::DataSet>();
300 return std::auto_ptr<te::da::DataSet>(
new DataSet(dsty,accessPolicy, uri));
311 return getDataSet(name, travType, connected, accessPolicy);
322 return getDataSet(name, travType, connected, accessPolicy);
331 return getDataSet(name, travType, connected, accessPolicy);
337 boost::ptr_vector<te::dt::Property> properties;
339 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
341 return boost::ptr_vector<te::dt::Property>();
343 const std::vector<te::dt::Property*>& props = dsty->getProperties();
344 for(std::size_t i = 0; i < props.size(); ++i)
345 properties.push_back(props[i]->clone());
352 boost::ptr_vector<te::dt::Property> properties;
354 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
356 return std::auto_ptr<te::dt::Property>();
358 const std::vector<te::dt::Property*>& props = dsty->getProperties();
359 for(std::size_t i = 0; i < props.size(); ++i)
361 if (props[i]->getName() == name)
362 return std::auto_ptr<te::dt::Property>(props[i]->clone());
365 return std::auto_ptr<te::dt::Property>();
370 boost::ptr_vector<te::dt::Property> properties;
372 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
374 return std::auto_ptr<te::dt::Property>();
376 const std::vector<te::dt::Property*>& props = dsty->getProperties();
377 if (propertyPos<props.size() && propertyPos>0)
378 return std::auto_ptr<te::dt::Property>(props[propertyPos]->clone());
380 return std::auto_ptr<te::dt::Property>();
385 std::vector<std::string> pNames;
387 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
390 const std::vector<te::dt::Property*>& props = dsty->getProperties();
391 for(std::size_t i = 0; i < props.size(); ++i)
392 pNames.push_back(props[i]->getName());
399 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
401 return dsty->getProperties().size();
408 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
411 const std::vector<te::dt::Property*>& props = dsty->getProperties();
412 for(std::size_t i = 0; i < props.size(); ++i)
413 if(props[i]->getName()==name)
421 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(datasetName);
423 return (propertyPos < dsty->getProperties().size() && propertyPos>0);
436 throw Exception(
TR_GDAL(
"Can not process the Select object."));
441 throw Exception(
TR_GDAL(
"Can not process the Select object."));
443 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(dsname->
getName());
448 throw Exception(
TR_GDAL(
"Can not process the Select object: dataset not found."));
450 std::string uri = dsty->getTitle();
451 return std::auto_ptr<te::da::DataSet>(
new DataSet(dsty,accessPolicy,uri));
459 std::vector<std::string> words;
461 boost::split(words, s, boost::is_any_of(
", "), boost::token_compress_on);
463 std::vector<std::string>::const_iterator it = std::find(words.begin(), words.end(),
"FROM");
464 if (it== words.end())
465 it = std::find(words.begin(), words.end(),
"from");
467 if (it== words.end())
468 throw Exception(
TR_GDAL(
"Can not process the query expression."));
471 if (it== words.end())
472 throw Exception(
TR_GDAL(
"Can not process the query expression."));
474 std::string dsname = *it;
476 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(dsname);
478 throw Exception(
TR_GDAL(
"Can not process the Select object: dataset not found."));
480 std::string uri = dsty->getTitle();
481 return std::auto_ptr<te::da::DataSet>(
new DataSet(dsty,accessPolicy,uri));
486 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
488 return (dsty.get() != 0);
493 std::auto_ptr<te::dt::Property> pp = getProperty(datasetName,propertyPos);
502 return std::auto_ptr<te::gm::Envelope>();
507 std::auto_ptr<te::dt::Property> pp = getProperty(datasetName,propertyName);
516 return std::auto_ptr<te::gm::Envelope>();
520 const std::map<std::string, std::string>& options)
522 if (!boost::filesystem::is_directory(m_path))
523 throw Exception(
TR_GDAL(
"Create operation supported just on directory data sources."));
527 boost::filesystem::path paux(m_path);
530 if (boost::filesystem::exists(paux))
531 throw Exception((boost::format(
TR_GDAL(
"The datasource already has a dataset with this name (\"%1%\")!")) % dt->
getName()).str());
538 throw Exception(
TR_GDAL(
"GDAL driver couldn't persist the raster file."));
544 const std::string& cloneName,
545 const std::map<std::string, std::string>& options)
547 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
550 throw Exception(
TR_GDAL(
"Dataset does not exist."));
552 boost::filesystem::path mpath(dsty->getTitle());
554 if (!boost::filesystem::is_regular_file(mpath))
555 throw Exception(
TR_GDAL(
"Can not clone a dataset that it is not a raster file."));
557 boost::filesystem::path newpath(mpath.parent_path() /= cloneName);
558 boost::filesystem::copy_file(mpath, newpath);
564 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
567 throw Exception(
TR_GDAL(
"Dataset does not exist."));
569 boost::filesystem::path mpath(dsty->getTitle());
570 if (!boost::filesystem::is_regular_file(mpath))
571 throw Exception(
TR_GDAL(
"Can not drop a dataset that it is not a raster file."));
573 boost::filesystem::remove(mpath.string());
578 std::auto_ptr<te::da::DataSetType> dsty = getDataSetType(name);
581 throw Exception(
TR_GDAL(
"Dataset does not exist."));
583 boost::filesystem::path mpath(dsty->getTitle());
584 if (!boost::filesystem::is_regular_file(mpath))
585 throw Exception(
TR_GDAL(
"Can not rename a dataset that it is not a raster file."));
587 boost::filesystem::path newpath(mpath.parent_path() /= newName);
588 boost::filesystem::rename(mpath, newpath);
std::vector< te::rst::BandProperty * > & getBandProperties()
Returns a reference to the list of bands definitions.
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.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
A class that models the name of a dataset used in a From clause.
Property * getProperty(std::size_t i) const
It returns the i-th property.
bool propertyExists(const std::string &datasetName, const std::string &name)
It checks if a property with the given name exists in the dataset.
A Select models a query to be used when retrieving data from a DataSource.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
bool isPropertyNameValid(const std::string &propertyName)
It checks if the given property name is valid.
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.
const std::string & getName() const
It returns the dataset name.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
std::string GetParentDataSetName(const std::string &subDataSetName)
It returns the parent dataset name from a Sub DataSet name.
Select & from(const FromItem &item)
const std::string & getName() const
It returns the property name.
std::vector< std::string > getDataSetNames()
It It gets the dataset names available in the data source.
void add(Constraint *c)
It adds a new constraint.
bool hasDataSets()
It checks if the data source has any dataset.
bool isDataSetNameValid(const std::string &datasetName)
It returns true if the given string is a valid dataset name.
SpatialRelation
Spatial relations between geometric objects.
std::auto_ptr< te::da::DataSetType > getDataSetType(const std::string &name)
It gets information about the given dataset.
GDAL data set use counter.
std::auto_ptr< te::da::DataSetType > getType(const std::string &dsfullname)
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left 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.
AccessPolicy
Supported data access policies (can be used as bitfield).
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...
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.
A rectified grid is the spatial support for raster data.
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.
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
std::vector< std::string > getPropertyNames(const std::string &datasetName)
It gets the property names of the given dataset.
A GDAL data set gives access to a raster file.
boost::ptr_vector< te::dt::Property > getProperties(const std::string &datasetName)
It retrieves the properties of the dataset.
te::rst::Grid * getGrid()
Returns the definition of the raster grid support.
std::size_t getNumberOfDataSets()
It retrieves the number of data sets available in the data source.
te::rst::Grid * GetGrid(GDALDataset *gds)
Gets the grid definition from a GDAL dataset.
An exception class for the GDAL module.
void GetBandProperties(GDALDataset *gds, std::vector< te::rst::BandProperty * > &bprops)
Gets the list of bands definition from a GDAL dataset.
void set(te::rst::Grid *grid)
Sets the definition of the raster grid support.
TraverseType
A dataset can be traversed in two ways:
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.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
void renameDataSet(const std::string &name, const std::string &newName)
It renames a dataset.
void add(te::rst::BandProperty *b)
It adds a new band information to the property.
A class that models the description of a dataset.
Transactor(const std::string &accessInfo)
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
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.
std::size_t getNumberOfProperties(const std::string &datasetName)
It gets the number of properties of the given dataset.
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
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.
std::string GetDriverName(const std::string &dsName)
It returns the GDAL driver name associated to a data source name.
void setTitle(const std::string &title)
It sets a human descriptive title for the DataSetType.
An Envelope defines a 2D rectangular region.
void dropDataSet(const std::string &name)
It removes the dataset schema from the data source.
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.
The implementation of a DataSource that consists of datasets that can be decoded by the GDAL Library...
#define TR_GDAL(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
bool dataSetExists(const std::string &name)
It checks if a dataset with the given name exists in the data source.