All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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  }
164  catch(...)
165  {
166  return false;
167  }
168 
169  if(ds.get() == 0 || !ds->isValid() || !ds->isOpened())
170  return false;
171 
172  return true;
173 }
174 
175 void te::map::DataSetLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid)
176 {
177  if(m_rendererType.empty())
178  throw Exception((boost::format(TR_MAP("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
179 
180  // Try get the defined renderer
181  std::auto_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
182  if(renderer.get() == 0)
183  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());
184 
185  renderer->draw(this, canvas, bbox, srid);
186 }
187 
188 const std::string& te::map::DataSetLayer::getType() const
189 {
190  return sm_type;
191 }
192 
193 const std::string& te::map::DataSetLayer::getDataSetName() const
194 {
195  return m_datasetName;
196 }
197 
198 void te::map::DataSetLayer::setDataSetName(const std::string& name)
199 {
200  m_datasetName = name;
201 }
202 
203 const std::string& te::map::DataSetLayer::getDataSourceId() const
204 {
205  return m_datasourceId;
206 }
207 
208 void te::map::DataSetLayer::setDataSourceId(const std::string& id)
209 {
210  m_datasourceId = id;
211 }
212 
213 const std::string& te::map::DataSetLayer::getRendererType() const
214 {
215  return m_rendererType;
216 }
217 
218 void te::map::DataSetLayer::setRendererType(const std::string& t)
219 {
220  m_rendererType = t;
221 }
const std::string & getDataSetName() 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
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
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
~DataSetLayer()
Destructor.
This is the base class for layers.
Definition: AbstractLayer.h:76
A layer with reference to a dataset.
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
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
static const std::string sm_type
A static data member used in the implementation of getType method.
Definition: DataSetLayer.h:162
const std::string & getDataSourceId() const
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
DataSetLayer(AbstractLayer *parent=0)
It initializes a new layer.
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
const std::string & getRendererType() const
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1395
A basic query processor for spatial restrictions.
void setRendererType(const std::string &t)
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.
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.
This is an abstract class that models a query expression.
Definition: Expression.h:47
void setDataSourceId(const std::string &id)
void setDataSetName(const std::string &name)
std::auto_ptr< LayerSchema > getSchema() const
It returns the layer schema.
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.
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. ...
virtual std::auto_ptr< DataSet > getDataSet(const DataSourcePtr &ds, const Select &q, te::common::TraverseType travType=te::common::FORWARDONLY)
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47