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 "../common/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 "DataSetLayer.h"
38 #include "Exception.h"
39 #include "RendererFactory.h"
40 
41 // Boost
42 #include <boost/format.hpp>
43 
44 // STL
45 #include <memory>
46 
47 const std::string te::map::DataSetLayer::sm_type("DATASETLAYER");
48 
50  : AbstractLayer(parent),
51  m_schema(0)
52 {
53 }
54 
55 te::map::DataSetLayer::DataSetLayer(const std::string& id, AbstractLayer* parent)
56  : AbstractLayer(id, parent),
57  m_schema(0)
58 {
59 }
60 
62  const std::string& title,
63  AbstractLayer* parent)
64  : AbstractLayer(id, title, parent),
65  m_schema(0)
66 {
67 }
68 
70 {
71  delete m_schema;
72 }
73 
74 std::auto_ptr<te::map::LayerSchema> te::map::DataSetLayer::getSchema() const
75 {
76  assert(!m_datasetName.empty());
77 
78  if(m_schema == 0)
79  {
80  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
81 
82  std::auto_ptr<LayerSchema> type = ds->getDataSetType(m_datasetName);
83 
84  if(!type.get())
85  return std::auto_ptr<te::da::DataSetType>();
86 
87  // Cache the schema from datasource
88  m_schema = static_cast<LayerSchema*>(type->clone());
89 
90  return type;
91  }
92 
93  return std::auto_ptr<LayerSchema>(static_cast<LayerSchema*>(m_schema->clone()));
94 }
95 
96 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::common::TraverseType travType,
97  const te::common::AccessPolicy accessPolicy) const
98 {
99  assert(!m_datasetName.empty());
100 
101  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
102 
103  return ds->getDataSet(m_datasetName, travType, accessPolicy);
104 }
105 
106 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
107  const te::gm::Envelope* e,
109  te::common::TraverseType travType,
110  const te::common::AccessPolicy accessPolicy) const
111 {
112  assert(!m_datasetName.empty());
113 
114  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
115 
116  return ds->getDataSet(m_datasetName, propertyName, e, r, travType, accessPolicy);
117 }
118 
119 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
120  const te::gm::Geometry* g,
122  te::common::TraverseType travType,
123  const te::common::AccessPolicy accessPolicy) const
124 {
125  assert(!m_datasetName.empty());
126 
127  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
128 
129  return ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
130 }
131 
132 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::da::Expression* restriction,
133  te::common::TraverseType travType,
134  const te::common::AccessPolicy accessPolicy) const
135 {
136  assert(restriction);
137  assert(!m_datasetName.empty());
138 
139  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
140 
141  // Where clause
142  te::da::Where* filter = new te::da::Where(restriction);
143 
144  // All fields (?)
145  te::da::Fields* all = new te::da::Fields;
146  all->push_back(new te::da::Field("*"));
147 
148  // From the data set
149  te::da::FromItem* fromItem = new te::da::DataSetName(m_datasetName);
150  te::da::From* from = new te::da::From;
151  from->push_back(fromItem);
152 
153  // The final Select
154  std::auto_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
155 
157  return sqp.getDataSet(ds, select.get(), travType);
158 }
159 
160 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const te::da::ObjectIdSet* oids,
161  te::common::TraverseType travType,
162  const te::common::AccessPolicy accessPolicy) const
163 {
164  assert(oids);
165 
166  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
167 
168  return ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
169 }
170 
172 {
173  if(m_datasourceId.empty() || m_datasetName.empty())
174  return false;
175 
177  try
178  {
179  ds = te::da::GetDataSource(m_datasourceId, true);
180  this->getSchema().get();
181  }
182  catch(...)
183  {
184  return false;
185  }
186 
187  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
188  return false;
189 
190  return true;
191 }
192 
193 void te::map::DataSetLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid)
194 {
195  if(m_rendererType.empty())
196  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
197 
198  // Try get the defined renderer
199  std::auto_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
200  if(renderer.get() == 0)
201  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());
202 
203  renderer->draw(this, canvas, bbox, srid);
204 }
205 
206 const std::string& te::map::DataSetLayer::getType() const
207 {
208  return sm_type;
209 }
210 
211 const std::string& te::map::DataSetLayer::getDataSetName() const
212 {
213  return m_datasetName;
214 }
215 
216 void te::map::DataSetLayer::setDataSetName(const std::string& name)
217 {
218  m_datasetName = name;
219 }
220 
221 const std::string& te::map::DataSetLayer::getDataSourceId() const
222 {
223  return m_datasourceId;
224 }
225 
226 void te::map::DataSetLayer::setDataSourceId(const std::string& id)
227 {
228  m_datasourceId = id;
229 }
230 
231 const std::string& te::map::DataSetLayer::getRendererType() const
232 {
233  return m_rendererType;
234 }
235 
236 void te::map::DataSetLayer::setRendererType(const std::string& t)
237 {
238  m_rendererType = t;
239 }
240 
242 {
243  delete m_schema;
244  m_schema = 0;
245 }
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
const std::string & getDataSetName() const
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
~DataSetLayer()
Destructor.
A class that models the description of a dataset.
Definition: DataSetType.h:72
void setDataSetName(const std::string &name)
An abstract factory for layer renderers.
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. ...
SpatialRelation
Spatial relations between geometric objects.
Definition: Enums.h:122
A layer with reference to a dataset.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
A basic query processor for spatial restrictions.
This is an abstract class that models a query expression.
Definition: Expression.h:47
An exception class for the MapTools module.
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.
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
static const std::string sm_type
A static data member used in the implementation of getType method.
Definition: DataSetLayer.h:168
virtual void setOutOfDate()
Its indicate that the layer schema is out of date.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
const std::string & getDataSourceId() const
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
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
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
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
virtual std::auto_ptr< DataSet > getDataSet(const DataSourcePtr &ds, const Select &q, te::common::TraverseType travType=te::common::FORWARDONLY)
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
DataSetLayer(AbstractLayer *parent=0)
It initializes a new layer.
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
void setDataSourceId(const std::string &id)
const std::string & getRendererType() const