All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
WMSLayer.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/wms/WMSLayer.cpp
22 
23  \brief A layer with reference to a WMS Layer.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../dataaccess/utils/Utils.h"
29 #include "../maptools/AbstractRenderer.h"
30 #include "../maptools/RendererFactory.h"
31 #include "Exception.h"
32 #include "WMSLayer.h"
33 
34 // Boost
35 #include <boost/format.hpp>
36 #include <boost/lexical_cast.hpp>
37 
38 const std::string te::wms::WMSLayer::sm_type("WMSLAYER");
39 
41  : te::map::AbstractLayer(parent),
42  m_rendererType("WMS_LAYER_RENDERER"),
43  m_width(boost::lexical_cast<std::size_t>(TE_WMS_DEFAULT_WIDTH)),
44  m_height(boost::lexical_cast<std::size_t>(TE_WMS_DEFAULT_HEIGHT)),
46  m_schema(0)
47 {
48 }
49 
50 te::wms::WMSLayer::WMSLayer(const std::string& id, te::map::AbstractLayer* parent)
51  : te::map::AbstractLayer(id, parent),
52  m_rendererType("WMS_LAYER_RENDERER"),
53  m_width(boost::lexical_cast<std::size_t>(TE_WMS_DEFAULT_WIDTH)),
54  m_height(boost::lexical_cast<std::size_t>(TE_WMS_DEFAULT_HEIGHT)),
56  m_schema(0)
57 {
58 }
59 
60 te::wms::WMSLayer::WMSLayer(const std::string& id,
61  const std::string& title,
62  te::map::AbstractLayer* parent)
63  : te::map::AbstractLayer(id, title, parent),
64  m_rendererType("WMS_LAYER_RENDERER"),
65  m_width(boost::lexical_cast<std::size_t>(TE_WMS_DEFAULT_WIDTH)),
66  m_height(boost::lexical_cast<std::size_t>(TE_WMS_DEFAULT_HEIGHT)),
68  m_schema(0)
69 {
70 }
71 
73 {
74 }
75 
76 std::auto_ptr<te::map::LayerSchema> te::wms::WMSLayer::getSchema() const
77 {
78  if(m_schema.get())
79  return std::auto_ptr<te::map::LayerSchema>(static_cast<te::map::LayerSchema*>(m_schema->clone()));
80 
81  assert(!m_datasetName.empty());
82 
83  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
84 
85  m_schema = ds->getDataSetType(m_datasetName);
86 
87  return std::auto_ptr<te::map::LayerSchema>(static_cast<te::map::LayerSchema*>(m_schema->clone()));
88 }
89 
90 std::auto_ptr<te::da::DataSet> te::wms::WMSLayer::getData(te::common::TraverseType travType,
91  const te::common::AccessPolicy accessPolicy) const
92 {
93  std::string request = buildRequest();
94 
95  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
96 
97  return ds->getDataSet(request, travType, accessPolicy);
98 }
99 
100 std::auto_ptr<te::da::DataSet> te::wms::WMSLayer::getData(const std::string& propertyName,
101  const te::gm::Envelope* e,
103  te::common::TraverseType travType,
104  const te::common::AccessPolicy accessPolicy) const
105 {
106  std::string request = buildRequest();
107 
108  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
109 
110  return ds->getDataSet(request, propertyName, e, r, travType, accessPolicy);
111 }
112 
113 std::auto_ptr<te::da::DataSet> te::wms::WMSLayer::getData(const std::string& propertyName,
114  const te::gm::Geometry* g,
116  te::common::TraverseType travType,
117  const te::common::AccessPolicy accessPolicy) const
118 {
119  std::string request = buildRequest();
120 
121  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
122 
123  return ds->getDataSet(request, propertyName, g, r, travType, accessPolicy);
124 }
125 
126 std::auto_ptr<te::da::DataSet> te::wms::WMSLayer::getData(te::da::Expression* restriction,
127  te::common::TraverseType travType,
128  const te::common::AccessPolicy accessPolicy) const
129 {
130  throw Exception(TE_TR("Query operations are not supported by the WMS Layer!"));
131 }
132 
133 std::auto_ptr<te::da::DataSet> te::wms::WMSLayer::getData(const te::da::ObjectIdSet* oids,
134  te::common::TraverseType travType,
135  const te::common::AccessPolicy accessPolicy) const
136 {
137  throw Exception(TE_TR("The ObjectIdSet concept is not supported by the WMS Layer!"));
138 }
139 
141 {
142  if(m_datasourceId.empty() || m_datasetName.empty())
143  return false;
144 
145  if(m_width == 0 || m_height == 0 || m_format.empty())
146  return false;
147 
149  try
150  {
151  ds = te::da::GetDataSource(m_datasourceId, true);
152  }
153  catch(...)
154  {
155  return false;
156  }
157 
158  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
159  return false;
160 
161  return true;
162 }
163 
164 void te::wms::WMSLayer::draw(te::map::Canvas* canvas, const te::gm::Envelope& bbox, int srid)
165 {
166  if(m_rendererType.empty())
167  throw Exception((boost::format(TE_TR("Could not draw the WMS layer %1%. The renderer type is empty!")) % getTitle()).str());
168 
169  // Try get the defined renderer
170  std::auto_ptr<te::map::AbstractRenderer> renderer(te::map::RendererFactory::make(m_rendererType));
171  if(renderer.get() == 0)
172  throw Exception((boost::format(TE_TR("Could not draw the WMS layer %1%. The renderer %2% could not be created!")) % getTitle() % m_rendererType).str());
173 
174  renderer->draw(this, canvas, bbox, srid);
175 }
176 
177 const std::string& te::wms::WMSLayer::getType() const
178 {
179  return sm_type;
180 }
181 
182 const std::string& te::wms::WMSLayer::getDataSetName() const
183 {
184  return m_datasetName;
185 }
186 
187 void te::wms::WMSLayer::setDataSetName(const std::string& name)
188 {
189  m_datasetName = name;
190 }
191 
192 const std::string& te::wms::WMSLayer::getDataSourceId() const
193 {
194  return m_datasourceId;
195 }
196 
197 void te::wms::WMSLayer::setDataSourceId(const std::string& id)
198 {
199  m_datasourceId = id;
200 }
201 
202 const std::string& te::wms::WMSLayer::getRendererType() const
203 {
204  return m_rendererType;
205 }
206 
207 void te::wms::WMSLayer::setRendererType(const std::string& t)
208 {
209  m_rendererType = t;
210 }
211 
212 void te::wms::WMSLayer::setWidth(const std::size_t& width)
213 {
214  m_width = width;
215 }
216 
217 void te::wms::WMSLayer::setHeight(const std::size_t& height)
218 {
219  m_height = height;
220 }
221 
222 void te::wms::WMSLayer::setFormat(const std::string& format)
223 {
224  m_format = format;
225 }
226 
228 {
229  assert(!m_datasetName.empty());
230  assert(m_width > 0);
231  assert(m_height > 0);
232  assert(!m_format.empty());
233 
234  std::string request = "LAYER=" + m_datasetName;
235  request += "&WIDTH=" + boost::lexical_cast<std::string>(m_width);
236  request += "&HEIGHT=" + boost::lexical_cast<std::string>(m_height);
237  request += "&FORMAT=" + m_format;
238 
239  return request;
240 }
A layer with reference to a WMS Layer.
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
void setDataSourceId(const std::string &id)
Definition: WMSLayer.cpp:197
std::auto_ptr< te::map::LayerSchema > getSchema() const
It returns the layer schema.
Definition: WMSLayer.cpp:76
#define TE_WMS_DEFAULT_WIDTH
It specifies the default width used on GetMap request.
Definition: Config.h:46
This is the base class for layers.
Definition: AbstractLayer.h:76
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
std::auto_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.
Definition: WMSLayer.cpp:90
#define TE_WMS_DEFAULT_IMAGE_FORMAT
It specifies the default image format used on GetMap request.
Definition: Config.h:60
void setFormat(const std::string &format)
Definition: WMSLayer.cpp:222
const std::string & getType() const
It returns the layer type.
Definition: WMSLayer.cpp:177
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
const std::string & getDataSetName() const
Definition: WMSLayer.cpp:182
void setDataSetName(const std::string &name)
Definition: WMSLayer.cpp:187
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
static const std::string sm_type
A static data member used in the implementation of getType method.
Definition: WMSLayer.h:141
This is an abstract class that models a query expression.
Definition: Expression.h:47
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
An exception class for the TerraLib WMS module.
const std::string & getDataSourceId() const
Definition: WMSLayer.cpp:192
WMSLayer(te::map::AbstractLayer *parent=0)
It initializes a new layer.
Definition: WMSLayer.cpp:40
#define TE_WMS_DEFAULT_HEIGHT
It specifies the default height used on GetMap request.
Definition: Config.h:53
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
void setWidth(const std::size_t &width)
Definition: WMSLayer.cpp:212
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
static AbstractRenderer * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
std::string buildRequest() const
Definition: WMSLayer.cpp:227
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
Definition: WMSLayer.cpp:140
const std::string & getRendererType() const
Definition: WMSLayer.cpp:202
~WMSLayer()
Destructor.
Definition: WMSLayer.cpp:72
void draw(te::map::Canvas *canvas, const te::gm::Envelope &bbox, int srid)
It draws the layer geographic objects in the given canvas using the informed SRS. ...
Definition: WMSLayer.cpp:164
void setRendererType(const std::string &t)
Definition: WMSLayer.cpp:207
void setHeight(const std::size_t &height)
Definition: WMSLayer.cpp:217