src/terralib/qt/plugins/mobile/geopackage/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 "../../../../core/filesystem/FileSystem.h"
25 #include "../../../../core/translator/Translator.h"
26 #include "../../../../dataaccess/query/SQLDialect.h"
27 
28 // OGR
29 #include <ogrsf_frmts.h>
30 #include <ogr_core.h>
31 #include <gdal_priv.h>
32 #include <ogr_api.h>
33 #include <gdal.h>
34 
35 // Boost
36 #include <boost/filesystem/operations.hpp>
37 
39 
41 {
43 
44  dt_caps.setSupportInt32(true);
45  dt_caps.setSupportArray(true);
46  dt_caps.setSupportString(true);
47  dt_caps.setSupportDouble(true);
48  dt_caps.setSupportNumeric(true);
49  dt_caps.setSupportByteArray(true);
50  dt_caps.setSupportDateTime(true);
51  dt_caps.setSupportGeometry(true);
52 
53  caps.setDataTypeCapabilities(dt_caps);
54 }
55 
57 {
59 
60  // Spatial Topologic Operators
61  qy_caps.addSpatialTopologicOperator("st_envelopeintersects");
62 
63  // Comparsion Operators
64  qy_caps.addComparsionOperator("=");
65  qy_caps.addComparsionOperator("<>");
66  qy_caps.addComparsionOperator("<");
67  qy_caps.addComparsionOperator(">");
68  qy_caps.addComparsionOperator("<=");
69  qy_caps.addComparsionOperator(">=");
70 
71  qy_caps.addLogicalOperator("and");
72  qy_caps.addLogicalOperator("or");
73 
74  caps.setQueryCapabilities(qy_caps);
75 }
76 
77 //void GetCapabilities(OGRDataSource* ds, te::da::DataSourceCapabilities& caps)
79 {
80  // DataSet
81  if(!ds || ds->GetLayerCount() <= 0)
82  return;
83 
85 
86  OGRLayer* l = ds->GetLayer(0);
87 
88  ds_caps.setSupportEfficientMove((l->TestCapability(OLCFastSetNextByIndex)) ? true : false);
89  ds_caps.setSupportRandomTraversing((l->TestCapability(OLCRandomRead)) ? true : false);
90  ds_caps.setSupportBidirectionalTraversing((l->TestCapability(OLCRandomRead)) ? true : false);
91  ds_caps.setSupportEfficientDataSetSize((l->TestCapability(OLCFastGetExtent)) ? true : false);
92 
93  caps.setDataSetCapabilities(ds_caps);
94 }
95 
96 
98  te::da::DataSource(),
99  m_ogrDS(0),
100  m_isValid(false)
101 {
102 }
103 
105 {
106  close();
107 }
108 
109 std::string te::gpkg::DataSource::getType() const
110 {
112 }
113 
114 const std::map<std::string, std::string>& te::gpkg::DataSource::getConnectionInfo() const
115 {
116  return m_connectionInfo;
117 }
118 
119 void te::gpkg::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
120 {
121  m_connectionInfo = connInfo;
122 }
123 
124 std::auto_ptr<te::da::DataSourceTransactor> te::gpkg::DataSource::getTransactor()
125 {
126  return std::auto_ptr<te::da::DataSourceTransactor>(new Transactor(this));
127 }
128 
130 {
131  close();
132 
133  if(m_connectionInfo.empty())
134  throw Exception(TE_TR("There is no information about the data source"));
135 
136  std::string path;
137  std::map<std::string, std::string>::const_iterator it;
138 
139  it = m_connectionInfo.find("URI");
140  if (it==m_connectionInfo.end())
141  throw(Exception(TE_TR("Not enough information to open the data source.")));
142 
143  path = it->second;
144 
146  //m_ogrDS = OGRSFDriverRegistrar::Open(path.c_str(), 1);
147  m_ogrDS = (GDALDataset*)GDALOpenEx(path.c_str(), GDAL_OF_UPDATE, NULL, NULL, NULL);
148 
149  // let's try to open it without update permission
150  if (!m_ogrDS)
151  {
152  //m_ogrDS = OGRSFDriverRegistrar::Open(path.c_str(), 0);
153  m_ogrDS = (GDALDataset*)GDALOpenEx(path.c_str(), GDAL_OF_READONLY, NULL, NULL, NULL);
154  if (m_ogrDS)
156  }
157  else
159 
160  m_isValid = true;
161 
164 
165  if (m_ogrDS)
167 }
168 
170 {
171  if (m_ogrDS)
172  //OGRDataSource::DestroyDataSource(m_ogrDS);
173  GDALClose(m_ogrDS);
174 
175  m_ogrDS = 0;
176 
177  m_isValid = false;
178 }
179 
181 {
182  return m_ogrDS != 0;
183 }
184 
186 {
187  return m_isValid;
188 }
189 
191 {
192  return m_capabilities;
193 }
194 
196 {
197  return sm_myDialect;
198 }
199 
201 {
202  if(sm_myDialect != 0)
203  delete sm_myDialect;
204 
206 }
207 
208 //OGRDataSource* te::gpkg::DataSource::getOGRDataSource()
210 {
211  return m_ogrDS;
212 }
213 
214 void te::gpkg::DataSource::create(const std::map<std::string, std::string>& dsInfo)
215 {
216  setConnectionInfo(dsInfo);
217 
218  close();
219 }
220 
221 void te::gpkg::DataSource::createDataSet(te::da::DataSetType* dt, const std::map<std::string, std::string>& options)
222 {
223  if (!m_ogrDS)
224  {
225  std::string path;
226  std::map<std::string, std::string>::const_iterator it;
227 
228  it = m_connectionInfo.find("URI");
229  if (it==m_connectionInfo.end())
230  throw(Exception(TE_TR("Not enough information to create data set.")));
231  path = it->second;
232 
233  boost::filesystem::path bpath(path);
234  std::string dir = bpath.parent_path().string();
237 
238  //OGRSFDriverRegistrar* driverManager = OGRSFDriverRegistrar::GetRegistrar();
239  //OGRSFDriver* driver;
240 
241  GDALDriverManager* driverManager = GetGDALDriverManager();
242  GDALDriver* driver;
243 
244  it = m_connectionInfo.find("DRIVER");
245  if (it!=m_connectionInfo.end())
246  driver = driverManager->GetDriverByName(it->second.c_str());
247  else
248  driver = driverManager->GetDriverByName(GetDriverName(path).c_str());
249 
250  if (driver == 0)
251  throw(Exception(TE_TR("Driver not found.")));
252 
253  //if(!driver->TestCapability(ODrCCreateDataSource))
254  // throw(Exception(TE_TR("The driver has no capability for creating a datasource!")));
255 
256  if (!OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
257  throw(Exception(TE_TR("The driver has no capability for creating a datasource!")));
258 
259  char** papszOptions = 0;
260  it = m_connectionInfo.begin();
261  while(it != m_connectionInfo.end())
262  {
263  if(it->first == "URI" || it->first == "SOURCE" || it->first == "DRIVER")
264  {
265  ++it;
266  continue;
267  }
268  papszOptions = CSLSetNameValue(papszOptions, it->first.c_str(), it->second.c_str());
269  ++it;
270  }
271 
272  //m_ogrDS = driver->CreateDataSource(path.c_str(), papszOptions);
273  m_ogrDS = driver->Create(path.c_str(), 0, 0, 0, GDT_Unknown, papszOptions);
274 
275  if(papszOptions)
276  CSLDestroy(papszOptions);
277  }
278 
279  if (!m_ogrDS)
280  throw(Exception(TE_TR("Error creating the dataset!")));
281 
282  std::auto_ptr<te::da::DataSourceTransactor> t = getTransactor();
283  return t->createDataSet(dt, options);
284 }
285 
286 void te::gpkg::DataSource::drop(const std::map<std::string, std::string>& dsInfo)
287 {
288  std::string path = dsInfo.begin()->second;
289 
290  //if (m_ogrDS != 0 && path.compare(m_ogrDS->GetName()) == 0)
291  if (m_ogrDS != 0 && path.compare(m_ogrDS->GetDescription()) == 0)
292  close();
293 
294  //OGRSFDriverRegistrar* driverManager = OGRSFDriverRegistrar::GetRegistrar();
295  //OGRSFDriver* driver = driverManager->GetDriverByName(GetDriverName(path).c_str());
296 
297  GDALDriverManager* driverManager = GetGDALDriverManager();
298  GDALDriver* driver = driverManager->GetDriverByName(GetDriverName(path).c_str());
299 
300  if (driver == 0)
301  throw(Exception(TE_TR("Driver not found!")));
302 
303 // if(!driver->TestCapability(ODrCDeleteDataSource))
304 // throw(Exception(TE_TR("The Driver does not have drop capability.")));
305 
306  if (!OGR_Dr_TestCapability(driver, ODrCDeleteDataSource))
307  throw(Exception(TE_TR("The driver has no drop capability!")));
308 
309 // if(driver->DeleteDataSource(path.c_str()) != OGRERR_NONE)
310 // throw(Exception(TE_TR("Error when dropping the data source.")));
311  GDALClose(driver);
312 }
313 
314 bool te::gpkg::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
315 {
316  return te::core::FileSystem::exists(dsInfo.begin()->second);
317 }
318 
319 std::vector<std::string> te::gpkg::DataSource::getDataSourceNames(const std::map<std::string, std::string>& dsInfo)
320 {
321  std::vector<std::string> names;
322 
323  names.push_back(dsInfo.begin()->second);
324 
325  return names;
326 }
327 
328 std::vector<te::core::EncodingType> te::gpkg::DataSource::getEncodings(const std::map<std::string, std::string>& dsInfo)
329 {
330  return std::vector<te::core::EncodingType>();
331 }
332 
334 {
335  return new DataSource;
336 }
void setSupportString(const bool &support)
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
GDALDataset * m_ogrDS
A pointer to OGR 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.
void GetCapabilities(GDALDataset *ds, te::da::DataSourceCapabilities &caps)
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Base exception class for plugin module.
A class that models the description of a dataset.
Definition: DataSetType.h:72
void addSpatialTopologicOperator(const std::string &op)
static te::da::SQLDialect * sm_myDialect
OGR SQL dialect.
void setSupportGeometry(const bool &support)
const std::map< std::string, std::string > & getConnectionInfo() const
static const std::string m_driverIdentifier
The OGR driver identifier.
void setSupportNumeric(const bool &support)
std::vector< te::core::EncodingType > getEncodings(const std::map< std::string, std::string > &dsInfo)
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
void addComparsionOperator(const std::string &op)
void setSupportEfficientMove(const bool &support)
DataSource()
Default constructor that can be called by subclasses.
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
void setAccessPolicy(const te::common::AccessPolicy &accessPolicy)
static te::dt::Date ds(2010, 01, 01)
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:242
void drop(const std::map< std::string, std::string > &dsInfo)
void setSupportDouble(const bool &support)
void setConnectionInfo(const std::map< std::string, std::string > &connInfo)
void setQueryCapabilities(const QueryCapabilities &capabilities)
void setSupportByteArray(const bool &support)
std::vector< std::string > getDataSourceNames(const std::map< std::string, std::string > &dsInfo)
void addLogicalOperator(const std::string &op)
bool exists(const std::map< std::string, std::string > &dsInfo)
A class that represents the supported data types of a specific data source.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
A class that informs the query support of a given data source.
void close()
It closes the data source and clears all the resources used by its internal communication channel...
This file contains include headers for the TerraLib GDAL driver.
URI C++ Library.
Definition: Attributes.h:37
static te::dt::TimeDuration dt(20, 30, 50, 11)
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
void open()
It opens the data source and makes it ready for using.
std::map< std::string, std::string > m_connectionInfo
Connection information.
void setDataSetCapabilities(const DataSetCapabilities &capabilities)
te::da::DataSourceCapabilities m_capabilities
OGR capabilities.
bool m_isValid
True if this is a valid datasource.
void setSupportInt32(const bool &support)
void setSupportBidirectionalTraversing(const bool &support)
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
static bool createDirectory(const std::string &path)
Creates a directory from a given path in UTF-8.
Definition: FileSystem.cpp:147
std::auto_ptr< te::da::DataSourceTransactor > getTransactor()
It returns the set of parameters used to set up the access channel to the underlying repository...
void setSupportRandomTraversing(const bool &support)
void setDataTypeCapabilities(const DataTypeCapabilities &capabilities)
bool isValid() const
It checks if the data source is valid (available for using).
void GetDataSetTypeCapabilities(te::da::DataSourceCapabilities &caps)
std::string GetDriverName(const std::string &path)
It tries extract the driver name used by OGR Library based on the given path.
void create(const std::map< std::string, std::string > &dsInfo)
void setSupportDateTime(const bool &support)
void GetQueryCapabilities(te::da::DataSourceCapabilities &caps)
void setSupportArray(const bool &support)
static void setDialect(te::da::SQLDialect *dialect)
void setSupportEfficientDataSetSize(const bool &support)