src/terralib/attributefill/VectorToRaster.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 VectorToRaster.cpp
22  */
23 // Terralib
24 
25 #include "../common.h"
26 #include "../common/progress/TaskProgress.h"
27 #include "../core/translator/Translator.h"
28 
29 #include "../dataaccess/dataset/DataSet.h"
30 #include "../dataaccess/dataset/DataSetAdapter.h"
31 #include "../dataaccess/dataset/DataSetType.h"
32 #include "../dataaccess/dataset/DataSetTypeConverter.h"
33 #include "../dataaccess/dataset/ObjectIdSet.h"
34 #include "../dataaccess/datasource/DataSource.h"
35 #include "../dataaccess/datasource/DataSourceCapabilities.h"
36 #include "../dataaccess/utils/Utils.h"
37 
38 #include "../datatype/Property.h"
39 #include "../datatype/StringProperty.h"
40 
41 #include "../geometry/GeometryProperty.h"
42 #include "../geometry/Line.h"
43 #include "../geometry/LineString.h"
44 #include "../geometry/Polygon.h"
45 #include "../geometry/Point.h"
46 #include "../geometry/Utils.h"
47 
48 #include "../memory/DataSetItem.h"
49 
50 #include "../raster.h"
51 #include "../raster/BandProperty.h"
52 #include "../raster/Grid.h"
53 #include "../raster/RasterFactory.h"
54 
55 #include "../rp/RasterAttributes.h"
56 
57 #include "../statistics/core/Utils.h"
58 
59 #include "Exception.h"
60 #include "VectorToRaster.h"
61 
62 // BOOST
63 #include <boost/lexical_cast.hpp>
64 
66 
68  std::string inVectorName,
69  std::unique_ptr<te::da::DataSetTypeConverter> inVectorDsType)
70 {
71  m_inVectorDsrc = inVectorDsrc;
72  m_inVectorName = inVectorName;
73  m_inVectorDsType = std::move(inVectorDsType);
74 }
75 
76 void te::attributefill::VectorToRaster::setParams(std::vector<std::string> selectedAttVec,
77  double resolutionX,
78  double resolutionY,
79  int columns,
80  int rows,
81  bool setDummy,
82  int dummy)
83 {
84  m_selectedAttVec = selectedAttVec;
85  m_resolutionX = resolutionX;
86  m_resolutionY = resolutionY;
87  m_columns = columns;
88  m_rows = rows;
89  m_setDummy = setDummy;
90 
91  if(setDummy == true)
92  m_dummy = dummy;
93 }
94 
96 {
97  m_outDsrc = outDsrc;
98  m_outDset = dsName;
99 }
100 
102 {
103  if(!m_inVectorDsType.get())
104  return false;
105 
106  if(m_outDset.empty() || !m_outDsrc.get())
107  return false;
108 
109  if(m_selectedAttVec.size() == 0)
110  return false;
111 
112  if(m_resolutionX == 0 || m_resolutionY == 0)
113  return false;
114 
115  if(m_columns == 0 || m_rows == 0)
116  return false;
117 
118  return true;
119 }
120 
122 {
123  std::unique_ptr<te::da::DataSet> inDataSet = m_inVectorDsrc->getDataSet(m_inVectorName);
125  std::size_t propPos = boost::lexical_cast<std::size_t>(m_inVectorDsType->getResult()->getPropertyPosition(geomProp->getName()));
126 
127  te::gm::Envelope* env = inDataSet->getExtent(propPos).release();
128 
129  te::rst::Grid* grid = new te::rst::Grid(m_resolutionX, m_resolutionY, env, geomProp->getSRID());
130 
131 // create bands
132  std::vector<te::rst::BandProperty*> vecBandProp;
133 
134  for(std::size_t i = 0; i < m_selectedAttVec.size(); ++i)
135  {
137 
138  if(m_setDummy == true)
139  bProp->m_noDataValue = m_dummy;
140 
141  vecBandProp.push_back(bProp);
142  }
143 
144 // create raster info
145  std::map<std::string, std::string> conInfo;
146  conInfo["URI"] = m_outDsrc->getConnectionInfo().host() + m_outDsrc->getConnectionInfo().path();
147 
148 // create raster
149  std::unique_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make("GDAL", grid, vecBandProp, conInfo));
150 
151  te::rst::FillRaster(rst.get(), rst->getBand(0)->getProperty()->m_noDataValue);
152 
153 // get vector data
154  std::string geomName = geomProp->getName();
155  std::map<te::gm::Geometry*, std::vector<double> > vectorMap;
156 
157  inDataSet->moveBeforeFirst();
158  while(inDataSet->moveNext())
159  {
160  te::gm::Geometry* geom = static_cast<te::gm::Geometry*>(inDataSet->getGeometry(geomName)->clone());
161  std::vector<double> valueVec;
162 
163  for(std::size_t b = 0; b < m_selectedAttVec.size(); ++b)
164  {
165  valueVec.push_back(inDataSet->getDouble(m_selectedAttVec[b]));
166  }
167 
168  vectorMap.insert(std::pair<te::gm::Geometry*, std::vector<double> >(geom, valueVec));
169  }
170 
171  te::common::TaskProgress task("Rasterizing...");
172  task.setTotalSteps((int)(m_selectedAttVec.size() * vectorMap.size()));
173  task.useTimer(true);
174 
175  for(std::size_t i = 0; i < m_selectedAttVec.size(); ++i)
176  {
177  std::map<te::gm::Geometry*, std::vector<double> >::iterator vectorIt = vectorMap.begin();
178 
179  while(vectorIt != vectorMap.end())
180  {
181  switch (vectorIt->first->getGeomTypeId())
182  {
183  case te::gm::PolygonType:
185  {
186  std::vector<te::gm::Geometry*> geomVec;
187 
188  if (vectorIt->first->getGeomTypeId() == te::gm::MultiPolygonType)
189  te::gm::Multi2Single(vectorIt->first, geomVec);
190  else
191  geomVec.push_back(vectorIt->first);
192 
193  for (std::size_t g = 0; g < geomVec.size(); ++g)
194  {
195  te::gm::Polygon* polygon = dynamic_cast<te::gm::Polygon*>(geomVec[g]);
196 
197  if (!polygon)
198  continue;
199 
200  polygon->setSRID(geomProp->getSRID());
201 
204 
205  while (it != itend)
206  {
207  rst->setValue(it.getColumn(), it.getRow(), vectorIt->second[i], i);
208  ++it;
209  }
210  }
211  break;
212  }
215  {
216  std::vector<te::gm::Geometry*> geomVec;
217 
218  if (vectorIt->first->getGeomTypeId() == te::gm::MultiLineStringType)
219  te::gm::Multi2Single(vectorIt->first, geomVec);
220  else
221  geomVec.push_back(vectorIt->first);
222 
223 
224  for (std::size_t g = 0; g < geomVec.size(); ++g)
225  {
226  te::gm::LineString* lineString = static_cast<te::gm::LineString*>(geomVec[g]);
227 
228  if (!lineString)
229  continue;
230 
231  std::size_t nPoints = lineString->getNPoints();
232 
233  for (std::size_t n = 0; n < nPoints - 1; ++n)
234  {
235  std::unique_ptr<te::gm::Line> line(new te::gm::Line(*lineString->getPointN(n),
236  *lineString->getPointN(n + 1),
238  geomProp->getSRID()));
239 
242 
243  while (it != itend)
244  {
245  rst->setValue(it.getColumn(), it.getRow(), vectorIt->second[i], i);
246  ++it;
247  }
248  }
249  }
250 
251  break;
252  }
253  case te::gm::PointType:
255  {
256  std::vector<te::gm::Geometry*> geomVec;
257 
258  if (vectorIt->first->getGeomTypeId() == te::gm::MultiPointType)
259  te::gm::Multi2Single(vectorIt->first, geomVec);
260  else
261  geomVec.push_back(vectorIt->first);
262 
263  for (std::size_t g = 0; g < geomVec.size(); ++g)
264  {
265  std::vector<te::gm::Point*> pointVec;
266  te::gm::Point* point = static_cast<te::gm::Point*>(geomVec[g]);
267 
268  if (!point)
269  continue;
270 
271  pointVec.push_back(point);
272 
273  point->setSRID(geomProp->getSRID());
274 
277 
278  while (it != itend)
279  {
280  rst->setValue(it.getColumn(), it.getRow(), vectorIt->second[i], i);
281  ++it;
282  }
283  }
284 
285  break;
286  }
287  default:
288  break;
289  }
290 
291  ++vectorIt;
292 
293  task.pulse();
294  }
295 
296  if (task.isActive() == false)
297  throw te::attributefill::Exception(TE_TR("Operation canceled!"));
298  }
299  return true;
300 }
Geometric property.
A Line is LineString with 2 points.
Definition: Line.h:50
void setSRID(int srid) _NOEXCEPT_OP(true)
It sets the Spatial Reference System ID of the Point.
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
boost::shared_ptr< DataSource > DataSourcePtr
A raster band description.
Definition: BandProperty.h:61
void useTimer(bool flag)
Used to define if task use progress timer information.
unsigned int getColumn() const
Returns the current column in iterator.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
std::unique_ptr< Point > getPointN(std::size_t i) const
It returns the specified point in this LineString.
static PointSetIterator end(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to after the end of the iterator.
TERASTEREXPORT void FillRaster(te::rst::Raster *rin, const std::complex< double > &value)
Fill a Raster with provided value (all bands).
unsigned int getRow() const
Returns the current row in iterator.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
bool isActive() const
Verify if the task is active.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
void setTotalSteps(int value)
Set the task total stepes.
unsigned int line
int b
Definition: TsRtree.cpp:32
te::da::DataSourcePtr m_inVectorDsrc
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
void setInput(te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, std::unique_ptr< te::da::DataSetTypeConverter > inVectorDsType)
std::size_t getNPoints() const
It returns the number of points (vertexes) in the linestring.
Definition: LineString.h:193
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
Vector to Raster processing.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
std::vector< std::string > m_selectedAttVec
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Returns an iterator referring to the first value of the band.
void setParams(std::vector< std::string > selectedAttVec, double resolutionX, double resolutionY, int columns, int rows, bool useDummy, int dummy=0)
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
te::da::DataSourcePtr m_outDsrc
unsigned int getColumn() const
Returns the current column in iterator.
static Raster * make()
It creates and returns an empty raster with default raster driver.
static LineIterator end(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to after the end of the iterator.
unsigned int getRow() const
Returns the current row in iterator.
unsigned int getColumn() const
Returns the current column in iterator.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
unsigned int getRow() const
Returns the current row in iterator.
An exception class for the Attribute Fill module.
static PointSetIterator begin(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to the first value of the band.
std::unique_ptr< te::da::DataSetTypeConverter > m_inVectorDsType
static LineIterator begin(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to the first value of the band.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127