RasterLayer.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/RasterLayer.cpp
22 
23  \brief A layer with reference to a raster.
24 */
25 
26 // TerraLib
27 #include "../core/translator/Translator.h"
28 #include "../dataaccess/dataset/DataSetType.h"
29 #include "../raster/Band.h"
30 #include "../raster/BandProperty.h"
31 #include "../raster/Grid.h"
32 #include "../raster/Raster.h"
33 #include "../raster/RasterFactory.h"
34 #include "Exception.h"
35 #include "RasterLayer.h"
36 #include "RendererFactory.h"
37 #include "Utils.h"
38 
39 // Boost
40 #include <boost/filesystem.hpp>
41 #include <boost/format.hpp>
42 
43 const std::string te::map::RasterLayer::sm_type("RASTERLAYER");
44 
46  : AbstractLayer(parent)
47 {
48 }
49 
50 te::map::RasterLayer::RasterLayer(const std::string& id, AbstractLayer* parent)
51  : AbstractLayer(id, parent)
52 {
53 }
54 
55 te::map::RasterLayer::RasterLayer(const std::string& id,
56  const std::string& title,
57  AbstractLayer* parent)
58  : AbstractLayer(id, title, parent)
59 {
60 }
61 
63 {
64  m_rinfo.clear();
65 }
66 
68 {
70 
71  te::map::CopyAbstractLayerInfo(this, layer);
72 
74 
75  layer->setRasterInfo(m_rinfo);
76 
77  return layer;
78 }
79 
80 std::unique_ptr<te::map::LayerSchema> te::map::RasterLayer::getSchema() const
81 {
82  LayerSchema* ls = nullptr;
83 
85 
86  if(r)
87  {
88  te::rst::Grid* grid = new te::rst::Grid(*r->getGrid());
89 
90  std::vector<te::rst::BandProperty*> bprops;
91 
92  for(size_t t = 0; t < r->getNumberOfBands(); ++t)
93  {
95 
96  bprops.push_back(bp);
97  }
98 
100 
101  std::string path;
102 
103  std::map<std::string, std::string>::const_iterator it = m_rinfo.find("URI");
104 
105  if(it != m_rinfo.end())
106  {
107  path = it->second;
108  }
109 
110  boost::filesystem::path rpath (path);
111 
112  ls = new te::da::DataSetType(rpath.filename().string());
113 
114  ls->add(rp);
115  }
116 
117  delete r;
118 
119  return std::unique_ptr<LayerSchema>(ls);
120 }
121 
122 std::unique_ptr<te::da::DataSet> te::map::RasterLayer::getData(
123  te::common::TraverseType /*travType*/, const te::common::AccessPolicy) const
124 {
125  return std::unique_ptr<te::da::DataSet>();
126 }
127 
128 std::unique_ptr<te::da::DataSet> te::map::RasterLayer::getData(const std::string& /*propertyName*/,
129  const te::gm::Envelope* /*e*/,
131  te::common::TraverseType /*travType*/,
132  const te::common::AccessPolicy) const
133 {
134  return std::unique_ptr<te::da::DataSet>();
135 }
136 
137 std::unique_ptr<te::da::DataSet> te::map::RasterLayer::getData(const std::string& /*propertyName*/,
138  const te::gm::Geometry* /*g*/,
140  te::common::TraverseType /*travType*/,
141  const te::common::AccessPolicy) const
142 {
143  return std::unique_ptr<te::da::DataSet>();
144 }
145 
146 std::unique_ptr<te::da::DataSet> te::map::RasterLayer::getData(te::da::Expression* /*restriction*/,
147  te::common::TraverseType /*travType*/,
148  const te::common::AccessPolicy) const
149 {
150  return std::unique_ptr<te::da::DataSet>();
151 }
152 
153 std::unique_ptr<te::da::DataSet> te::map::RasterLayer::getData(const te::da::ObjectIdSet* /*oids*/,
154  te::common::TraverseType /*travType*/,
155  const te::common::AccessPolicy) const
156 {
157  return std::unique_ptr<te::da::DataSet>();
158 }
159 
160 
162 {
163  return true;
164 }
165 
166 void te::map::RasterLayer::draw(Canvas* canvas, const te::gm::Envelope& bbox, int srid, const double& scale, bool* cancel)
167 {
168  if(m_rendererType.empty())
169  throw Exception((boost::format(TE_TR("Could not draw the data set layer %1%. The renderer type is empty!")) % getTitle()).str());
170 
171  // Try get the defined renderer
172  std::unique_ptr<AbstractRenderer> renderer(RendererFactory::make(m_rendererType));
173  if(renderer.get() == nullptr)
174  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());
175 
176  renderer->draw(this, canvas, bbox, srid, scale, cancel);
177 }
178 
179 const std::string& te::map::RasterLayer::getType() const
180 {
181  return sm_type;
182 }
183 
184 const std::string& te::map::RasterLayer::getRendererType() const
185 {
186  return m_rendererType;
187 }
188 
189 void te::map::RasterLayer::setRendererType(const std::string& t)
190 {
191  m_rendererType = t;
192 }
193 
195 {
197 
198  return r;
199 }
200 
201 void te::map::RasterLayer::setRasterInfo(const std::map<std::string, std::string>& rinfo)
202 {
203  m_rinfo = rinfo;
204 
206 
207  if(r)
208  {
209  setSRID(r->getSRID());
210  setExtent(*r->getExtent());
211  }
212 
213  delete r;
214 }
215 
216 std::map<std::string, std::string> te::map::RasterLayer::getRasterInfo() const
217 {
218  return m_rinfo;
219 }
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
A layer with reference to a raster.
~RasterLayer()
Destructor.
Definition: RasterLayer.cpp:62
TEMAPEXPORT void CopyAbstractLayerInfo(const te::map::AbstractLayer *refLayer, te::map::AbstractLayer *layer)
Make a copy of refLayer abstract attributes to layer. Creating new id.
std::unique_ptr< LayerSchema > getSchema() const
It returns the layer schema.
Definition: RasterLayer.cpp:80
An exception class for the MapTools module.
This is the base class for layers.
Definition: AbstractLayer.h:77
void setRasterInfo(const std::map< std::string, std::string > &rinfo)
virtual const std::string & getTitle() const
It returns the layer title.
A raster band description.
Definition: BandProperty.h:61
Base exception class for plugin module.
A class that models the description of a dataset.
Definition: DataSetType.h:72
An abstract factory for layer renderers.
te::rst::Raster * getRaster() const
SpatialRelation
Spatial relations between geometric objects.
std::map< std::string, std::string > getRasterInfo() const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
Raster property.
RasterLayer(AbstractLayer *parent=0)
It initializes a new layer.
Definition: RasterLayer.cpp:45
This is an abstract class that models a query expression.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
A layer with reference to a raster.
Definition: RasterLayer.h:52
void draw(Canvas *canvas, const te::gm::Envelope &bbox, int srid, const double &scale, bool *cancel)
It draws the layer geographic objects in the given canvas using the informed SRS. ...
An Envelope defines a 2D rectangular region.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
An abstract class for raster data strucutures.
bool isValid() const
It returns true if the layer can be used for instance to draw, otherwise, it returns false...
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
virtual void setExtent(const te::gm::Envelope &mbr)
It sets the Layer extent (or minimum bounding box).
AbstractLayer * clone()
It returns a clone of the object.
Definition: RasterLayer.cpp:67
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
std::map< std::string, std::string > m_rinfo
A raster connection info.
Definition: RasterLayer.h:161
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
static AbstractRenderer * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
void add(Constraint *c)
It adds a new constraint.
A canvas is an abstraction of a drawing area.
int getSRID() const
Returns the raster spatial reference system identifier.
virtual void setSRID(int srid)
It sets the Spatial Reference System ID associated to the Layer.
std::unique_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.
static const std::string sm_type
A static data member used in the implementation of getType method.
Definition: RasterLayer.h:162
std::string m_rendererType
A pointer to the internal renderer used to paint this layer.
Definition: RasterLayer.h:160
const std::string & getType() const
It returns the layer type: RASTER_LAYER.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
Utility functions for MapTools module.
void setRendererType(const std::string &t)
const std::string & getRendererType() const
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.