src/terralib/ws/ogc/wms/dataaccess/Utils.cpp
Go to the documentation of this file.
1 #include "Utils.h"
2 #include "../../../../common/StringUtils.h"
3 #include "../../../../common/Enums.h"
4 #include "../../../core/Exception.h"
5 #include "../../../../core/translator/Translator.h"
6 #include "../../../../dataaccess/datasource/DataSource.h"
7 #include "../../../../dataaccess/datasource/DataSourceFactory.h"
8 #include "../../../../dataaccess/datasource/DataSourceTransactor.h"
9 #include "../../../../dataaccess/dataset/DataSet.h"
10 #include "../../../../raster/BandProperty.h"
11 #include "../../../../raster/Grid.h"
12 #include "../../../../raster/Raster.h"
13 #include "../../../../raster/RasterFactory.h"
14 #include "../../../../geometry/Envelope.h"
15 #include "../../../../gdal/Utils.h"
16 
17 // GDAL
18 #include <gdal_priv.h>
19 #include <gdalwarper.h>
20 #include <ogr_api.h>
21 #include <ogr_spatialref.h>
22 #include <ogrsf_frmts.h>
23 
24 #include <string>
25 
27  const int &width, const int &height)
28 {
29  //Preparing request acording to size and bounding box.
31 
32  request.m_width = width;
33  request.m_height = height;
34 
36  boundingBox.m_crs = request.m_boundingBox.m_crs;
37  boundingBox.m_minX = box.getLowerLeftX();
38  boundingBox.m_minY = box.getLowerLeftY();
39  boundingBox.m_maxX = box.getUpperRightX();
40  boundingBox.m_maxY = box.getUpperRightY();
41 
42  request.m_boundingBox = boundingBox;
43 
44  wmsLayer->setGetMapRequest(request);
45 
46  te::ws::ogc::wms::WMSGetMapResponse response = wmsLayer->getMap();
47 
48  return response;
49 }
50 
51 te::rst::Raster* te::ws::ogc::wms::GetMapRaster(te::ws::ogc::wms::WMSLayer *wmsLayer, const int& srid, const te::gm::Envelope &box, const int &width, const int &height)
52 {
53 
54  //Preparing request acording to size and bounding box.
56 
57  request.m_width = width;
58  request.m_height = height;
59 
60  std::string epsgCode = "EPSG:" + std::to_string(srid);
61 
63  boundingBox.m_crs = epsgCode;
64  boundingBox.m_minX = box.getLowerLeftX();
65  boundingBox.m_minY = box.getLowerLeftY();
66  boundingBox.m_maxX = box.getUpperRightX();
67  boundingBox.m_maxY = box.getUpperRightY();
68 
69  request.m_boundingBox = boundingBox;
70 
71  request.m_srs = epsgCode;
72 
73  wmsLayer->setGetMapRequest(request);
74 
75  std::string format = request.m_format;
76 
77  std::vector<std::string> formatSplit = te::common::SplitString(format, '/');
78 
79  if(formatSplit.size() != 2)
80  {
81  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Image format not supported."));
82  }
83 
84  format = formatSplit[1];
85 
86 
87  std::string filename = wmsLayer->getId() + "." + format;
88 
89  std::string filePath = wmsLayer->saveGetMap(filename);
90 
91  std::map<std::string, std::string> memRInfo;
92  memRInfo["URI"] = filePath;
93 
94  te::rst::Raster* raster = te::rst::RasterFactory::open(std::string("GDAL"), memRInfo, te::common::RAccess);
95 
96  if (!raster)
97  {
98  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Failed on create a Raster from WMS Server."));
99  }
100 
101  te::gm::Envelope* mbr = new te::gm::Envelope(boundingBox.m_minX, boundingBox.m_minY, boundingBox.m_maxX, boundingBox.m_maxY);
102 
103  te::rst::Grid* grid = new te::rst::Grid((unsigned int)raster->getNumberOfColumns(), (unsigned int)raster->getNumberOfRows(),
104  mbr, srid);
105 
106  double geoTrans[6];
107  geoTrans[0] = grid->getGeoreference()[0];
108  geoTrans[1] = grid->getGeoreference()[1];
109  geoTrans[2] = grid->getGeoreference()[2];
110  geoTrans[3] = grid->getGeoreference()[3];
111  geoTrans[4] = grid->getGeoreference()[4];
112  geoTrans[5] = grid->getGeoreference()[5];
113 
114  raster->getGrid()->setGeoreference(geoTrans, srid);
115 
116  delete grid;
117 
118  return raster;
119 }
120 
122 {
123  //Preparing request acording to size and bounding box.
125 
126  std::string format = request.m_format;
127 
128  std::vector<std::string> formatSplit = te::common::SplitString(format, '/');
129 
130  if (formatSplit.size() != 2)
131  {
132  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Image format not supported."));
133  }
134 
135  format = formatSplit[1];
136 
137 
138  std::string filename = wmsLayer->getId() + "." + format;
139 
140  std::string filePath = wmsLayer->saveGetMap(filename);
141 
142  std::map<std::string, std::string> memRInfo;
143  memRInfo["URI"] = filePath;
144 
145  te::rst::Raster* raster = te::rst::RasterFactory::open(std::string("GDAL"), memRInfo, te::common::RAccess);
146 
147  if (!raster)
148  {
149  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Failed on create a Raster from WMS Server."));
150  }
151 
152  te::ws::ogc::wms::BoundingBox boundingBox = request.m_boundingBox;
153 
154  te::gm::Envelope* mbr = new te::gm::Envelope(boundingBox.m_minX, boundingBox.m_minY, boundingBox.m_maxX, boundingBox.m_maxY);
155 
156  te::rst::Grid* grid = new te::rst::Grid((unsigned int)raster->getNumberOfColumns(), (unsigned int)raster->getNumberOfRows(), mbr, wmsLayer->getSRID());
157 
158  double geoTrans[6];
159  geoTrans[0] = grid->getGeoreference()[0];
160  geoTrans[1] = grid->getGeoreference()[1];
161  geoTrans[2] = grid->getGeoreference()[2];
162  geoTrans[3] = grid->getGeoreference()[3];
163  geoTrans[4] = grid->getGeoreference()[4];
164  geoTrans[5] = grid->getGeoreference()[5];
165 
166  raster->getGrid()->setGeoreference(geoTrans, wmsLayer->getSRID());
167 
168  delete grid;
169 
170  return raster;
171 }
virtual const std::string & getId() const
It returns the layer id.
TECOMMONEXPORT std::vector< std::string > SplitString(const std::string &str, const char &delimiter)
Definition: StringUtils.cpp:32
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
TEOGCWMSDATAACCESSEXPORT te::rst::Raster * GetLayerMapRaster(const WMSLayer *wmsLayer)
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
void setGeoreference(const te::gm::Coord2D &ulLocation, int srid, double resX, double resY)
Sets the information needed to georeference the grid.
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
TEOGCWMSDATAACCESSEXPORT WMSGetMapResponse GetMap(WMSLayer *wmsLayer, const te::gm::Envelope &box, const int &width, const int &height)
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
TEOGCWMSDATAACCESSEXPORT te::rst::Raster * GetMapRaster(WMSLayer *wmsLayer, const int &srid, const te::gm::Envelope &box, const int &width, const int &height)
unsigned int getNumberOfRows() const
Returns the raster number of rows.
const double * getGeoreference() const
Returns a list of 6 coefficients describing an affine transformation to georeference a grid...
The WMSGetMapResponse WMS 1.3.0 struct.
void setGetMapRequest(const te::ws::ogc::wms::WMSGetMapRequest &getMapRequest)
A layer with reference to a WMS Layer.
Grid * getGrid()
It returns the raster grid.
te::ws::ogc::wms::WMSGetMapRequest getRequest() const
Base exception class for WS Core Runtime Library.
const std::string saveGetMap(const std::string &filename) const
It execute a WMS GetMap and save the result image on disk. The request will be based on WMSGetMapRequ...
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
Utilitary functions to access GDAL and match some of its concepts to TerraLib concepts.
The WMSGetMapRequest WMS 1.3.0 struct.
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.
const te::ws::ogc::wms::WMSGetMapResponse getMap() const
It gets a WMS GetMap response with the image on a buffer. The request will be based on WMSGetMapReque...
The BoundingBox WMS 1.3.0 struct.