All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
24 #include "../common.h"
25 #include "../common/progress/TaskProgress.h"
26 #include "../common/Translator.h"
27 
28 #include "../dataaccess/dataset/DataSet.h"
29 #include "../dataaccess/dataset/DataSetAdapter.h"
30 #include "../dataaccess/dataset/DataSetType.h"
31 #include "../dataaccess/dataset/DataSetTypeConverter.h"
32 #include "../dataaccess/dataset/ObjectIdSet.h"
33 #include "../dataaccess/datasource/DataSource.h"
34 #include "../dataaccess/datasource/DataSourceCapabilities.h"
35 #include "../dataaccess/utils/Utils.h"
36 
37 #include "../datatype/Property.h"
38 #include "../datatype/StringProperty.h"
39 
40 #include "../geometry/GeometryProperty.h"
41 #include "../geometry/Polygon.h"
42 
43 #include "../memory/DataSetItem.h"
44 
45 #include "../raster.h"
46 #include "../raster/BandProperty.h"
47 #include "../raster/Grid.h"
48 #include "../raster/RasterFactory.h"
49 
50 #include "../rp/RasterAttributes.h"
51 
52 #include "../statistics/core/Utils.h"
53 
54 #include "Exception.h"
55 #include "VectorToRaster.h"
56 
57 
59 {
60 }
61 
63  std::string inVectorName,
64  std::auto_ptr<te::da::DataSetType> inVectorDsType)
65 {
66  m_inVectorDsrc = inVectorDsrc;
67  m_inVectorName = inVectorName;
68  m_inVectorDsType = inVectorDsType;
69 }
70 
71 void te::attributefill::VectorToRaster::setParams(std::vector<std::string> selectedAttVec,
72  double resolutionX,
73  double resolutionY,
74  int columns,
75  int rows,
76  bool setDummy,
77  int dummy)
78 {
79  m_selectedAttVec = selectedAttVec;
80  m_resolutionX = resolutionX;
81  m_resolutionY = resolutionY;
82  m_columns = columns;
83  m_rows = rows;
84  m_setDummy = setDummy;
85 
86  if(setDummy == true)
87  m_dummy = dummy;
88 }
89 
91 {
92  m_outDsrc = outDsrc;
93  m_outDset = dsName;
94 }
95 
97 {
98  if(!m_inVectorDsType.get())
99  return false;
100 
101  if(m_outDset.empty() || !m_outDsrc.get())
102  return false;
103 
104  if(m_selectedAttVec.size() == 0)
105  return false;
106 
107  if(m_resolutionX == 0 || m_resolutionY == 0)
108  return false;
109 
110  if(m_columns == 0 || m_rows == 0)
111  return false;
112 
113  return true;
114 }
115 
117 {
118  std::auto_ptr<te::da::DataSet> inDataSet = m_inVectorDsrc->getDataSet(m_inVectorName);
119  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(m_inVectorDsType.get());
120  std::size_t propPos = m_inVectorDsType->getPropertyPosition(geomProp->getName());
121 
122  te::gm::Envelope* env = inDataSet->getExtent(propPos).release();
123 
124  te::rst::Grid* grid = new te::rst::Grid(m_resolutionX, m_resolutionY, env, geomProp->getSRID());
125 
126 // create bands
127  std::vector<te::rst::BandProperty*> vecBandProp;
128 
129  for(std::size_t i = 0; i < m_selectedAttVec.size(); ++i)
130  {
131  te::dt::Property* prop = m_inVectorDsType->getProperty(m_selectedAttVec[i]);
133 
134  if(m_setDummy == true)
135  bProp->m_noDataValue = m_dummy;
136 
137  vecBandProp.push_back(bProp);
138  }
139 
140 // create raster info
141  std::map<std::string, std::string> conInfo = m_outDsrc->getConnectionInfo();
142 
143 // create raster
144  std::auto_ptr<te::rst::Raster> rst(te::rst::RasterFactory::make("GDAL", grid, vecBandProp, conInfo));
145 
146 // get vector data
147  std::string geomName = geomProp->getName();
148  std::map<te::gm::Geometry*, std::vector<double> > vectorMap;
149 
150  inDataSet->moveBeforeFirst();
151  while(inDataSet->moveNext())
152  {
153  te::gm::Geometry* geom = static_cast<te::gm::Geometry*>(inDataSet->getGeometry(geomName)->clone());
154  std::vector<double> valueVec;
155 
156  for(std::size_t b = 0; b < m_selectedAttVec.size(); ++b)
157  {
158  valueVec.push_back(inDataSet->getDouble(m_selectedAttVec[b]));
159  }
160 
161  vectorMap.insert(std::pair<te::gm::Geometry*, std::vector<double> >(geom, valueVec));
162  }
163 
164  te::common::TaskProgress task("Rasterizing...");
165  task.setTotalSteps(m_selectedAttVec.size() * vectorMap.size());
166  task.useTimer(true);
167 
168  for(std::size_t i = 0; i < m_selectedAttVec.size(); ++i)
169  {
170  std::map<te::gm::Geometry*, std::vector<double> >::iterator vectorIt = vectorMap.begin();
171 
172  while(vectorIt != vectorMap.end())
173  {
174  te::gm::Polygon* polygon = static_cast<te::gm::Polygon*>(vectorIt->first);
175 
176  if(!polygon)
177  continue;
178 
181 
182  while (it != itend)
183  {
184  rst->setValue(it.getColumn(), it.getRow(), vectorIt->second[i], i);
185  ++it;
186  }
187 
188  ++vectorIt;
189  task.pulse();
190  }
191 
192  if (task.isActive() == false)
193  throw te::attributefill::Exception(TE_TR("Operation canceled!"));
194  }
195  return true;
196 }
Geometric property.
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A raster band description.
Definition: BandProperty.h:61
void useTimer(bool flag)
Used to define if task use progress timer information.
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
An exception class for the Attribute Fill module.
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::max().
Definition: BandProperty.h:136
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
It models a property definition.
Definition: Property.h:59
bool isActive() const
Verify if the task is active.
void setTotalSteps(int value)
Set the task total stepes.
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.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
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.
Definition: Geometry.h:73
void setInput(te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, std::auto_ptr< te::da::DataSetType > inVectorDsType)
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
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to the first value of the band.
static Raster * make()
It creates and returns an empty raster with default raster driver.
unsigned int getColumn() const
Returns the current column in iterator.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
const std::string & getName() const
It returns the property name.
Definition: Property.h:127