All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  // Cache the schema from datasource
85  m_schema = static_cast<LayerSchema*>(type->clone());
86 
87  return type;
88  }
89 
90  return std::auto_ptr<LayerSchema>(static_cast<LayerSchema*>(m_schema->clone()));
91 }
92 
93 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::common::TraverseType travType,
94  const te::common::AccessPolicy accessPolicy) const
95 {
96  assert(!m_datasetName.empty());
97 
98  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
99 
100  return ds->getDataSet(m_datasetName, travType, accessPolicy);
101 }
102 
103 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
104  const te::gm::Envelope* e,
106  te::common::TraverseType travType,
107  const te::common::AccessPolicy accessPolicy) const
108 {
109  assert(!m_datasetName.empty());
110 
111  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
112 
113  return ds->getDataSet(m_datasetName, propertyName, e, r, travType, accessPolicy);
114 }
115 
116 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
117  const te::gm::Geometry* g,
119  te::common::TraverseType travType,
120  const te::common::AccessPolicy accessPolicy) const
121 {
122  assert(!m_datasetName.empty());
123 
124  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
125 
126  return ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
127 }
128 
129 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::da::Expression* restriction,
130  te::common::TraverseType travType,
131  const te::common::AccessPolicy accessPolicy) const
132 {
133  assert(restriction);
134  assert(!m_datasetName.empty());
135 
136  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
137 
138  // Where clause
139  te::da::Where* filter = new te::da::Where(restriction);
140 
141  // All fields (?)
142  te::da::Fields* all = new te::da::Fields;
143  all->push_back(new te::da::Field("*"));
144 
145  // From the data set
146  te::da::FromItem* fromItem = new te::da::DataSetName(m_datasetName);
147  te::da::From* from = new te::da::From;
148  from->push_back(fromItem);
149 
150  // The final Select
151  std::auto_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
152 
154  return sqp.getDataSet(ds, select.get(), travType);
155 }
156 
157 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const te::da::ObjectIdSet* oids,
158  te::common::TraverseType travType,
159  const te::common::AccessPolicy accessPolicy) const
160 {
161  assert(oids);
162 
163  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
164 
165  return ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
166 }
167 
169 {
170  if(m_datasourceId.empty() || m_datasetName.empty())
171  return false;
172 
174  try
175  {
176  ds = te::da::GetDataSource(m_datasourceId, true);
177  this->getSchema().get();
178  }
179  catch(...)
180  {
181  return false;
182  }
183 
184  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
185  return false;
186 
187  return true;
188 }
189 
190 void te::map::DataSetLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid)
191 {
192  if(m_rendererType.empty())
193  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
194 
195  // Try get the defined renderer
196  std::auto_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
197  if(renderer.get() == 0)
198  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());
199 
200  renderer->draw(this, canvas, bbox, srid);
201 }
202 
203 const std::string& te::map::DataSetLayer::getType() const
204 {
205  return sm_type;
206 }
207 
208 const std::string& te::map::DataSetLayer::getDataSetName() const
209 {
210  return m_datasetName;
211 }
212 
213 void te::map::DataSetLayer::setDataSetName(const std::string& name)
214 {
215  m_datasetName = name;
216 }
217 
218 const std::string& te::map::DataSetLayer::getDataSourceId() const
219 {
220  return m_datasourceId;
221 }
222 
223 void te::map::DataSetLayer::setDataSourceId(const std::string& id)
224 {
225  m_datasourceId = id;
226 }
227 
228 const std::string& te::map::DataSetLayer::getRendererType() const
229 {
230  return m_rendererType;
231 }
232 
233 void te::map::DataSetLayer::setRendererType(const std::string& t)
234 {
235  m_rendererType = t;
236 }
237 
239 {
240  delete m_schema;
241  m_schema = 0;
242 }
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:347
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