src/terralib/ws/ogc/wms/dataaccess/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/ws/ogc/wms/dataaccess/DataSource.cpp
22 
23  \brief Data Source for WS OGC WMS
24 
25  \author Emerson Moraes
26 */
27 
28 #include "DataSource.h"
29 
30 #include "../../../../core/translator/Translator.h"
31 #include "../../../../core/uri/URI.h"
32 #include "../../../../core/uri/Utils.h"
33 #include "../../../../dataaccess/datasource/DataSourceTransactor.h"
34 #include "../../../core/Exception.h"
35 #include "../../../core/CurlWrapper.h"
36 #include "Transactor.h"
37 
38 
40 
41 
43  : te::da::DataSource(connInfo),
44  m_isOpened(false)
45 {
46 }
47 
49  : te::da::DataSource(uri),
50  m_isOpened(false)
51 {
52 }
53 
55 
57 {
59 }
60 
61 std::unique_ptr<te::da::DataSourceTransactor> te::ws::ogc::wms::da::DataSource::getTransactor()
62 {
63  if(!m_isOpened)
64  {
65  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The data source is not opened!"));
66  }
67 
68  return std::unique_ptr<te::da::DataSourceTransactor>(new Transactor(m_wms));
69 }
70 
72 {
73  if(m_isOpened)
74  return;
75 
77 
78  try
79  {
80  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
81 
82  m_wms = std::shared_ptr<te::ws::ogc::WMSClient>(new te::ws::ogc::WMSClient(kvp["USERDATADIR"], kvp["URI"], kvp["VERSION"]));
83 
84  m_wms->updateCapabilities();
85  }
87  {
88  throw;
89  }
90  catch(...)
91  {
92  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Unknow error!"));
93  }
94 
95  m_isOpened = true;
96 }
97 
99 {
100  m_isOpened = false;
101 }
102 
104 {
105  return m_isOpened;
106 }
107 
109 {
110  if(m_isOpened)
111  return true;
112 
113  try
114  {
116 
117  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
118 
119  te::ws::ogc::WMSClient wms(kvp["USERDATADIR"], kvp["URI"], kvp["VERSION"]);
120 
121  wms.updateCapabilities();
122  }
123  catch(...)
124  {
125  return false;
126  }
127 
128  return true;
129 }
130 
132 {
133  return sm_capabilities;
134 }
135 
137 {
139 }
140 
142 {
143  return nullptr;
144 }
145 
146 
148 {
149  if(!isOpened())
150  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The WMS DataSource is not opened."));
151 
152  return m_wms->getMap(request);
153 }
154 
156 {
157  if(!isOpened())
158  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The WMS DataSource is not opened."));
159 
160  return m_wms->saveGetMap(request, filename);
161 }
162 
164 {
165  if(!isOpened())
166  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The WMS DataSource is not opened."));
167 
168  return m_wms->getFeatureInfo(request);
169 }
170 
171 std::shared_ptr<te::ws::ogc::WMSClient> te::ws::ogc::wms::da::DataSource::getWMSClient()
172 {
173  return m_wms;
174 }
175 
176 void te::ws::ogc::wms::da::DataSource::create(const std::string& /*connInfo*/)
177 {
178  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The create() method is not supported by the WMS driver!"));
179 }
180 
181 void te::ws::ogc::wms::da::DataSource::drop(const std::string& /*connInfo*/)
182 {
183  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The drop() method is not supported by the WMS driver!"));
184 }
185 
186 bool te::ws::ogc::wms::da::DataSource::exists(const std::string& connInfo)
187 {
188  if (connInfo.empty())
189  return false;
190 
191  const te::core::URI aux(connInfo);
192  if (!aux.isValid())
193  return false;
194 
195  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
196  std::map<std::string, std::string>::const_iterator it = kvp.begin();
197  std::map<std::string, std::string>::const_iterator itend = kvp.end();
198  std::string usrDataDir, version, uri;
199 
200  it = kvp.find("USERDATADIR");
201  if (it == itend || it->second.empty())
202  return false;
203  else
204  usrDataDir = it->second;
205 
206  it = kvp.find("VERSION");
207  if (it == itend || it->second.empty())
208  return false;
209  else
210  version = it->second;
211 
212  it = kvp.find("URI");
213  if (it == itend || it->second.empty())
214  return false;
215  else
216  uri = it->second;
217 
218 
219 
220  try
221  {
223 
224  te::ws::ogc::WMSClient wms(usrDataDir, uri, version);
225 
226  wms.updateCapabilities();
227  }
228  catch(...)
229  {
230  return false;
231  }
232 
233  return true;
234 }
235 
236 std::vector<std::string> te::ws::ogc::wms::da::DataSource::getDataSourceNames(const std::string& /*connInfo*/)
237 {
238  return std::vector<std::string>();
239 }
240 
241 std::vector<te::core::EncodingType> te::ws::ogc::wms::da::DataSource::getEncodings(const std::string& /*connInfo*/)
242 {
243  return std::vector<te::core::EncodingType>();
244 }
245 
247 {
248  if(!m_uri.isValid())
249  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The connection information is invalid!"));
250 
251  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
252  std::map<std::string, std::string>::const_iterator it = kvp.begin();
253  std::map<std::string, std::string>::const_iterator itend = kvp.end();
254 
255  it = kvp.find("URI");
256  if (it == itend || it->second.empty())
257  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The connection information is invalid. Missing URI parameter!"));
258 
259  it = kvp.find("VERSION");
260  if (it == itend || it->second.empty())
261  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The connection information is invalid. Missing VERSION parameter!"));
262 
263  it = kvp.find("USERDATADIR");
264  if (it == itend || it->second.empty())
265  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The connection information is invalid. Missing USERDATADIR parameter!"));
266 }
void close()
It closes the data source and clears all the resources used by its internal communication channel...
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
const std::string saveGetMap(const te::ws::ogc::wms::WMSGetMapRequest &request, const std::string &filename)
It execute a WMS GetMap and save the result image on disk. The request will be based on WMSGetMapRequ...
std::shared_ptr< te::ws::ogc::WMSClient > getWMSClient()
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
void open()
It opens the data source and makes it ready for using.
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
void drop(const std::string &connInfo)
It removes the data source with the connection information from a driver.
std::vector< te::core::EncodingType > getEncodings(const std::string &connInfo)
Data Source for WS OGC WMS.
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
static void setCapabilities(const te::da::DataSourceCapabilities &capabilities)
te::da::DataSourceCapabilities capabilities
URI C++ Library.
Definition: Attributes.h:37
void create(const std::string &connInfo)
It creates a new data source.
std::unique_ptr< te::da::DataSourceTransactor > getTransactor()
It returns the set of parameters used to set up the access channel to the underlying repository...
The WMSGetFeatureInfoRequest WMS 1.3.0 struct.
The WMSGetMapResponse WMS 1.3.0 struct.
The WMSGetMapResponse WMS 1.3.0 struct.
Base exception class for WS Core Runtime Library.
Implementation of the data source for the WMS driver.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
Implementation of the transactor for the WS OGC WMS.
te::ws::ogc::wms::WMSGetMapResponse getMap(const te::ws::ogc::wms::WMSGetMapRequest &request)
te::core::URI m_uri
The URI used to describe the datasource connection;.
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
#define TE_OGC_WMS_DRIVER_IDENTIFIER
The OGC WMS driver identifier string.
std::vector< std::string > getDataSourceNames(const std::string &connInfo)
It gets the data source names available in a driver.
void updateCapabilities()
Method to get the capabilities from a WMS server and store in m_capabilities member.
Definition: WMSClient.cpp:67
A class to retrieve information and data from a Web Map Service.
Definition: WMSClient.h:57
bool isValid() const
It checks if the data source is valid (available for using).
Implementation of the transactor for the WMS driver.
bool exists(const std::string &connInfo)
Check the existence of a data source in a driver.
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
~DataSource()
Virtual destructor.
const te::ws::ogc::wms::WMSGetFeatureInfoResponse getFeatureInfo(const te::ws::ogc::wms::WMSGetFeatureInfoRequest &request)
The WMSGetMapRequest WMS 1.3.0 struct.