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 
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 "../common/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 
45  : te::da::DataSource(),
46  m_ogrDS(0),
47  m_isOpened(false)
48 {
49 }
50 
52 {
53  close();
54 }
55 
56 std::string te::wfs::DataSource::getType() const
57 {
59 }
60 
61 const std::map<std::string, std::string>& te::wfs::DataSource::getConnectionInfo() const
62 {
63  return m_connectionInfo;
64 }
65 
66 void te::wfs::DataSource::setConnectionInfo(const std::map<std::string, std::string>& connInfo)
67 {
68  m_connectionInfo = connInfo;
69 }
70 
71 std::auto_ptr<te::da::DataSourceTransactor> te::wfs::DataSource::getTransactor()
72 {
73  if(!m_isOpened)
74  throw Exception(TE_TR("The data source is not opened!"));
75 
76  return std::auto_ptr<te::da::DataSourceTransactor>(new Transactor(this));
77 }
78 
80 {
81  if(m_isOpened)
82  return;
83 
84  verifyConnectionInfo();
85 
86  m_ogrDS = OGRSFDriverRegistrar::Open(m_connectionInfo.find("URI")->second.c_str());
87 
88  if(m_ogrDS == 0)
89  throw Exception(TE_TR("Could not open the WFS data source!"));
90 
91  m_isOpened = true;
92 }
93 
95 {
96  if(m_ogrDS)
97  OGRDataSource::DestroyDataSource(m_ogrDS);
98 
99  m_ogrDS = 0;
100 
101  m_isOpened = false;
102 }
103 
105 {
106  return m_isOpened;
107 }
108 
110 {
111  if(m_connectionInfo.empty())
112  return false;
113 
114  std::map<std::string, std::string>::const_iterator it = m_connectionInfo.find("URI");
115  if(it == m_connectionInfo.end())
116  return false;
117 
118  OGRDataSource* ds = OGRSFDriverRegistrar::Open(it->second.c_str());
119  if(ds == 0)
120  return false;
121 
122  OGRDataSource::DestroyDataSource(ds);
123 
124  return true;
125 }
126 
128 {
129  return sm_capabilities;
130 }
131 
133 {
134  sm_capabilities = capabilities;
135 }
136 
138 {
139  return sm_dialect;
140 }
141 
143 {
144  delete sm_dialect;
145  sm_dialect = dialect;
146 }
147 
149 {
150  return m_ogrDS;
151 }
152 
153 const std::vector<te::wfs::WFSLayerInfo>& te::wfs::DataSource::getLayersInfo()
154 {
155  if(!m_layersInfo.empty())
156  return m_layersInfo;
157 
158  buildLayersInfo();
159 
160  return m_layersInfo;
161 }
162 
163 void te::wfs::DataSource::create(const std::map<std::string, std::string>& /*dsInfo*/)
164 {
165  throw Exception(TE_TR("The create() method is not supported by the WFS driver!"));
166 }
167 
168 void te::wfs::DataSource::drop(const std::map<std::string, std::string>& /*dsInfo*/)
169 {
170  throw Exception(TE_TR("The drop() method is not supported by the WFS driver!"));
171 }
172 
173 bool te::wfs::DataSource::exists(const std::map<std::string, std::string>& dsInfo)
174 {
175  if(dsInfo.empty())
176  return false;
177 
178  std::map<std::string, std::string>::const_iterator it = dsInfo.find("URI");
179  if(it == dsInfo.end())
180  return false;
181 
182  OGRDataSource* ds = OGRSFDriverRegistrar::Open(it->second.c_str());
183  if(ds == 0)
184  return false;
185 
186  OGRDataSource::DestroyDataSource(ds);
187 
188  return true;
189 }
190 
191 std::vector<std::string> te::wfs::DataSource::getDataSourceNames(const std::map<std::string, std::string>& /*dsInfo*/)
192 {
193  return std::vector<std::string>();
194 }
195 
196 std::vector<te::common::CharEncoding> te::wfs::DataSource::getEncodings(const std::map<std::string, std::string>& /*dsInfo*/)
197 {
198  return std::vector<te::common::CharEncoding>();
199 }
200 
202 {
203  if(m_connectionInfo.empty())
204  throw Exception(TE_TR("The connection information is empty!"));
205 
206  std::map<std::string, std::string>::const_iterator it = m_connectionInfo.find("URI");
207  if(it == m_connectionInfo.end())
208  throw Exception(TE_TR("The connection information is invalid. Missing URI parameter!"));
209 }
210 
212 {
213  open();
214 
215  OGRLayer* wfsMetadata = m_ogrDS->GetLayerByName("WFSLayerMetadata");
216 
217  if(wfsMetadata == 0)
218  throw Exception(TE_TR("Could not retrieve the metadata from WFS server!"));
219 
220  OGRFeature* f;
221  wfsMetadata->ResetReading();
222  while((f = wfsMetadata->GetNextFeature()) != NULL)
223  {
224  WFSLayerInfo info;
225  info.m_name = f->GetFieldAsString("layer_name");
226  info.m_title = f->GetFieldAsString("title");
227  info.m_abstract = f->GetFieldAsString("abstract");
228 
229  m_layersInfo.push_back(info);
230  }
231 }
Implementation of the data source for the WFS driver.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
Definition: DataSource.cpp:137
Implementation of the transactor for the WFS driver.
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:66
Implementation of the transactor for the WFS driver.
Definition: Transactor.h:50
te::da::SQLDialect * dialect
Definition: WFSDialect.h:1
std::string m_abstract
Definition: WFSLayerInfo.h:48
static te::da::DataSourceCapabilities sm_capabilities
Definition: DataSource.h:112
#define TE_WFS_DRIVER_IDENTIFIER
The WFS driver identifier string.
Definition: Config.h:39
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...
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:168
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
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:196
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:191
Implementation of the data source for the WFS driver.
Definition: DataSource.h:52
Informations about WFS Layers.
Definition: WFSLayerInfo.h:44
static void setCapabilities(const te::da::DataSourceCapabilities &capabilities)
Definition: DataSource.cpp:132
static void setDialect(te::da::SQLDialect *dialect)
Definition: DataSource.cpp:142
te::da::DataSourceCapabilities capabilities
void verifyConnectionInfo() const
Definition: DataSource.cpp:201
void close()
It closes the data source and clears all the resources used by its internal communication channel...
Definition: DataSource.cpp:94
~DataSource()
Virtual destructor.
Definition: DataSource.cpp:51
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
Definition: DataSource.cpp:104
void create(const std::map< std::string, std::string > &dsInfo)
It creates a new data source.
Definition: DataSource.cpp:163
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:71
const std::vector< WFSLayerInfo > & getLayersInfo()
Definition: DataSource.cpp:153
Utility functions for WFS driver.
bool isValid() const
It checks if the data source is valid (available for using).
Definition: DataSource.cpp:109
bool exists(const std::map< std::string, std::string > &dsInfo)
Check the existence of a data source in a driver.
Definition: DataSource.cpp:173
static te::da::SQLDialect * sm_dialect
Definition: DataSource.h:113
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:61
void open()
It opens the data source and makes it ready for using.
Definition: DataSource.cpp:79
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Definition: DataSource.cpp:127
OGRDataSource * getOGRDataSource()
Definition: DataSource.cpp:148
An exception class for the TerraLib WFS module.
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
Definition: DataSource.cpp:56