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) 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/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 {
52 }
53 
54 te::map::DataSetLayer::DataSetLayer(const std::string& id, AbstractLayer* parent)
55  : AbstractLayer(id, parent)
56 {
57 }
58 
60  const std::string& title,
61  AbstractLayer* parent)
62  : AbstractLayer(id, title, parent)
63 {
64 }
65 
67 {
68 }
69 
70 std::auto_ptr<te::map::LayerSchema> te::map::DataSetLayer::getSchema() const
71 {
72  assert(!m_datasetName.empty());
73 
74  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
75 
76  return ds->getDataSetType(m_datasetName);
77 }
78 
79 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::common::TraverseType travType,
80  const te::common::AccessPolicy accessPolicy) const
81 {
82  assert(!m_datasetName.empty());
83 
84  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
85 
86  return ds->getDataSet(m_datasetName, travType, accessPolicy);
87 }
88 
89 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
90  const te::gm::Envelope* e,
92  te::common::TraverseType travType,
93  const te::common::AccessPolicy accessPolicy) const
94 {
95  assert(!m_datasetName.empty());
96 
97  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
98 
99  return ds->getDataSet(m_datasetName, propertyName, e, r, travType, accessPolicy);
100 }
101 
102 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const std::string& propertyName,
103  const te::gm::Geometry* g,
105  te::common::TraverseType travType,
106  const te::common::AccessPolicy accessPolicy) const
107 {
108  assert(!m_datasetName.empty());
109 
110  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
111 
112  return ds->getDataSet(m_datasetName, propertyName, g, r, travType, accessPolicy);
113 }
114 
115 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(te::da::Expression* restriction,
116  te::common::TraverseType travType,
117  const te::common::AccessPolicy accessPolicy) const
118 {
119  assert(restriction);
120  assert(!m_datasetName.empty());
121 
122  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
123 
124  // Where clause
125  te::da::Where* filter = new te::da::Where(restriction);
126 
127  // All fields (?)
128  te::da::Fields* all = new te::da::Fields;
129  all->push_back(new te::da::Field("*"));
130 
131  // From the data set
132  te::da::FromItem* fromItem = new te::da::DataSetName(m_datasetName);
133  te::da::From* from = new te::da::From;
134  from->push_back(fromItem);
135 
136  // The final Select
137  std::auto_ptr<te::da::Select> select(new te::da::Select(all, from, filter));
138 
140  return sqp.getDataSet(ds, select.get(), travType);
141 }
142 
143 std::auto_ptr<te::da::DataSet> te::map::DataSetLayer::getData(const te::da::ObjectIdSet* oids,
144  te::common::TraverseType travType,
145  const te::common::AccessPolicy accessPolicy) const
146 {
147  assert(oids);
148 
149  te::da::DataSourcePtr ds = te::da::GetDataSource(m_datasourceId, true);
150 
151  return ds->getDataSet(m_datasetName, oids, travType, accessPolicy);
152 }
153 
155 {
156  if(m_datasourceId.empty() || m_datasetName.empty())
157  return false;
158 
160  try
161  {
162  ds = te::da::GetDataSource(m_datasourceId, true);
163  this->getSchema().get();
164  }
165  catch(...)
166  {
167  return false;
168  }
169 
170  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
171  return false;
172 
173  return true;
174 }
175 
176 void te::map::DataSetLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid)
177 {
178  if(m_rendererType.empty())
179  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
180 
181  // Try get the defined renderer
182  std::auto_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
183  if(renderer.get() == 0)
184  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());
185 
186  renderer->draw(this, canvas, bbox, srid);
187 }
188 
189 const std::string& te::map::DataSetLayer::getType() const
190 {
191  return sm_type;
192 }
193 
194 const std::string& te::map::DataSetLayer::getDataSetName() const
195 {
196  return m_datasetName;
197 }
198 
199 void te::map::DataSetLayer::setDataSetName(const std::string& name)
200 {
201  m_datasetName = name;
202 }
203 
204 const std::string& te::map::DataSetLayer::getDataSourceId() const
205 {
206  return m_datasourceId;
207 }
208 
209 void te::map::DataSetLayer::setDataSourceId(const std::string& id)
210 {
211  m_datasourceId = id;
212 }
213 
214 const std::string& te::map::DataSetLayer::getRendererType() const
215 {
216  return m_rendererType;
217 }
218 
219 void te::map::DataSetLayer::setRendererType(const std::string& t)
220 {
221  m_rendererType = t;
222 }
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:259
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.
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:345
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:162
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:53
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