All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSource.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 #include "DataSource.h"
20 #include "Globals.h"
21 #include "Transactor.h"
22 #include "Utils.h"
23 
24 #include "../common/Translator.h"
25 #include "../dataaccess/query/SQLDialect.h"
26 
27 // OGR
28 #include <ogrsf_frmts.h>
29 #include <ogr_core.h>
30 
31 // Boost
32 #include <boost/filesystem/operations.hpp>
33 
35 
37 {
39 
40  dt_caps.setSupportInt32(true);
41  dt_caps.setSupportArray(true);
42  dt_caps.setSupportString(true);
43  dt_caps.setSupportDouble(true);
44  dt_caps.setSupportNumeric(true);
45  dt_caps.setSupportByteArray(true);
46  dt_caps.setSupportDateTime(true);
47  dt_caps.setSupportGeometry(true);
48 
49  caps.setDataTypeCapabilities(dt_caps);
50 }
51 
53 {
55 
56  // Spatial Topologic Operators
57  qy_caps.addSpatialTopologicOperator("st_envelopeintersects");
58 
59  // Comparsion Operators
60  qy_caps.addComparsionOperator("=");
61  qy_caps.addComparsionOperator("<>");
62  qy_caps.addComparsionOperator("<");
63  qy_caps.addComparsionOperator(">");
64  qy_caps.addComparsionOperator("<=");
65  qy_caps.addComparsionOperator(">=");
66 
67  qy_caps.addLogicalOperator("and");
68  qy_caps.addLogicalOperator("or");
69 
70  caps.setQueryCapabilities(qy_caps);
71 }
72 
73 void GetCapabilities(OGRDataSource* ds, te::da::DataSourceCapabilities& caps)
74 {
75  // DataSet
76  if(!ds || ds->GetLayerCount() <= 0)
77  return;
78 
80 
81  OGRLayer* l = ds->GetLayer(0);
82 
83  ds_caps.setSupportEfficientMove((l->TestCapability(OLCFastSetNextByIndex)) ? true : false);
84  ds_caps.setSupportRandomTraversing((l->TestCapability(OLCRandomRead)) ? true : false);
85  ds_caps.setSupportBidirectionalTraversing((l->TestCapability(OLCRandomRead)) ? true : false);
86  ds_caps.setSupportEfficientDataSetSize((l->TestCapability(OLCFastGetExtent)) ? true : false);
87 
88  caps.setDataSetCapabilities(ds_caps);
89 }
90 
91 
93  te::da::DataSource(),
94  m_ogrDS(0),
95  m_isValid(false)
96 {
97 }
98 
100 {
101  close();
102 }
103 
104 std::string te::ogr::DataSource::getType() const
105 {
107 }
108 
109 const std::map<std::string, std::string>& te::ogr::DataSource::getConnectionInfo() const
110 {
111  return m_connectionInfo;
112 }
113 
114 void te::ogr::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
115 {
116  m_connectionInfo = connInfo;
117 }
118 
119 std::auto_ptr<te::da::DataSourceTransactor> te::ogr::DataSource::getTransactor()
120 {
121  return std::auto_ptr<te::da::DataSourceTransactor>(new Transactor(this));
122 }
123 
125 {
126  close();
127 
128  if(m_connectionInfo.empty())
129  throw Exception(TE_TR("There is no information about the data source"));
130 
131  std::string path;
132  std::map<std::string, std::string>::const_iterator it;
133 
134  it = m_connectionInfo.find("URI");
135  if (it==m_connectionInfo.end())
136  throw(Exception(TE_TR("Not enough information to open the data source.")));
137 
138  path = it->second;
139 
140  if (boost::filesystem::exists(path))
141  m_ogrDS = OGRSFDriverRegistrar::Open(path.c_str(), 1);
142 
143  // let's try to open it without update permission
144  if (!m_ogrDS)
145  {
146  m_ogrDS = OGRSFDriverRegistrar::Open(path.c_str(), 0);
147  if (m_ogrDS)
148  m_capabilities.setAccessPolicy(te::common::RAccess);
149  }
150  else
151  m_capabilities.setAccessPolicy(te::common::RWAccess);
152 
153  m_isValid = true;
154 
155  GetDataSetTypeCapabilities(m_capabilities);
156  GetQueryCapabilities(m_capabilities);
157 
158  if (m_ogrDS)
159  GetCapabilities(m_ogrDS, m_capabilities);
160 }
161 
163 {
164  if (m_ogrDS)
165  OGRDataSource::DestroyDataSource(m_ogrDS);
166 
167  m_ogrDS = 0;
168 
169  m_isValid = false;
170 }
171 
173 {
174  return m_ogrDS != 0;
175 }
176 
178 {
179  return m_isValid;
180 }
181 
183 {
184  return m_capabilities;
185 }
186 
188 {
189  return sm_myDialect;
190 }
191 
193 {
194  if(sm_myDialect != 0)
195  delete sm_myDialect;
196 
197  sm_myDialect = dialect;
198 }
199 
201 {
202  return m_ogrDS;
203 }
204 
205 void te::ogr::DataSource::create(const std::map<std::string, std::string>& dsInfo)
206 {
207  setConnectionInfo(dsInfo);
208 
209  close();
210 }
211 
212 void te::ogr::DataSource::createDataSet(te::da::DataSetType* dt, const std::map<std::string, std::string>& options)
213 {
214  if (!m_ogrDS)
215  {
216  std::string path;
217  std::map<std::string, std::string>::const_iterator it;
218 
219  it = m_connectionInfo.find("URI");
220  if (it==m_connectionInfo.end())
221  throw(Exception(TE_TR("Not enough information to create data set.")));
222  path = it->second;
223 
224  boost::filesystem::path bpath(path);
225  std::string dir = bpath.parent_path().string();
226  if (!boost::filesystem::exists(dir))
227  boost::filesystem::create_directory(dir);
228 
229  OGRSFDriverRegistrar* driverManager = OGRSFDriverRegistrar::GetRegistrar();
230  OGRSFDriver* driver;
231 
232  it = m_connectionInfo.find("DRIVER");
233  if (it!=m_connectionInfo.end())
234  driver = driverManager->GetDriverByName(it->second.c_str());
235  else
236  driver = driverManager->GetDriverByName(GetDriverName(path).c_str());
237 
238  if (driver == 0)
239  throw(Exception(TE_TR("Driver not found.")));
240 
241  if(!driver->TestCapability(ODrCCreateDataSource))
242  throw(Exception(TE_TR("The Driver does not have create capability.")));
243 
244  char** papszOptions = 0;
245  it = m_connectionInfo.begin();
246  while(it != m_connectionInfo.end())
247  {
248  if(it->first == "URI" || it->first == "SOURCE" || it->first == "DRIVER")
249  {
250  ++it;
251  continue;
252  }
253  papszOptions = CSLSetNameValue(papszOptions, it->first.c_str(), it->second.c_str());
254  ++it;
255  }
256 
257  m_ogrDS = driver->CreateDataSource(path.c_str(),papszOptions);
258 
259  if(papszOptions)
260  CSLDestroy(papszOptions);
261  }
262 
263  if (!m_ogrDS)
264  throw(Exception(TE_TR("Error creating the dataset.")));
265 
266  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
267  return t->createDataSet(dt, options);
268 }
269 
270 void te::ogr::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
271 {
272  std::string path = dsInfo.begin()->second;
273 
274  if(m_ogrDS!=0 && path.compare(m_ogrDS->GetName()) == 0)
275  close();
276 
277  OGRSFDriverRegistrar* driverManager = OGRSFDriverRegistrar::GetRegistrar();
278  OGRSFDriver* driver = driverManager->GetDriverByName(GetDriverName(path).c_str());
279 
280  if (driver == 0)
281  throw(Exception(TE_TR("Driver not found.")));
282 
283  if(!driver->TestCapability(ODrCDeleteDataSource))
284  throw(Exception(TE_TR("The Driver does not have drop capability.")));
285 
286  if(driver->DeleteDataSource(path.c_str()) != OGRERR_NONE)
287  throw(Exception(TE_TR("Error when dropping the data source.")));
288 }
289 
290 bool te::ogr::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
291 {
292  return boost::filesystem::exists(dsInfo.begin()->second);
293 }
294 
295 std::vector<std::string> te::ogr::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
296 {
297  std::vector<std::string> names;
298 
299  names.push_back(dsInfo.begin()->second);
300 
301  return names;
302 }
303 
304 std::vector<te::common::CharEncoding> te::ogr::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
305 {
306  return std::vector<te::common::CharEncoding>();
307 }
308 
310 {
311  return new DataSource;
312 }
void drop(const std::map< std::string, std::string > &dsInfo)
It removes the data source with the connection information from a driver.
Definition: DataSource.cpp:270
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Definition: DataSource.cpp:172
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:104
void setSupportString(const bool &support)
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
void GetQueryCapabilities(te::da::DataSourceCapabilities &caps)
Definition: DataSource.cpp:52
A class for data providers of OGR.
void GetDataSetTypeCapabilities(te::da::DataSourceCapabilities &caps)
Definition: DataSource.cpp:36
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:290
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:124
A class that models the description of a dataset.
Definition: DataSetType.h:72
void addSpatialTopologicOperator(const std::string &op)
void setSupportGeometry(const bool &support)
void setSupportNumeric(const bool &support)
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:205
void addComparsionOperator(const std::string &op)
void setSupportEfficientMove(const bool &support)
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
A class that informs what the dataset implementation of a given data source can perform.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void setSupportDouble(const bool &support)
void setQueryCapabilities(const QueryCapabilities &capabilities)
void setSupportByteArray(const bool &support)
std::string GetDriverName(const std::string &path)
It tries extract the driver name used by OGR Library based on the given path.
Definition: Utils.cpp:574
void addLogicalOperator(const std::string &op)
std::vector< std::string > getDataSourceNames(const std::map< std::string, std::string > &dsInfo)
It gets the data source names available in a driver.
Definition: DataSource.cpp:295
A class that represents the supported data types of a specific data source.
static te::da::SQLDialect * sm_myDialect
OGR SQL dialect.
Definition: DataSource.h:104
A class that informs the query support of a given data source.
OGRDataSource * getOGRDataSource()
Definition: DataSource.cpp:200
void GetCapabilities(OGRDataSource *ds, te::da::DataSourceCapabilities &caps)
Definition: DataSource.cpp:73
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.
Definition: DataSource.cpp:212
void setDataSetCapabilities(const DataSetCapabilities &capabilities)
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:182
bool isValid() const
It checks if the data source is valid (available for using).
Definition: DataSource.cpp:177
The OGR data source provider.
Definition: DataSource.h:48
Utility functions for OGR support.
void setSupportInt32(const bool &support)
void setSupportBidirectionalTraversing(const bool &support)
std::vector< te::common::CharEncoding > getEncodings(const std::map< std::string, std::string > &dsInfo)
It gets the encodings for the data source.
Definition: DataSource.cpp:304
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:99
void setSupportRandomTraversing(const bool &support)
void setDataTypeCapabilities(const DataTypeCapabilities &capabilities)
DataSource * Build()
Definition: DataSource.cpp:309
void setSupportDateTime(const bool &support)
void close()
It closes the data source and clears all the resources used by its internal communication channel...
Definition: DataSource.cpp:162
const std::map< std::string, std::string > & getConnectionInfo() const
It returns the set of parameters used to set up the access channel to the underlying repository...
Definition: DataSource.cpp:109
An static class with global definitions.
DataSource()
Default constructor that can be called by subclasses.
Definition: DataSource.cpp:92
void setSupportArray(const bool &support)
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Definition: DataSource.cpp:187
static void setDialect(te::da::SQLDialect *dialect)
Definition: DataSource.cpp:192
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
It sets the connection information to be used when connecting to the data source. ...
Definition: DataSource.cpp:114
static const std::string m_driverIdentifier
The OGR driver identifier.
Definition: Globals.h:49
void setSupportEfficientDataSetSize(const bool &support)
std::auto_ptr< te::da::DataSourceTransactor > getTransactor()
It returns an object that can execute transactions in the context of a data source.
Definition: DataSource.cpp:119