DataSetLayer.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/maptools/DataSetLayer.cpp
22 
23  \brief A layer with reference to a dataset.
24 */
25 
26 // TerraLib
27 #include "../core/translator/Translator.h"
28 #include "../dataaccess/query/DataSetName.h"
29 #include "../dataaccess/query/Field.h"
30 #include "../dataaccess/query/Fields.h"
31 #include "../dataaccess/query/From.h"
32 #include "../dataaccess/query/FromItem.h"
33 #include "../dataaccess/query/Select.h"
34 #include "../dataaccess/query/SpatialQueryProcessor.h"
35 #include "../dataaccess/query/Where.h"
36 #include "../dataaccess/utils/Utils.h"
37 #include "../geometry/GeometryProperty.h"
38 #include "../raster/Grid.h"
39 #include "../raster/RasterProperty.h"
40 //#include "../se/Style.h"
41 #include "DataSetLayer.h"
42 //#include "Chart.h"
43 #include "Exception.h"
44 //#include "Grouping.h"
45 //#include "RasterContrast.h"
46 #include "RendererFactory.h"
47 #include "Utils.h"
48 
49 // Boost
50 #include <boost/format.hpp>
51 
52 // STL
53 #include <memory>
54 
55 const std::string te::map::DataSetLayer::sm_type("DATASETLAYER");
56 
58  : AbstractLayer(parent),
59  m_schema(nullptr)
60 {
61 }
62 
63 te::map::DataSetLayer::DataSetLayer(const std::string& id, AbstractLayer* parent)
64  : AbstractLayer(id, parent),
65  m_schema(nullptr)
66 {
67 }
68 
70  const std::string& title,
71  AbstractLayer* parent)
72  : AbstractLayer(id, title, parent),
73  m_schema(nullptr)
74 {
75 }
76 
78 {
79  delete m_schema;
80 }
81 
83 {
85 
86  te::map::CopyAbstractLayerInfo(this, layer);
87 
89 
90  return layer;
91 }
92 
94 {
95  // set the srid associated to the parent AbstractLayer
96  m_srid=srid;
97 
98  // if there is no shema cached try to retrieve one
99  if (!m_schema)
100  {
101  // this guard is to deal with empty dataset layers
102  if (!m_datasourceId.empty() && !m_datasetName.empty())
103  loadSchema();
104  }
105 
106  // propagate it to my cached dataset schema
107  if(m_schema)
108  {
109  if (m_schema->hasGeom())
110  {
112  myGeom->setSRID(srid);
113  }
114  else if (m_schema->hasRaster())
115  {
117  rstProp->getGrid()->setSRID(srid);
118  }
119  }
120 }
121 
123 {
124  if(m_schema)
125  {
127 
128  std::unique_ptr<LayerSchema> type = ds->getDataSetType(m_datasetName);
129 
130  // Cache the schema from datasource
131  if(type.get())
132  m_schema = static_cast<LayerSchema*>(type->clone());
133  }
134 }
135 
136 std::unique_ptr<te::map::LayerSchema> te::map::DataSetLayer::getSchema() const
137 {
138  if(m_schema == nullptr && !m_datasourceId.empty() && !m_datasetName.empty())
139  {
141 
142  std::unique_ptr<LayerSchema> type = ds->getDataSetType(m_datasetName);
143 
144  if(!type.get())
145  return std::unique_ptr<te::da::DataSetType>();
146 
147  // Cache the schema from datasource
148  m_schema = static_cast<LayerSchema*>(type->clone());
149 
150  return type;
151  }
152  return std::unique_ptr<LayerSchema>(static_cast<LayerSchema*>(m_schema->clone()));
153 }
154 
155 std::unique_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::common::TraverseType travType,
156  const te::common::AccessPolicy accessPolicy) const
157 {
158  assert(!m_datasetName.empty());
159 
161 
162  ds->setEncoding(this->getEncoding());
163 
164  return ds->getDataSet(m_datasetName, travType, accessPolicy);
165 }
166 
167 std::unique_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
168  const te::gm::Envelope* e,
170  te::common::TraverseType travType,
171  const te::common::AccessPolicy accessPolicy) const
172 {
173  assert(!m_datasetName.empty());
174 
176 
177  ds->setEncoding(this->getEncoding());
178 
179  return ds->getDataSet(m_datasetName, propertyName, e, r, travType, accessPolicy);
180 }
181 
182 std::unique_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
183  const te::gm::Geometry* g,
185  te::common::TraverseType travType,
186  const te::common::AccessPolicy accessPolicy) const
187 {
188  assert(!m_datasetName.empty());
189 
191 
192  ds->setEncoding(this->getEncoding());
193 
194  return ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
195 }
196 
197 std::unique_ptr<te::da::DataSet> te::map::DataSetLayer::getData(
198  te::da::Expression* restriction, te::common::TraverseType travType,
199  const te::common::AccessPolicy /*accessPolicy*/) const
200 {
201  assert(restriction);
202  assert(!m_datasetName.empty());
203 
205 
206  ds->setEncoding(this->getEncoding());
207 
208  // Where clause
209  te::da::Where* filter = new te::da::Where(restriction);
210 
211  // All fields (?)
212  te::da::Fields* all = new te::da::Fields;
213  all->push_back(new te::da::Field("*"));
214 
215  // From the data set
217  te::da::From* from = new te::da::From;
218  from->push_back(fromItem);
219 
220  // The final Select
221  std::unique_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
222 
224  return sqp.getDataSet(ds, select.get(), travType);
225 }
226 
227 std::unique_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const te::da::ObjectIdSet* oids,
228  te::common::TraverseType travType,
229  const te::common::AccessPolicy accessPolicy) const
230 {
231  assert(oids);
232 
234 
235  ds->setEncoding(this->getEncoding());
236 
237  return ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
238 }
239 
241 {
242  if(m_datasourceId.empty() || m_datasetName.empty())
243  return false;
244 
246  try
247  {
249  this->getSchema().get();
250  }
251  catch(...)
252  {
253  return false;
254  }
255 
256  if(ds.get() == nullptr || !ds->isValid() || !ds->isOpened())
257  return false;
258 
259  return true;
260 }
261 
262 void te::map::DataSetLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid, const double& scale, bool* cancel)
263 {
264  if(m_rendererType.empty())
265  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
266 
267  // Try get the defined renderer
268  std::unique_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
269  if(renderer.get() == nullptr)
270  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer %2% could not be created!")) % getTitle() % m_rendererType).str());
271 
272  renderer->draw(this, canvas, bbox, srid, scale, cancel);
273 }
274 
275 const std::string& te::map::DataSetLayer::getType() const
276 {
277  return sm_type;
278 }
279 
280 const std::string& te::map::DataSetLayer::getRendererType() const
281 {
282  return m_rendererType;
283 }
284 
285 void te::map::DataSetLayer::setRendererType(const std::string& t)
286 {
287  m_rendererType = t;
288 }
289 
291 {
292  delete m_schema;
293  m_schema = nullptr;
294 }
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)
Geometric property.
std::string m_datasetName
The dataset name where we will retrieve the layer objects.
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
void setSRID(int srid)
It sets the spatial reference system identifier associated to this property.
TEMAPEXPORT void CopyAbstractLayerInfo(const te::map::AbstractLayer *refLayer, te::map::AbstractLayer *layer)
Make a copy of refLayer abstract attributes to layer. Creating new id.
An exception class for the MapTools module.
This is the base class for layers.
Definition: AbstractLayer.h:77
Property * clone() const
It returns a clone of the object.
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
boost::shared_ptr< DataSource > DataSourcePtr
virtual const std::string & getTitle() const
It returns the layer title.
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
~DataSetLayer()
Destructor.
Base exception class for plugin module.
A class that models the description of a dataset.
Definition: DataSetType.h:72
An abstract factory for layer renderers.
std::string m_rendererType
A pointer to the internal renderer used to paint this layer.
Definition: DataSetLayer.h:142
void setSRID(int srid)
It sets the Spatial Reference System ID associated to the Layer.
SpatialRelation
Spatial relations between geometric objects.
std::unique_ptr< LayerSchema > getSchema() const
It returns the layer schema.
static te::dt::Date ds(2010, 01, 01)
A layer with reference to a dataset.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
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.
A basic query processor for spatial restrictions.
Raster property.
This is an abstract class that models a query expression.
int m_srid
The identifier of the layer spatial reference system.
te::core::EncodingType getEncoding() const
It returns the encoding type.
void setRendererType(const std::string &t)
AbstractLayer * clone()
It returns a clone of the object.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
static const std::string sm_type
A static data member used in the implementation of getType method.
Definition: DataSetLayer.h:145
virtual void setOutOfDate()
Its indicate that the layer schema is out of date.
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
bool hasRaster() const
It returns true if the DataSetType has at least one raster property; otherwise, it returns false...
Definition: DataSetType.h:660
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
const std::string & getType() const
It returns the layer type: DATASET_LAYER.
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
virtual std::unique_ptr< DataSet > getDataSet(const DataSourcePtr &ds, const Select &q, te::common::TraverseType travType=te::common::FORWARDONLY)
Utility functions for the data access module.
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.
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
virtual void select(te::da::ObjectIdSet *oids)
It adds the given oids to the selected group of this Layer.
te::rst::Grid * getGrid()
Returns the definition of the raster grid support.
A canvas is an abstraction of a drawing area.
std::string m_datasourceId
DataSource id.
LayerSchema * m_schema
The dataset schema.
Definition: DataSetLayer.h:143
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setSRID(int srid)
Just sets the grid spatial reference system identifier.
DataSetLayer(AbstractLayer *parent=0)
It initializes a new layer.
void loadSchema() const
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
void draw(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. ...
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
const std::string & getRendererType() const