WCSClient.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/wcs/client/WCSClient.cpp
22 
23  \brief WS Client for OGC WCS
24 
25  \author Vinicius Campanha
26 */
27 
28 // LibCURL
29 #include <curl/curl.h>
30 
31 // TerraLib
32 #include "../../../../core/filesystem/FileSystem.h"
33 #include "../../../../core/translator/Translator.h"
34 #include "../../../../core/uri/Utils.h"
35 #include "../../../core/CurlWrapper.h"
36 #include "../../../core/Exception.h"
37 #include "WCSClient.h"
38 
39 //STL
40 #include <iostream>
41 #include <string>
42 
43 // Boost
44 #include <boost/filesystem.hpp>
45 
46 te::ws::ogc::WCSClient::WCSClient(const std::string usrDataDir, const std::string uri, const std::string version)
47  : m_version(version),
48  m_uri (uri)
49 {
50  m_dataDir = usrDataDir + "/wcs/";
51 
52  m_curl = std::shared_ptr<te::ws::core::CurlWrapper>(new te::ws::core::CurlWrapper());
53 
54  if(!m_uri.user().empty() && !m_uri.password().empty())
55  {
56  m_curl->setAuthenticationMethod(te::ws::core::HTTP_BASIC);
57  m_curl->setUsername(m_uri.user());
58  m_curl->setPassword(m_uri.password());
59  }
60 
61  std::string baseUrl = m_uri.scheme() + "://" + m_uri.host();
62 
63  if (!m_uri.port().empty())
64  {
65  baseUrl = baseUrl + ":" + m_uri.port();
66  }
67 
68  baseUrl = baseUrl + m_uri.path() + "?";
69 
70  if (!m_uri.query().empty())
71  {
72  std::string query = m_uri.query();
73 
74  size_t endsWith = query.rfind("&");
75 
76  if (endsWith != (query.size() - 1))
77  {
78  query.append("&");
79  }
80 
81  baseUrl = baseUrl + query;
82  }
83 
84  m_uri = te::core::URI(baseUrl);
85 
88 }
89 
91 
93 {
94  std::string url = m_uri.uri();
95 
96  if(m_version == "2.0.1")
97  {
98  url = url + "SERVICE=WCS" + "&VERSION=" + m_version + "&REQUEST=GetCapabilities";
99  }
100  else
101  {
102  throw te::common::Exception(TE_TR("WCS version not supported!"));
103  }
104 
105  m_curl->setTaskMessage(TE_TR("Getting Capabilities"));
106 
107  // Request the WCS Capabilities XML file
108  std::string xmlPath = te::ws::ogc::WCSClient::makeFileRequest(url, "capabilities.xml");
109 
110  try
111  {
112  // Parse the XML file into a struct
114  }
116  {
117  throw;
118  }
119 }
120 
121 
123 {
124 
125  if(m_descriptionMap.find(coverage) != m_descriptionMap.end())
126  {
127  return m_descriptionMap[coverage];
128  }
129 
131 
132  std::string url = m_uri.uri();
133 
134  if(m_version == "2.0.1")
135  {
136  url = url + "SERVICE=WCS" + "&VERSION=" + m_version + "&REQUEST=DescribeCoverage&CoverageID=" + coverage;
137  }
138  else
139  {
140  throw te::common::Exception(TE_TR("WCS version not supported!"));
141  }
142 
143  m_curl->setTaskMessage(TE_TR("Getting Coverage Description"));
144 
145  // Request the WCS Describe Coverage XML file
146  std::string xmlPath = te::ws::ogc::WCSClient::makeFileRequest(url, "describeCoverage.xml");
147 
148 
149  try
150  {
151  // Parse the XML file into a struct
152  describeCoverage = m_parser.parseDescribeCoverage(xmlPath);
153 
154  }
156  {
157  throw;
158  }
159 
161 
162  return describeCoverage;
163 }
164 
165 std::string te::ws::ogc::WCSClient::getFileExtension(const std::string& format) const
166 {
167  std::string fileExtension;
168 
169  if(format == "image/tiff")
170  {
171  fileExtension = ".tif";
172  }
173 
174  return fileExtension;
175 }
176 
177 
179 {
180  std::string coveragePath;
181 
182  std::string url = m_uri.uri();
183 
184  if(m_version == "2.0.1")
185  {
186  url = url + "SERVICE=WCS" + "&VERSION=" + m_version + "&REQUEST=GetCoverage&COVERAGEID=" + coverageRequest.coverageID;
187 
188  if(!coverageRequest.format.empty())
189  url += "&FORMAT=" + coverageRequest.format;
190 
191  if(!coverageRequest.mediaType.empty())
192  url += "&MEDIATYPE=" + coverageRequest.mediaType;
193 
194  const te::ws::ogc::wcs::EnvelopeWithTimePeriod* envelope = &coverageRequest.envelope;
195 
196  std::string x1 = envelope->lowerCorner_X;
197  std::string y1 = envelope->lowerCorner_Y;
198  std::string x2 = envelope->upperCorner_X;
199  std::string y2 = envelope->upperCorner_Y;
200 
201  std::string subset1, subset2;
202 
203  if(!x1.empty() && !x2.empty())
204  subset1 = "&SUBSET=" + envelope->firstLabel + "(" + x1 + (!x1.empty() && !x2.empty()? ",": "") + x2 + ")";
205 
206  if(!y1.empty() && !y2.empty())
207  subset2 = "&SUBSET=" + envelope->secondLabel + "(" + y1 + (!y1.empty() && !y2.empty()? ",": "") + y2 + ")";
208 
209  url += subset1 + subset2;
210 
211  if(!envelope->timeLabel.empty())
212  {
213  url += "&SUBSET=" + envelope->timeLabel + "(" + te::core::URIEncode("\"");
214 
215  if(coverageRequest.time.empty())
216  url += envelope->endPosition;
217  else
218  url += coverageRequest.time;
219 
220  url += te::core::URIEncode("\"")+ ")";
221  }
222 
223  for(std::map<std::string, std::string>::const_iterator parameter = coverageRequest.additionalParameters.begin(); parameter != coverageRequest.additionalParameters.end(); parameter++)
224  {
225  url += "&" + parameter->first + "=" + parameter->second;
226  }
227  }
228  else
229  {
230  throw te::common::Exception(TE_TR("WCS version not supported!"));
231  }
232 
233  m_curl->setTaskMessage(TE_TR("Getting Coverage"));
234 
235  std::string fileName = coverageRequest.coverageID;
236 
237  if(!coverageRequest.time.empty())
238  fileName += "_" + coverageRequest.time;
239 
240  fileName += getFileExtension(coverageRequest.format);
241 
242  try
243  {
244  coveragePath = makeFileRequest(url, fileName, taskProgress);
245  }
247  {
248  throw;
249  }
250 
251  return coveragePath;
252 }
253 
254 std::string te::ws::ogc::WCSClient::makeFileRequest(const std::string url, const std::string fileName, te::common::TaskProgress* taskProgress) const
255 {
256  std::string path = m_dataDir + fileName;
257 
258  m_curl->downloadFile(url, path, taskProgress);
259 
260  return path;
261 }
262 
264 {
265  return m_capabilities;
266 }
267 
269 {
270  m_curl.reset(curlWrapper);
271 }
WS Client for OGC WCS.
std::string path() const
Retrieving the path.
Definition: URI.cpp:118
std::string scheme() const
Retrieving the scheme.
Definition: URI.cpp:93
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
static bool createDirectories(const std::string &path)
Creates a directory for any element of path that does not exist.
Definition: FileSystem.cpp:153
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.
CoverageDescription parseDescribeCoverage(const std::string &xmlPath)
std::string makeFileRequest(const std::string url, const std::string fileName, te::common::TaskProgress *taskProgress=0) const
Executes a request on a WCS server.
Definition: WCSClient.cpp:254
te::ws::ogc::wcs::Capabilities m_capabilities
Definition: WCSClient.h:138
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
te::ws::ogc::wcs::XMLParser m_parser
Definition: WCSClient.h:141
std::string m_version
Definition: WCSClient.h:135
std::string password() const
Retrieving the password information.
Definition: URI.cpp:103
An abstraction to Lib Curl functions.
Definition: CurlWrapper.h:61
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
std::map< std::string, std::string > additionalParameters
te::core::URI m_uri
Definition: WCSClient.h:137
te::ws::ogc::wcs::CoverageDescription describeCoverage(const std::string coverage)
Method to get the information about a coverage in the WCS server.
Definition: WCSClient.cpp:122
void updateCapabilities()
Method to get the capabilities from a WCS server and store in m_capabilities member.
Definition: WCSClient.cpp:92
std::string m_dataDir
Definition: WCSClient.h:136
~WCSClient()
Default destructor.
void setCurlWrapper(te::ws::core::CurlWrapper *curlWrapper)
Sets the TerraLib CurlWrapper to be used internally.
Definition: WCSClient.cpp:268
WCSClient(const std::string usrDataDir="", const std::string uri="", const std::string version="2.0.1")
Class constructor. It initializes the m_uri and m_version class members.
Definition: WCSClient.cpp:46
std::string port() const
Retrieving the port.
Definition: URI.cpp:113
std::string host() const
Retrieving the host.
Definition: URI.cpp:108
A struct to set the parameters of requested coverage.
const std::string & uri() const
Retrieving the full URI.
Definition: URI.cpp:88
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Base exception class for WS Core Runtime Library.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
std::string getFileExtension(const std::string &format) const
Definition: WCSClient.cpp:165
std::string getCoverage(const te::ws::ogc::wcs::CoverageRequest coverageRequest, te::common::TaskProgress *taskProgress=0) const
Method to get the coverage from the WCS server.
Definition: WCSClient.cpp:178
Capabilities parseCapabilities(const std::string &xmlPath)
std::map< std::string, te::ws::ogc::wcs::CoverageDescription > m_descriptionMap
Definition: WCSClient.h:140
const te::ws::ogc::wcs::Capabilities & getCapabilities() const
Return the m_capabilities member.
Definition: WCSClient.cpp:263
std::string user() const
Retrieving the user information.
Definition: URI.cpp:98
std::shared_ptr< te::ws::core::CurlWrapper > m_curl
Definition: WCSClient.h:139