src/terralib/ws/ogc/wms/dataaccess/WMSLayer.cpp
Go to the documentation of this file.
1 #include "WMSLayer.h"
2 
3 #include "../../../../dataaccess/datasource/DataSource.h"
4 #include "../../../../dataaccess/dataset/DataSetType.h"
5 #include "../../../../dataaccess/utils/Utils.h"
6 #include "../../../../maptools/AbstractRenderer.h"
7 #include "../../../../maptools/RendererFactory.h"
8 #include "../../../../maptools/Utils.h"
9 #include "../../../../raster/RasterProperty.h"
10 #include "../../../../raster/BandProperty.h"
11 #include "../../../../raster/Band.h"
12 #include "../../../../raster/Grid.h"
13 #include "../../../../raster/RasterFactory.h"
14 #include "../../../../memory/DataSet.h"
15 #include "../../../../memory/DataSetItem.h"
16 #include "../../../core/Exception.h"
17 #include "DataSource.h"
18 #include "Utils.h"
19 
20 //Boost
21 #include <boost/format.hpp>
22 
23 //STL
24 #include <vector>
25 
26 const std::string te::ws::ogc::wms::WMSLayer::sm_type("OGCWMSLAYER");
27 
29  : te::map::AbstractLayer(parent),
30  m_rendererType("OGC_WMS_LAYER_RENDERER"),
31  m_currentSRID(TE_UNKNOWN_SRS)
32 {
33 
34 }
35 
37  : te::map::AbstractLayer(id, parent),
38  m_rendererType("OGC_WMS_LAYER_RENDERER"),
40 {
41 
42 }
43 
44 te::ws::ogc::wms::WMSLayer::WMSLayer(const std::string &id, const std::string &title, te::map::AbstractLayer *parent)
45  : te::map::AbstractLayer(id, title, parent),
46  m_rendererType("OGC_WMS_LAYER_RENDERER"),
48 {
49 
50 }
51 
53 
55 {
57 
58  te::map::CopyAbstractLayerInfo(this, layer);
59 
61 
63 
65 
66  return layer;
67 }
68 
70 {
72  {
74  }
75  else
76  {
77  return m_currentExtent;
78  }
79 }
80 
82 {
84  {
86  }
87  else
88  {
89  return m_currentSRID;
90  }
91 }
92 
94 {
95  if(m_currentSRID == srid)
96  return;
97 
98  m_currentSRID = srid;
99 
101 
103 }
104 
105 std::unique_ptr<te::map::LayerSchema> te::ws::ogc::wms::WMSLayer::getSchema() const
106 {
107  if (m_schema.get() == nullptr)
108  {
109  assert(!m_datasetName.empty());
110 
111  try
112  {
114 
115  m_schema = ds->getDataSetType(m_datasetName);
116  }
117  catch (...)
118  {
120 
121  m_schema = std::unique_ptr<te::da::DataSetType>(dataSetType);
122 
123  return std::unique_ptr<te::map::LayerSchema>(static_cast<te::map::LayerSchema*>(m_schema->clone()));
124  }
125  }
126 
127  return std::unique_ptr<te::map::LayerSchema>(static_cast<te::map::LayerSchema*>(m_schema->clone()));
128 }
129 
130 std::unique_ptr<te::da::DataSet> te::ws::ogc::wms::WMSLayer::getData(
131  te::common::TraverseType /*travType*/,
132  const te::common::AccessPolicy /*accessPolicy*/) const
133 {
134  te::rst::Raster* raster = nullptr;
135 
136  try
137  {
139  }
140  catch (...)
141  {
142  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("No data is available."));
143  }
144 
145 
146  if (m_schema.get() == nullptr)
147  {
149 
150  m_schema = ds->getDataSetType(m_datasetName);
151  }
152 
153  //update internal dataSetType if neeed
155 
156  if (rasterProperty)
157  {
158  if (rasterProperty->getBandProperties().empty())
159  {
160  for (size_t i = 0; i < raster->getNumberOfBands(); i++)
161  {
162  rasterProperty->add(new te::rst::BandProperty(*raster->getBand(i)->getProperty()));
163  }
164  }
165 
166  rasterProperty->set(new te::rst::Grid(*raster->getGrid()));
167  }
168 
169 
170  std::unique_ptr<te::map::LayerSchema> dataSetType = getSchema();
171 
172  std::map<std::string, std::string> rinfo;
173 
174  te::mem::DataSet* dataSet = new te::mem::DataSet(dataSetType.release());
175 
176  te::mem::DataSetItem* dataSetItem = new te::mem::DataSetItem(dataSet);
177 
178  dataSetItem->setRaster(0, raster);
179 
180  dataSet->add(dataSetItem);
181 
182  return std::unique_ptr<te::da::DataSet>(dataSet);
183 }
184 
185 std::unique_ptr<te::da::DataSet> te::ws::ogc::wms::WMSLayer::getData(const std::string& /*propertyName*/,
186  const te::gm::Envelope* /*e*/,
188  te::common::TraverseType travType,
189  const te::common::AccessPolicy accessPolicy) const
190 {
191  return getData(travType, accessPolicy);
192 }
193 
194 std::unique_ptr<te::da::DataSet> te::ws::ogc::wms::WMSLayer::getData(const std::string& /*propertyName*/,
195  const te::gm::Geometry* /*g*/,
197  te::common::TraverseType travType,
198  const te::common::AccessPolicy accessPolicy) const
199 {
200  return getData(travType, accessPolicy);
201 }
202 
203 std::unique_ptr<te::da::DataSet> te::ws::ogc::wms::WMSLayer::getData(te::da::Expression* /*restriction*/,
204  te::common::TraverseType travType,
205  const te::common::AccessPolicy accessPolicy) const
206 {
207  return getData(travType, accessPolicy);
208 }
209 
210 std::unique_ptr<te::da::DataSet> te::ws::ogc::wms::WMSLayer::getData(const te::da::ObjectIdSet* /*oids*/,
211  te::common::TraverseType travType,
212  const te::common::AccessPolicy accessPolicy) const
213 {
214  return getData(travType, accessPolicy);
215 }
216 
218 {
219  if(m_datasourceId.empty() || m_getMapRequest.m_layers.empty())
220  return false;
221 
223  return false;
224 
226  try
227  {
229  }
230  catch(...)
231  {
232  return false;
233  }
234 
235  if(ds.get() == nullptr || !ds->isValid() || !ds->isOpened())
236  return false;
237 
238  return true;
239 }
240 
242  const te::gm::Envelope &bbox, int srid,
243  const double &scale, bool *cancel)
244 {
245  if(m_rendererType.empty())
246  throw te::ws::core::Exception() << te::ErrorDescription((boost::format(TE_TR("Could not draw the WMS layer %1%. The renderer type is empty!")) % getTitle()).str());
247 
248  // Try get the defined renderer
249  std::unique_ptr<te::map::AbstractRenderer> renderer(te::map::RendererFactory::make(m_rendererType));
250 
251  if(renderer.get() == nullptr)
252  throw te::ws::core::Exception() << te::ErrorDescription((boost::format(TE_TR("Could not draw the WMS layer %1%. The renderer %2% could not be created!")) % getTitle() % m_rendererType).str());
253 
254  this->setCurrentSRID(srid);
255 
256  renderer->draw(this, canvas, bbox, srid, scale, cancel);
257 }
258 
259 const std::string &te::ws::ogc::wms::WMSLayer::getType() const
260 {
261  return sm_type;
262 }
263 
265 {
266  return m_datasourceId;
267 }
268 
269 void te::ws::ogc::wms::WMSLayer::setDataSourceId(const std::string &datasourceId)
270 {
271  m_datasourceId = datasourceId;
272 }
273 
275 {
276  return m_rendererType;
277 }
278 
279 void te::ws::ogc::wms::WMSLayer::setRendererType(const std::string &rendererType)
280 {
281  m_rendererType = rendererType;
282 }
283 
285 {
286  return m_getMapRequest;
287 }
288 
290 {
291  m_getMapRequest = getMapRequest;
292 }
293 
295 {
296 
298  try
299  {
301  }
302  catch(...)
303  {
305  }
306 
307  if(ds.get() == nullptr || !ds->isValid() || !ds->isOpened())
309 
310  te::ws::ogc::wms::da::DataSource* wmsDataSource = dynamic_cast<te::ws::ogc::wms::da::DataSource*>(ds.get());
311 
312  if(wmsDataSource == nullptr)
313  {
315  }
316 
317  return wmsDataSource->getMap(m_getMapRequest);
318 }
319 
320 const std::string te::ws::ogc::wms::WMSLayer::saveGetMap(const std::string &filename) const
321 {
323  try
324  {
326  }
327  catch(...)
328  {
329  return std::string("");
330  }
331 
332  if(ds.get() == nullptr || !ds->isValid() || !ds->isOpened())
333  return std::string("");
334 
335  te::ws::ogc::wms::da::DataSource* wmsDataSource = dynamic_cast<te::ws::ogc::wms::da::DataSource*>(ds.get());
336 
337  if(wmsDataSource == nullptr)
338  {
339  return std::string("");
340  }
341 
342  return wmsDataSource->saveGetMap(m_getMapRequest, filename);
343 }
344 
346 {
348  try
349  {
351  }
352  catch(...)
353  {
355  }
356 
357  if(ds.get() == nullptr || !ds->isValid() || !ds->isOpened())
359 
360  te::ws::ogc::wms::da::DataSource* wmsDataSource = dynamic_cast<te::ws::ogc::wms::da::DataSource*>(ds.get());
361 
362  if(wmsDataSource == nullptr)
363  {
365  }
366 
367  return wmsDataSource->getFeatureInfo(request);
368 }
369 
370 
371 
372 
373 
374 
375 
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
void setDataSourceId(const std::string &datadourceId)
void set(te::rst::Grid *grid)
Sets the definition of the raster grid support.
WMSLayer(te::map::AbstractLayer *parent=0)
It initializes a new layer.
std::string m_datasetName
The dataset name where we will retrieve the layer objects.
int m_currentSRID
SRID used when the layer is drawn.
TEMAPEXPORT void CopyAbstractLayerInfo(const te::map::AbstractLayer *refLayer, te::map::AbstractLayer *layer)
Make a copy of refLayer abstract attributes to layer. Creating new id.
This is the base class for layers.
Definition: AbstractLayer.h:77
virtual const te::gm::Envelope & getExtent() const
It returns the Layer extent (or minimum bounding box).
te::ws::ogc::wms::WMSGetMapRequest m_getMapRequest
boost::shared_ptr< DataSource > DataSourcePtr
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
virtual const std::string & getTitle() const
It returns the layer title.
A raster band description.
Definition: BandProperty.h:61
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
A class that models the description of a dataset.
Definition: DataSetType.h:72
TEOGCWMSDATAACCESSEXPORT te::rst::Raster * GetLayerMapRaster(const WMSLayer *wmsLayer)
te::gm::Envelope m_currentExtent
Layer extent in current SRID.
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...
SpatialRelation
Spatial relations between geometric objects.
static te::dt::Date ds(2010, 01, 01)
virtual std::unique_ptr< te::map::LayerSchema > getSchema() const
It returns the layer schema.
const te::ws::ogc::wms::WMSGetFeatureInfoResponse getFeatureInfo(const te::ws::ogc::wms::WMSGetFeatureInfoRequest &request)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::string m_datasourceId
The DataSource associated to this layer.
Raster property.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
This is an abstract class that models a query expression.
void add(DataSetItem *item)
It adds a new item to the dataset and takes its ownership.
int m_srid
The identifier of the layer spatial reference system.
void draw(te::map::Canvas *canvas, const te::gm::Envelope &bbox, int srid, const double &scale, bool *cancel)
It draws the layer geographic objects in the given canvas using the informed SRS. ...
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
void add(te::rst::BandProperty *b)
It adds a new band information to the property.
An Envelope defines a 2D rectangular region.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
An abstract class for raster data strucutures.
std::string m_rendererType
A pointer to the internal renderer used to paint this layer.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
URI C++ Library.
Definition: Attributes.h:37
void DataSet()
The WMSGetFeatureInfoRequest WMS 1.3.0 struct.
AbstractLayer * clone()
It returns a clone of the object.
std::unique_ptr< te::map::LayerSchema > m_schema
The WMS layer schema.
The WMSGetMapResponse WMS 1.3.0 struct.
The WMSGetMapResponse WMS 1.3.0 struct.
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
void setGetMapRequest(const te::ws::ogc::wms::WMSGetMapRequest &getMapRequest)
A layer with reference to a WMS Layer.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
Utility functions for the data access module.
te::ws::ogc::wms::WMSGetMapRequest getRequest() const
void setRendererType(const std::string &rendererType)
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
static AbstractRenderer * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
Base exception class for WS Core Runtime Library.
virtual const te::gm::Envelope & getExtent() const
It returns the Layer extent (or minimum bounding box).
Implementation of the data source for the WMS driver.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A canvas is an abstraction of a drawing area.
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 std::string & getType() const
It returns the layer type.
te::ws::ogc::wms::WMSGetMapResponse getMap(const te::ws::ogc::wms::WMSGetMapRequest &request)
std::unique_ptr< te::da::DataSet > getData(te::common::TraverseType travType=te::common::FORWARDONLY, const te::common::AccessPolicy accessPolicy=te::common::RAccess) const
It gets the dataset identified by the layer name.
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
std::vector< te::rst::BandProperty * > & getBandProperties()
Returns a reference to the list of bands definitions.
AbstractLayer(AbstractLayer *parent=0)
It initializes a new layer.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
static const std::string sm_type
A static data member used in the implementation of getType method.
const te::ws::ogc::wms::WMSGetFeatureInfoResponse getFeatureInfo(const te::ws::ogc::wms::WMSGetFeatureInfoRequest &request)
The WMSGetMapRequest WMS 1.3.0 struct.
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...