src/terralib/wfs/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 
20 /*!
21  \file terralib/wfs/DataSource.cpp
22 
23  \brief Implementation of the data source for the WFS driver.
24 */
25 
26 // TerraLib
27 #include "../dataaccess/datasource/DataSourceTransactor.h"
28 #include "../dataaccess/query/SQLDialect.h"
29 #include "../common/StringUtils.h"
30 #include "../core/translator/Translator.h"
31 #include "DataSource.h"
32 #include "Exception.h"
33 #include "Transactor.h"
34 #include "Utils.h"
35 
36 // OGR
37 #include <ogrsf_frmts.h>
38 #include <ogr_core.h>
39 
41 
43 
44 te::wfs::DataSource::DataSource(const std::string& connInfo)
45  : te::da::DataSource(connInfo),
46  m_ogrDS(nullptr),
47  m_isOpened(false)
48 {
49 }
50 
52  : te::da::DataSource(uri),
53  m_ogrDS(nullptr),
54  m_isOpened(false)
55 {
56 }
57 
59 {
60  close();
61 }
62 
63 std::string te::wfs::DataSource::getType() const
64 {
66 }
67 
68 std::unique_ptr<te::da::DataSourceTransactor> te::wfs::DataSource::getTransactor()
69 {
70  if(!m_isOpened)
71  throw Exception(TE_TR("The data source is not opened!"));
72 
73  return std::unique_ptr<te::da::DataSourceTransactor>(new Transactor(this));
74 }
75 
77 {
78  if(m_isOpened)
79  return;
80 
82 
83  m_ogrDS = (GDALDataset*)GDALOpenEx(m_uri.uri().c_str(), GDAL_OF_READONLY, nullptr, nullptr, nullptr);
84 
85  if(m_ogrDS == nullptr)
86  throw Exception(TE_TR("Could not open the WFS data source!"));
87 
88  m_isOpened = true;
89 }
90 
92 {
93 // if (m_ogrDS)
94  //OGRDataSource::DestroyDataSource(m_ogrDS);
95 // GDALClose(m_ogrDS);
96 
97  m_ogrDS = nullptr;
98 
99  m_isOpened = false;
100 }
101 
103 {
104  return m_isOpened;
105 }
106 
108 {
109  if (m_isOpened)
110  return true;
111 
113 
114  GDALDataset* gds = static_cast<GDALDataset*>(GDALOpenEx(m_uri.uri().c_str(), GDAL_OF_READONLY, nullptr, nullptr, nullptr));
115  if (gds == nullptr)
116  return false;
117 
118  GDALClose(gds);
119 
120  return true;
121 }
122 
124 {
125  return sm_capabilities;
126 }
127 
129 {
131 }
132 
134 {
135  return sm_dialect;
136 }
137 
139 {
140  delete sm_dialect;
142 }
143 
144 //OGRDataSource* te::wfs::DataSource::getOGRDataSource()
146 {
147  return m_ogrDS;
148 }
149 
150 const std::vector<te::wfs::WFSLayerInfo>& te::wfs::DataSource::getLayersInfo()
151 {
152  if(!m_layersInfo.empty())
153  return m_layersInfo;
154 
155  buildLayersInfo();
156 
157  return m_layersInfo;
158 }
159 
160 void te::wfs::DataSource::create(const std::string& /*connInfo*/)
161 {
162  throw Exception(TE_TR("The create() method is not supported by the WFS driver!"));
163 }
164 
165 void te::wfs::DataSource::drop(const std::string& /*connInfo*/)
166 {
167  throw Exception(TE_TR("The drop() method is not supported by the WFS driver!"));
168 }
169 
170 bool te::wfs::DataSource::exists(const std::string& connInfo)
171 {
172  if (connInfo.empty())
173  return false;
174 
175  const te::core::URI aux(connInfo);
176  if (!aux.isValid())
177  return false;
178 
179  std::string path = aux.path();
180  if (path.empty())
181  return false;
182 
183  GDALDataset* gds = static_cast<GDALDataset*>(GDALOpen(path.c_str(), GA_ReadOnly));
184  if (gds == nullptr)
185  return false;
186 
187  GDALClose(gds);
188 
189  return true;
190 }
191 
192 std::vector<std::string> te::wfs::DataSource::getDataSourceNames(const std::string& /*connInfo*/)
193 {
194  return std::vector<std::string>();
195 }
196 
198 {
199  if (!m_uri.isValid())
200  throw Exception(TE_TR("The connection information is invalid!"));
201 
202  if (m_uri.path().empty())
203  throw Exception(TE_TR("The connection information is invalid. Missing the path parameter!"));
204 }
205 
207 {
208  open();
209 
210  OGRLayer* wfsMetadata = m_ogrDS->GetLayerByName("WFSLayerMetadata");
211 
212  if(wfsMetadata == nullptr)
213  throw Exception(TE_TR("Could not retrieve the metadata from WFS server!"));
214 
215  OGRFeature* f;
216  wfsMetadata->ResetReading();
217  while((f = wfsMetadata->GetNextFeature()) != nullptr)
218  {
219  WFSLayerInfo info;
220  info.m_name = f->GetFieldAsString("layer_name");
221  info.m_title = f->GetFieldAsString("title");
222  info.m_abstract = f->GetFieldAsString("abstract");
223 
224  m_layersInfo.push_back(info);
225  }
226 }
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Implementation of the transactor for the WFS driver.
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
std::vector< std::string > getDataSourceNames(const std::string &connInfo)
It gets the data source names available in a driver.
std::string m_abstract
Definition: WFSLayerInfo.h:48
Implementation of the data source for the WFS driver.
static te::da::DataSourceCapabilities sm_capabilities
Base exception class for plugin module.
An exception class for the TerraLib WFS module.
#define TE_WFS_DRIVER_IDENTIFIER
The WFS driver identifier string.
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
bool isValid() const
Return if the given URI is valid or not.
Definition: URI.cpp:133
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
DataSource(const std::string &connInfo)
void drop(const std::string &connInfo)
It removes the data source with the connection information from a driver.
Implementation of the data source for the WFS driver.
Informations about WFS Layers.
Definition: WFSLayerInfo.h:44
static void setCapabilities(const te::da::DataSourceCapabilities &capabilities)
void create(const std::string &connInfo)
It creates a new data source.
static void setDialect(te::da::SQLDialect *dialect)
std::vector< WFSLayerInfo > m_layersInfo
te::da::DataSourceCapabilities capabilities
URI C++ Library.
Definition: Attributes.h:37
void close()
It closes the data source and clears all the resources used by its internal communication channel...
const std::string & uri() const
Retrieving the full URI.
Definition: URI.cpp:88
~DataSource()
Virtual destructor.
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
te::core::URI m_uri
The URI used to describe the datasource connection;.
const std::vector< WFSLayerInfo > & getLayersInfo()
std::unique_ptr< te::da::DataSourceTransactor > getTransactor()
It returns the set of parameters used to set up the access channel to the underlying repository...
bool isValid() const
It checks if the data source is valid (available for using).
static te::da::SQLDialect * sm_dialect
Implementation of the transactor for the WFS driver.
void open()
It opens the data source and makes it ready for using.
Utility functions for WFS driver.
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
bool exists(const std::string &connInfo)
Check the existence of a data source in a driver.
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.