TsWMSDataAccess.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2017 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/unittest/ogc-wms/TsWMSDataAccess.cpp
22 
23  \brief A test suit for the WS OGC WMS DataAccess.
24 
25  \author Emerson Moraes
26  */
27 
28 //TerraLib
29 
30 
43 
44 
45 // Boost
46 #include <boost/test/unit_test.hpp>
47 
48 
49 
50 BOOST_AUTO_TEST_CASE(datasource_open_test)
51 {
53 
55 
56  // Directory where temporary WMS data will be stored.
57  std::string usrDataDir = te::qt::af::AppCtrlSingleton::getInstance().getUserDataDir().toUtf8().data();
58 
59  // WMS server URL.
60  std::string url = "http://demo.geo-solutions.it/geoserver/ows";
61 
62  // Version that will be used on WMS Requests.
63  std::string version = "1.3.0";
64 
65  std::string encodedURL = te::core::URIEncode(url);
66 
67  std::string connInfo("wms://?URI=" + encodedURL + "&VERSION=" + version + "&USERDATADIR=" + usrDataDir);
68 
69  // Perform connection
70  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS2", connInfo);
71  ds->open();
72 
73  BOOST_CHECK(ds->isOpened());
74 
75  return;
76 }
77 
78 BOOST_AUTO_TEST_CASE(datasource_open_exception_test)
79 {
81 
82  // Directory where temporary WMS data will be stored.
83  std::string usrDataDir = te::qt::af::AppCtrlSingleton::getInstance().getUserDataDir().toUtf8().data();
84 
85  // WMS server URL.
86  std::string url = "http://demo.geo-solutions.it/geoserver/ows";
87 
88  std::string encodedURL = te::core::URIEncode(url);
89 
90  // Version that will be used on WMS Requests.
91  std::string version = "1.3.0";
92 
93  std::string connInfo;
94 
95  // Perform connection
96  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS2", connInfo);
97 
98  // Test without any correct connection parameter
99  BOOST_CHECK(ds.get() == nullptr);
100 
101  connInfo = "wms://?URI=" + encodedURL;
102  ds.reset(te::da::DataSourceFactory::make("WMS2", connInfo).release());
103 
104  // Test with URI correct connection parameter
105  BOOST_CHECK_THROW(ds->open(), te::ws::core::Exception);
106 
107  connInfo = "wms://?VERSION=" + version;
108  ds.reset(te::da::DataSourceFactory::make("WMS2", connInfo).release());
109 
110  // Test with VERSION correct connection parameter
111  BOOST_CHECK_THROW(ds->open(), te::ws::core::Exception);
112 
113  connInfo = "wms://?USERDATADIR=" + usrDataDir;
114  ds.reset(te::da::DataSourceFactory::make("WMS2", connInfo).release());
115 
116  // Test with USERDATADIR correct connection parameter
117  BOOST_CHECK_THROW(ds->open(), te::ws::core::Exception);
118 
119  connInfo = "wms://?URI=" + encodedURL + "&VERSION=" + version;
120  ds.reset(te::da::DataSourceFactory::make("WMS2", connInfo).release());
121 
122  // Test with URI and VERSION correct connection parameters
123  BOOST_CHECK_THROW(ds->open(), te::ws::core::Exception);
124 
125  connInfo = "wms://?URI=" + encodedURL + "&USERDATADIR=" + usrDataDir;
126  ds.reset(te::da::DataSourceFactory::make("WMS2", connInfo).release());
127 
128  // Test with URI and USERDATADIR correct connection parameters
129  BOOST_CHECK_THROW(ds->open(), te::ws::core::Exception);
130 
131  connInfo = "wms://?VERSION=" + version + "&USERDATADIR=" + usrDataDir;
132  ds.reset(te::da::DataSourceFactory::make("WMS2", connInfo).release());
133 
134  // Test with VERSION and USERDATADIR correct connection parameters
135  BOOST_CHECK_THROW(ds->open(), te::ws::core::Exception);
136 
137  return;
138 }
139 
140 BOOST_AUTO_TEST_CASE(datasource_getmap_test)
141 {
143 
144  // Directory where temporary WMS data will be stored.
145  std::string usrDataDir = te::qt::af::AppCtrlSingleton::getInstance().getUserDataDir().toUtf8().data();
146 
147  // WMS server URL.
148  std::string url = "http://demo.geo-solutions.it/geoserver/ows";
149 
150  // Version that will be used on WMS Requests.
151  std::string version = "1.3.0";
152 
153  std::string encodedURL = te::core::URIEncode(url);
154 
155  std::string connInfo("wms://?URI=" + encodedURL + "&VERSION=" + version + "&USERDATADIR=" + usrDataDir);
156 
157  // Perform connection
158  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS2", connInfo);
159  ds->open();
160 
161  te::ws::ogc::wms::da::DataSource* wmsDs = dynamic_cast<te::ws::ogc::wms::da::DataSource*>(ds.get());
162 
163  std::shared_ptr<te::ws::ogc::WMSClient> client = wmsDs->getWMSClient();
164 
165  te::ws::ogc::wms::Capability capability = client->getCapabilities().m_capability;
166 
167  std::vector<te::ws::ogc::wms::Layer> layers = capability.m_layer.m_layers;
168 
170 
171  for(size_t i = 0; i < layers.size(); i++)
172  {
173  if(layers[i].m_name == "tasmania")
174  {
175  request.m_layers.push_back(layers[i].m_name);
176 
177  if(layers[i].m_styles.size() > 0)
178  {
179  request.m_styles.push_back(layers[i].m_styles[0].m_name);
180  }
181 
182  request.m_srs = "EPSG:4326";
183 
185  bbox.m_crs = request.m_srs;
186  bbox.m_maxX = layers[i].m_geoBoundingBox.m_eastBoundLongitude;
187  bbox.m_maxY = layers[i].m_geoBoundingBox.m_northBoundLatitude;
188  bbox.m_minX = layers[i].m_geoBoundingBox.m_westBoundLongitude;
189  bbox.m_minY = layers[i].m_geoBoundingBox.m_southBoundLatitude;
190 
191  request.m_boundingBox = bbox;
192 
193  break;
194  }
195  }
196 
197  BOOST_CHECK(request.m_layers.size() == 1);
198 
199  te::ws::ogc::wms::WMSGetMapResponse response = wmsDs->getMap(request);
200 
201  BOOST_CHECK(response.m_buffer.empty() == false);
202  BOOST_CHECK(response.m_size > 0);
203  BOOST_CHECK(response.m_format == "image/png");
204 
205  return;
206 }
207 
208 BOOST_AUTO_TEST_CASE(datasource_savegetmap_test)
209 {
211 
212  // Directory where temporary WMS data will be stored.
213  std::string usrDataDir = te::qt::af::AppCtrlSingleton::getInstance().getUserDataDir().toUtf8().data();
214 
215  // WMS server URL.
216  std::string url = "http://demo.geo-solutions.it/geoserver/ows";
217 
218  // Version that will be used on WMS Requests.
219  std::string version = "1.3.0";
220 
221  std::string encodedURL = te::core::URIEncode(url);
222 
223  std::string connInfo("wms://?URI=" + encodedURL + "&VERSION=" + version + "&USERDATADIR=" + usrDataDir);
224 
225  // Perform connection
226  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("WMS2", connInfo);
227  ds->open();
228 
229  te::ws::ogc::wms::da::DataSource* wmsDs = dynamic_cast<te::ws::ogc::wms::da::DataSource*>(ds.get());
230 
231  std::shared_ptr<te::ws::ogc::WMSClient> client = wmsDs->getWMSClient();
232 
233  te::ws::ogc::wms::Capability capability = client->getCapabilities().m_capability;
234 
235  std::vector<te::ws::ogc::wms::Layer> layers = capability.m_layer.m_layers;
236 
238 
239  for(size_t i = 0; i < layers.size(); i++)
240  {
241  if(layers[i].m_name == "tasmania")
242  {
243  request.m_layers.push_back(layers[i].m_name);
244 
245  if(layers[i].m_styles.size() > 0)
246  {
247  request.m_styles.push_back(layers[i].m_styles[0].m_name);
248  }
249 
250  request.m_srs = "EPSG:4326";
251 
253  bbox.m_crs = request.m_srs;
254  bbox.m_maxX = layers[i].m_geoBoundingBox.m_eastBoundLongitude;
255  bbox.m_maxY = layers[i].m_geoBoundingBox.m_northBoundLatitude;
256  bbox.m_minX = layers[i].m_geoBoundingBox.m_westBoundLongitude;
257  bbox.m_minY = layers[i].m_geoBoundingBox.m_southBoundLatitude;
258 
259  request.m_boundingBox = bbox;
260 
261  break;
262  }
263  }
264 
265  BOOST_CHECK(request.m_layers.size() == 1);
266 
267  std::string imageDiskPath = wmsDs->saveGetMap(request, "test_image.png");
268 
269  std::string expectedResult = usrDataDir + std::string("/wms/") + "test_image.png";
270 
271  BOOST_CHECK(imageDiskPath == expectedResult);
272 }
273 
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
This is the factory for WMS data sources.
TECOREEXPORT std::string URIEncode(const std::string &srcUri)
Encodes an decoded URI. The algorithm implementation is based on http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c12759/URI-Encoding-and-Decoding.htm.
TEOGCWMSDATAACCESSEXPORT te::da::DataSource * Build(const te::core::URI &uri)
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
The Capability WMS 1.3.0 struct.
This is a singleton for managing all data source instances available in the system.
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()
static te::dt::Date ds(2010, 01, 01)
BOOST_AUTO_TEST_CASE(datasource_open_test)
Data Source for WS OGC WMS.
static ApplicationController & getInstance()
It returns a reference to the singleton instance.
A factory for data sources.
The WMSGetMapResponse WMS 1.3.0 struct.
Base exception class for WS Core Runtime Library.
Implementation of the data source for the WMS driver.
This file contains utility functions used to manipulate data from a URI.
te::ws::ogc::wms::WMSGetMapResponse getMap(const te::ws::ogc::wms::WMSGetMapRequest &request)
Exception classes for the WS Core Runtime Library.
#define TE_OGC_WMS_DRIVER_IDENTIFIER
The OGC WMS driver identifier string.
static void add(const std::string &dsType, FactoryFnctType f)
Specifies containers for WMS data types.
The base API for controllers of TerraLib applications.
Configuration flags for the Terrralib WS OGC WMS DATA ACCESS module.
The WMSGetMapRequest WMS 1.3.0 struct.
The BoundingBox WMS 1.3.0 struct.