src/terralib/ws/ogc/wms/dataaccess/WMSLayerRenderer.cpp
Go to the documentation of this file.
1 #include "WMSLayerRenderer.h"
2 
3 #include "WMSLayer.h"
4 #include "Utils.h"
5 
6 #include "../../../../core/translator/Translator.h"
7 #include "../../../../maptools/AbstractLayer.h"
8 #include "../../../../maptools/Canvas.h"
9 #include "../../../../maptools/Utils.h"
10 #include "../../../../dataaccess/utils/Utils.h"
11 #include "../../../../geometry/Envelope.h"
12 #include "../../../../se/Utils.h"
13 #include "../../../../se/CoverageStyle.h"
14 #include "../../../../raster/RasterProperty.h"
15 #include "../../../core/Exception.h"
16 #include "../../../core/Utils.h"
17 #include "../../../../srs/Config.h"
18 #include "../../../../srs/SpatialReferenceSystemManager.h"
19 
20 // Boost
21 #include <boost/format.hpp>
22 
24 
26 
27 void te::ws::ogc::wms::WMSLayerRenderer::draw(te::map::AbstractLayer *layer, te::map::Canvas *canvas, const te::gm::Envelope &bbox, int srid, const double &scale, bool *cancel)
28 {
29 
30  te::ws::ogc::wms::WMSLayer* wmsLayer = dynamic_cast<te::ws::ogc::wms::WMSLayer*>(layer);
31 
32  if(wmsLayer == nullptr)
33  {
34  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("Wrong type render type for this layer!"));
35  }
36 
37  te::gm::Envelope reprojectedBBOX(bbox);
38 
39  if(srid != TE_UNKNOWN_SRS)
40  {
41  reprojectedBBOX.transform(srid, wmsLayer->getSRID());
42  }
43  else if(layer->getSRID() != srid)
44  {
45  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The layer or map has no valid SRID!"));
46  }
47 
48  if(!reprojectedBBOX.intersects(wmsLayer->getExtent()))
49  return;
50 
51  //update request
52 
54 
55  request.m_width = canvas->getWidth();
56  request.m_height = canvas->getHeight();
57 
58  std::string epsgCode = "EPSG:" + std::to_string(srid);
59 
61  boundingBox.m_crs = epsgCode;
62  boundingBox.m_minX = bbox.getLowerLeftX();
63  boundingBox.m_minY = bbox.getLowerLeftY();
64  boundingBox.m_maxX = bbox.getUpperRightX();
65  boundingBox.m_maxY = bbox.getUpperRightY();
66 
67  request.m_boundingBox = boundingBox;
68 
69  request.m_srs = epsgCode;
70 
71  wmsLayer->setGetMapRequest(request);
72 
73  std::unique_ptr<te::rst::Raster> memRaster(te::map::GetRaster(wmsLayer));
74 
75  std::unique_ptr<te::da::DataSetType> dsType = wmsLayer->getSchema();
76 
77  te::rst::RasterProperty* rasterProperty = te::da::GetFirstRasterProperty(dsType.get());
78 
79  // get the associated layer style
80  te::se::Style* style = wmsLayer->getStyle();
81  if(style == nullptr)
82  {
83  // try create an appropriate style
84  style = te::se::CreateCoverageStyle(rasterProperty->getBandProperties());
85 
86  if(style == nullptr)
87  throw te::ws::core::Exception() << te::ErrorDescription((boost::format(TE_TR("Could not create a default coverage style to the layer %1%.")) % wmsLayer->getTitle()).str());
88 
89  wmsLayer->setStyle(style);
90  }
91 
92  // should I render this style?
93  te::se::CoverageStyle* cs = dynamic_cast<te::se::CoverageStyle*>(style);
94  if(cs == nullptr)
95  throw te::ws::core::Exception() << te::ErrorDescription(TE_TR("The layer style is not a Coverage Style!"));
96 
97  te::map::DrawRaster(memRaster.get(), canvas, bbox, srid, bbox, srid, cs, layer->getRasterContrast(), scale, cancel);
98 }
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
This is the base class for layers.
Definition: AbstractLayer.h:77
#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.
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
TESEEXPORT Style * CreateCoverageStyle(const std::vector< te::rst::BandProperty * > &properties)
Try creates an appropriate coverage style based on given band properties.
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
virtual std::unique_ptr< te::map::LayerSchema > getSchema() const
It returns the layer schema.
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
void draw(te::map::AbstractLayer *layer, 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 SRS informed. ...
Raster property.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
TEMAPEXPORT void DrawRaster(te::da::DataSetType *type, te::da::DataSourcePtr ds, Canvas *canvas, const te::gm::Envelope &bbox, int bboxSRID, const te::gm::Envelope &visibleArea, int srid, te::se::CoverageStyle *style, te::map::RasterContrast *rc, const double &scale, bool *cancel)
An Envelope defines a 2D rectangular region.
virtual te::se::Style * getStyle() const
It returns the Style associated to the layer.
virtual int getHeight() const =0
It returns the canvas height.
void setGetMapRequest(const te::ws::ogc::wms::WMSGetMapRequest &getMapRequest)
A layer with reference to a WMS Layer.
Utility functions for the data access module.
te::ws::ogc::wms::WMSGetMapRequest getRequest() const
Base exception class for WS Core Runtime Library.
virtual const te::gm::Envelope & getExtent() const
It returns the Layer extent (or minimum bounding box).
virtual te::map::RasterContrast * getRasterContrast() const
It returns the raster contrast associated to the Layer.
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
A canvas is an abstraction of a drawing area.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
virtual void setStyle(te::se::Style *style)
It sets the Style associated to the layer.
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.
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
virtual int getWidth() const =0
It returns the canvas width.
The WMSGetMapRequest WMS 1.3.0 struct.
The BoundingBox WMS 1.3.0 struct.