All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataSetAdapterLayer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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  if(dsType->hasGeom())
107  {
108  return ds->getDataSet(m_datasetName, propertyName, e, r, travType, accessPolicy);
109  }
110 
111  // Gets all data
112  std::auto_ptr<te::da::DataSet> inputData = ds->getDataSet(m_datasetName, travType, accessPolicy);
113 
114  // Creates the data set adapter
115  std::auto_ptr<te::da::DataSet> adaptedDataSet;
116  adaptedDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
117 
118  std::vector<std::size_t> positions;
119 
120  m_rtree.search(*(e), positions);
121 
122  adaptedDataSet->moveBeforeFirst();
123 
124  std::auto_ptr<te::da::DataSet> result(new te::da::FilteredDataSet(adaptedDataSet.release(), positions, true));
125 
126  return result;
127 }
128 
129 std::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const std::string& propertyName,
130  const te::gm::Geometry* g,
132  te::common::TraverseType travType,
133  const te::common::AccessPolicy accessPolicy) const
134 {
135  assert(!m_datasetName.empty());
136  std::auto_ptr<te::da::DataSet> inputData, outputDataSet;
137 
138  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
139 
140  inputData = ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
141  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
142 
143  return outputDataSet;
144 }
145 
146 std::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(te::da::Expression* restriction,
147  te::common::TraverseType travType,
148  const te::common::AccessPolicy accessPolicy) const
149 {
150  assert(!m_datasetName.empty());
151  std::auto_ptr<te::da::DataSet> inputData, outputDataSet;
152 
153  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
154 
155  // Where clause
156  te::da::Where* filter = new te::da::Where(restriction);
157 
158  // All fields (?)
159  te::da::Fields* all = new te::da::Fields;
160  all->push_back(new te::da::Field("*"));
161 
162  // From the data set
163  te::da::FromItem* fromItem = new te::da::DataSetName(m_datasetName);
164  te::da::From* from = new te::da::From;
165  from->push_back(fromItem);
166 
167  // The final Select
168  std::auto_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
169 
170  inputData = ds->query(select.get(), travType, accessPolicy);
171  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
172 
173  return outputDataSet;
174 }
175 
176 std::auto_ptr<te::da::DataSet> te::map::DataSetAdapterLayer::getData(const te::da::ObjectIdSet* oids,
177  te::common::TraverseType travType,
178  const te::common::AccessPolicy accessPolicy) const
179 {
180  assert(oids);
181  std::auto_ptr<te::da::DataSet> inputData, outputDataSet;
182 
183  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
184 
185  inputData = ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
186  outputDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
187 
188  return outputDataSet;
189 }
190 
192 {
193  if(m_datasourceId.empty() || m_datasetName.empty())
194  return false;
195 
196  if(m_converter.get() == 0)
197  return false;
198 
200  try
201  {
202  ds = te::da::GetDataSource(m_datasourceId, true);
203  }
204  catch(...)
205  {
206  return false;
207  }
208 
209  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
210  return false;
211 
212  return true;
213 }
214 
215 void te::map::DataSetAdapterLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid)
216 {
217  if(m_rendererType.empty())
218  throw Exception((boost::format(TR_MAP("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
219 
220  std::auto_ptr<te::da::DataSetType> dsType = getSchema();
221 
222  if(!dsType->hasGeom())
223  return;
224 
225  // Try get the defined renderer
226  std::auto_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
227  if(renderer.get() == 0)
228  throw Exception((boost::format(TR_MAP("Could not draw the data set layer %1%. The renderer %2% could not be created!")) % getTitle() % m_rendererType).str());
229 
230  renderer->draw(this, canvas, bbox, srid);
231 }
232 
234 {
235  return m_converter.get();
236 }
237 
238 void te::map::DataSetAdapterLayer::setConverter(std::auto_ptr<te::da::DataSetTypeConverter> converter)
239 {
240  m_converter = converter;
241 
242  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
243 
244  std::auto_ptr<te::da::DataSetType> dsType = ds->getDataSetType(m_datasetName);
245 
246  if(!dsType->hasGeom() && m_converter->getResult()->hasGeom())
247  {
248  m_rtree.clear();
249 
250  // Gets all data
251  std::auto_ptr<te::da::DataSet> inputData = ds->getDataSet(m_datasetName);
252 
253  // Creates the data set adapter
254  std::auto_ptr<te::da::DataSet> adaptedDataSet;
255  adaptedDataSet.reset(te::da::CreateAdapter(inputData.release(), m_converter.get(), true));
256 
257  std::size_t geomPropPos = te::da::GetFirstSpatialPropertyPos(adaptedDataSet.get());
258 
259  std::size_t pos = 0;
260 
261  while(adaptedDataSet->moveNext())
262  {
263  std::auto_ptr<te::gm::Geometry> geom(adaptedDataSet->getGeometry(geomPropPos));
264  assert(geom.get());
265 
266  te::gm::Envelope env(*geom->getMBR());
267 
268  m_rtree.insert(env, pos);
269 
270  ++pos;
271  }
272  }
273 }
274 
275 const std::string& te::map::DataSetAdapterLayer::getType() const
276 {
277  return sm_type;
278 }
279 
281 {
282  return m_datasetName;
283 }
284 
285 void te::map::DataSetAdapterLayer::setDataSetName(const std::string& name)
286 {
287  m_datasetName = name;
288 }
289 
291 {
292  return m_datasourceId;
293 }
294 
296 {
297  m_datasourceId = id;
298 }
299 
301 {
302  return m_rendererType;
303 }
304 
306 {
307  m_rendererType = t;
308 }
DataSetAdapterLayer(AbstractLayer *parent=0)
It initializes a new layer.
void setDataSetName(const std::string &name)
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
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:409
const std::string & getDataSetName() const
const std::string & getDataSourceId() const
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:258
An converter for DataSetType.
This is the base class for layers.
Definition: AbstractLayer.h:76
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
const std::string & getType() const
It returns the layer type: DATASET_LAYER.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
static AbstractRenderer * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
AccessPolicy
Supported data access policies (can be used as bitfield).
Definition: Enums.h:40
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
An exception class for the MapTools module.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
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. ...
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
TraverseType
A dataset can be traversed in two ways:
Definition: Enums.h:53
#define TR_MAP(message)
It marks a string in order to get translated. This is a special mark used in the Map Rendering module...
An abstract factory for layer renderers.
void setDataSourceId(const std::string &id)
This is an abstract class that models a query expression.
Definition: Expression.h:47
void setRendererType(const std::string &t)
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.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
A layer with reference to a DataSetTypeConverter.
te::da::DataSetTypeConverter * getConverter() const
It returns the DataSetTypeConverter.
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
const std::string & getRendererType() const
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:591
static const std::string sm_type
A static data member used in the implementation of getType method.
This class represents a filtered data set.
void setConverter(std::auto_ptr< te::da::DataSetTypeConverter > converter)
It sets the converter that will be used by the layer.