All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../common/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::auto_ptr<te::map::LayerSchema> te::map::DataSetAdapterLayer::getSchema() const
74 {
75  std::auto_ptr<te::da::DataSetType> type;
76  type.reset(dynamic_cast<te::da::DataSetType*>(m_converter->getResult()->clone()));
77  return type;
78 }
79 
80 std::auto_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::auto_ptr<te::da::DataSet> inputData, outputDataSet;
85 
86  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
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::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const std::string& propertyName,
95  const te::gm::Envelope* e,
97  te::common::TraverseType travType,
98  const te::common::AccessPolicy accessPolicy) const
99 {
100  assert(!m_datasetName.empty());
101 
102  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
103 
104  std::auto_ptr<te::da::DataSetType> dsType = ds->getDataSetType(m_datasetName);
105 
106  // Gets all data
107  std::auto_ptr<te::da::DataSet> inputData = ds->getDataSet(m_datasetName, travType, accessPolicy);
108 
109  // Creates the data set adapter
110  std::auto_ptr<te::da::DataSet> adaptedDataSet;
111  adaptedDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
112 
113  std::vector<std::size_t> positions;
114 
115  m_rtree.search(*(e), positions);
116 
117  adaptedDataSet->moveBeforeFirst();
118 
119  std::auto_ptr<te::da::DataSet> result(new te::da::FilteredDataSet(adaptedDataSet.release(), positions, true));
120 
121  return result;
122 }
123 
124 std::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const std::string& propertyName,
125  const te::gm::Geometry* g,
127  te::common::TraverseType travType,
128  const te::common::AccessPolicy accessPolicy) const
129 {
130  assert(!m_datasetName.empty());
131  std::auto_ptr<te::da::DataSet> inputData, outputDataSet;
132 
133  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
134 
135  inputData = ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
136  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
137 
138  return outputDataSet;
139 }
140 
141 std::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(te::da::Expression* restriction,
142  te::common::TraverseType travType,
143  const te::common::AccessPolicy accessPolicy) const
144 {
145  assert(!m_datasetName.empty());
146  std::auto_ptr<te::da::DataSet> inputData, outputDataSet;
147 
148  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
149 
150  // Where clause
151  te::da::Where* filter = new te::da::Where(restriction);
152 
153  // All fields (?)
154  te::da::Fields* all = new te::da::Fields;
155  all->push_back(new te::da::Field("*"));
156 
157  // From the data set
158  te::da::FromItem* fromItem = new te::da::DataSetName(m_datasetName);
159  te::da::From* from = new te::da::From;
160  from->push_back(fromItem);
161 
162  // The final Select
163  std::auto_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
164 
165  inputData = ds->query(select.get(), travType, accessPolicy);
166  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
167 
168  return outputDataSet;
169 }
170 
171 std::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const te::da::ObjectIdSet* oids,
172  te::common::TraverseType travType,
173  const te::common::AccessPolicy accessPolicy) const
174 {
175  assert(oids);
176  std::auto_ptr<te::da::DataSet> inputData, outputDataSet;
177 
178  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
179 
180  inputData = ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
181  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
182 
183  return outputDataSet;
184 }
185 
187 {
188  if(m_datasourceId.empty() || m_datasetName.empty())
189  return false;
190 
191  if(m_converter.get() == 0)
192  return false;
193 
195  try
196  {
197  ds = te::da::GetDataSource(m_datasourceId, true);
198  }
199  catch(...)
200  {
201  return false;
202  }
203 
204  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
205  return false;
206 
207  return true;
208 }
209 
210 void te::map::DataSetAdapterLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid)
211 {
212  if(m_rendererType.empty())
213  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
214 
215  std::auto_ptr<te::da::DataSetType> dsType = getSchema();
216 
217  if(!dsType->hasGeom())
218  return;
219 
220  // Try get the defined renderer
221  std::auto_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
222  if(renderer.get() == 0)
223  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());
224 
225  renderer->draw(this, canvas, bbox, srid);
226 }
227 
229 {
230  return m_converter.get();
231 }
232 
233 void te::map::DataSetAdapterLayer::setConverter(std::auto_ptr<te::da::DataSetTypeConverter> converter)
234 {
235  m_converter = converter;
236 
237  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
238 
239  std::auto_ptr<te::da::DataSetType> dsType = ds->getDataSetType(m_datasetName);
240 
241  if(m_converter->getResult()->hasGeom())
242  {
243  m_rtree.clear();
244 
245  // Gets all data
246  std::auto_ptr<te::da::DataSet> inputData = ds->getDataSet(m_datasetName);
247 
248  // Creates the data set adapter
249  std::auto_ptr<te::da::DataSet> adaptedDataSet;
250  adaptedDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
251 
252  std::size_t geomPropPos = te::da::GetFirstSpatialPropertyPos(adaptedDataSet.get());
253 
254  std::size_t pos = 0;
255 
256  while(adaptedDataSet->moveNext())
257  {
258  std::auto_ptr<te::gm::Geometry> geom(adaptedDataSet->getGeometry(geomPropPos));
259  assert(geom.get());
260 
261  te::gm::Envelope env(*geom->getMBR());
262 
263  m_rtree.insert(env, pos);
264 
265  ++pos;
266  }
267  }
268 }
269 
270 const std::string& te::map::DataSetAdapterLayer::getType() const
271 {
272  return sm_type;
273 }
274 
276 {
277  return m_datasetName;
278 }
279 
280 void te::map::DataSetAdapterLayer::setDataSetName(const std::string& name)
281 {
282  m_datasetName = name;
283 }
284 
286 {
287  return m_datasourceId;
288 }
289 
291 {
292  m_datasourceId = id;
293 }
294 
296 {
297  return m_rendererType;
298 }
299 
301 {
302  m_rendererType = t;
303 }
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
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.
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...
Definition: Field.h:50
This is the base class for layers.
Definition: AbstractLayer.h:76
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
An abstract factory for layer renderers.
te::da::DataSetTypeConverter * getConverter() const
It returns the DataSetTypeConverter.
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
static const std::string sm_type
A static data member used in the implementation of getType method.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void setDataSourceId(const std::string &id)
This is an abstract class that models a query expression.
Definition: Expression.h:47
An exception class for the MapTools module.
void setConverter(std::auto_ptr< te::da::DataSetTypeConverter > converter)
It sets the converter that will be used by the layer.
An converter for DataSetType.
const std::string & getRendererType() const
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
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.
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
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
const std::string & getType() const
It returns the layer type: DATASET_LAYER.
const std::string & getDataSourceId() const
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)
void setDataSetName(const std::string &name)
const std::string & getDataSetName() const
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.
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
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
void draw(Canvas *canvas, const te::gm::Envelope &bbox, int srid)
It draws the layer geographic objects in the given canvas using the informed SRS. ...
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644