DataSetAdapterLayer.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/DataSetAdapterLayer.cpp
22 
23  \brief A layer with reference to a dataset.
24 */
25 
26 // TerraLib
27 #include "../core/translator/Translator.h"
28 #include "../dataaccess/dataset/DataSetAdapter.h"
29 #include "../dataaccess/dataset/DataSetTypeConverter.h"
30 #include "../dataaccess/dataset/FilteredDataSet.h"
31 #include "../dataaccess/query/DataSetName.h"
32 #include "../dataaccess/query/Field.h"
33 #include "../dataaccess/query/Fields.h"
34 #include "../dataaccess/query/From.h"
35 #include "../dataaccess/query/FromItem.h"
36 #include "../dataaccess/query/Select.h"
37 #include "../dataaccess/query/Where.h"
38 #include "../dataaccess/utils/Utils.h"
39 #include "DataSetAdapterLayer.h"
40 #include "Exception.h"
41 #include "RendererFactory.h"
42 
43 // Boost
44 #include <boost/format.hpp>
45 
46 // STL
47 #include <memory>
48 
49 const std::string te::map::DataSetAdapterLayer::sm_type("DATASETADAPTERLAYER");
50 
52  : AbstractLayer(parent)
53 {
54 }
55 
57  : AbstractLayer(id, parent)
58 {
59 }
60 
62  const std::string& title,
63  AbstractLayer* parent)
64  : AbstractLayer(id, title, parent)
65 {
66 }
67 
69 {
70  m_rtree.clear();
71 }
72 
73 std::unique_ptr<te::map::LayerSchema> te::map::DataSetAdapterLayer::getSchema() const
74 {
75  std::unique_ptr<te::da::DataSetType> type;
76  type.reset(dynamic_cast<te::da::DataSetType*>(m_converter->getResult()->clone()));
77  return type;
78 }
79 
80 std::unique_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(te::common::TraverseType travType,
81  const te::common::AccessPolicy accessPolicy) const
82 {
83  assert(!m_datasetName.empty());
84  std::unique_ptr<te::da::DataSet> inputData, outputDataSet;
85 
87 
88  inputData = ds->getDataSet(m_datasetName, travType, accessPolicy);
89  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
90 
91  return outputDataSet;
92 }
93 
94 std::unique_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(
95  const std::string& /*propertyName*/, const te::gm::Envelope* e,
97  const te::common::AccessPolicy accessPolicy) const
98 {
99  assert(!m_datasetName.empty());
100 
102 
103  std::unique_ptr<te::da::DataSetType> dsType = ds->getDataSetType(m_datasetName);
104 
105  // Gets all data
106  std::unique_ptr<te::da::DataSet> inputData = ds->getDataSet(m_datasetName, travType, accessPolicy);
107 
108  // Creates the data set adapter
109  std::unique_ptr<te::da::DataSet> adaptedDataSet;
110  adaptedDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
111 
112  std::vector<std::size_t> positions;
113 
114  m_rtree.search(*(e), positions);
115 
116  adaptedDataSet->moveBeforeFirst();
117 
118  std::unique_ptr<te::da::DataSet> result(new te::da::FilteredDataSet(adaptedDataSet.release(), positions, true));
119 
120  return result;
121 }
122 
123 std::unique_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const std::string& propertyName,
124  const te::gm::Geometry* g,
126  te::common::TraverseType travType,
127  const te::common::AccessPolicy accessPolicy) const
128 {
129  assert(!m_datasetName.empty());
130  std::unique_ptr<te::da::DataSet> inputData, outputDataSet;
131 
133 
134  inputData = ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
135  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
136 
137  return outputDataSet;
138 }
139 
140 std::unique_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(te::da::Expression* restriction,
141  te::common::TraverseType travType,
142  const te::common::AccessPolicy accessPolicy) const
143 {
144  assert(!m_datasetName.empty());
145  std::unique_ptr<te::da::DataSet> inputData, outputDataSet;
146 
148 
149  // Where clause
150  te::da::Where* filter = new te::da::Where(restriction);
151 
152  // All fields (?)
153  te::da::Fields* all = new te::da::Fields;
154  all->push_back(new te::da::Field("*"));
155 
156  // From the data set
158  te::da::From* from = new te::da::From;
159  from->push_back(fromItem);
160 
161  // The final Select
162  std::unique_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
163 
164  inputData = ds->query(select.get(), travType, accessPolicy);
165  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
166 
167  return outputDataSet;
168 }
169 
170 std::unique_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const te::da::ObjectIdSet* oids,
171  te::common::TraverseType travType,
172  const te::common::AccessPolicy accessPolicy) const
173 {
174  assert(oids);
175  std::unique_ptr<te::da::DataSet> inputData, outputDataSet;
176 
178 
179  inputData = ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
180  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
181 
182  return outputDataSet;
183 }
184 
186 {
187  if(m_datasourceId.empty() || m_datasetName.empty())
188  return false;
189 
190  if(m_converter.get() == nullptr)
191  return false;
192 
194  try
195  {
197  }
198  catch(...)
199  {
200  return false;
201  }
202 
203  if(ds.get() == nullptr || !ds->isValid() || !ds->isOpened())
204  return false;
205 
206  return true;
207 }
208 
209 void te::map::DataSetAdapterLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid, const double& scale, bool* cancel)
210 {
211  if(m_rendererType.empty())
212  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
213 
214  std::unique_ptr<te::da::DataSetType> dsType = getSchema();
215 
216  if(!dsType->hasGeom())
217  return;
218 
219  // Try get the defined renderer
220  std::unique_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
221  if(renderer.get() == nullptr)
222  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());
223 
224  renderer->draw(this, canvas, bbox, srid, scale, cancel);
225 }
226 
228 {
229  return m_converter.get();
230 }
231 
232 void te::map::DataSetAdapterLayer::setConverter(std::unique_ptr<te::da::DataSetTypeConverter> converter)
233 {
234  m_converter = std::move(converter);
235 
237 
238  std::unique_ptr<te::da::DataSetType> dsType = ds->getDataSetType(m_datasetName);
239 
240  if(m_converter->getResult()->hasGeom())
241  {
242  m_rtree.clear();
243 
244  // Gets all data
245  std::unique_ptr<te::da::DataSet> inputData = ds->getDataSet(m_datasetName);
246 
247  // Creates the data set adapter
248  std::unique_ptr<te::da::DataSet> adaptedDataSet;
249  adaptedDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
250 
251  std::size_t geomPropPos = te::da::GetFirstSpatialPropertyPos(adaptedDataSet.get());
252 
253  std::size_t pos = 0;
254 
255  while(adaptedDataSet->moveNext())
256  {
257  std::unique_ptr<te::gm::Geometry> geom(adaptedDataSet->getGeometry(geomPropPos));
258  assert(geom.get());
259 
260  te::gm::Envelope env(*geom->getMBR());
261 
262  m_rtree.insert(env, pos);
263 
264  ++pos;
265  }
266  }
267 }
268 
269 const std::string& te::map::DataSetAdapterLayer::getType() const
270 {
271  return sm_type;
272 }
273 
275 {
276  return m_rendererType;
277 }
278 
280 {
281  m_rendererType = t;
282 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
DataSetAdapterLayer(AbstractLayer *parent=0)
It initializes a new layer.
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...
std::unique_ptr< te::da::DataSetTypeConverter > m_converter
The DataSetConverter that will be cused to configure the layer.
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. ...
An exception class for the MapTools module.
This is the base class for layers.
Definition: AbstractLayer.h:77
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.
void setConverter(std::unique_ptr< te::da::DataSetTypeConverter > converter)
It sets the converter that will be used by the layer.
Base exception class for plugin module.
An abstract factory for layer renderers.
te::da::DataSetTypeConverter * getConverter() const
It returns the DataSetTypeConverter.
SpatialRelation
Spatial relations between geometric objects.
te::sam::rtree::Index< std::size_t, 4 > m_rtree
static const std::string sm_type
A static data member used in the implementation of getType method.
static te::dt::Date ds(2010, 01, 01)
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.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
This is an abstract class that models a query expression.
An converter for DataSetType.
const std::string & getRendererType() const
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
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
const std::string & getType() const
It returns the layer type: DATASET_LAYER.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
void setRendererType(const std::string &t)
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
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
A layer with reference to a DataSetTypeConverter.
This class represents a filtered data set.
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.
std::string m_rendererType
A pointer to the internal renderer used to paint this layer.
A canvas is an abstraction of a drawing area.
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
std::string m_datasourceId
DataSource id.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
std::unique_ptr< LayerSchema > getSchema() const
It returns the layer schema.