Volume.cpp
Go to the documentation of this file.
1 /*!
2 \file terralib/mnt/core/Volume.cpp
3 
4 \brief This file contains a class to isolines smooth.
5 
6 */
7 
8 #include "Volume.h"
9 #include "Utils.h"
10 
11 //terralib
12 #include "../../common/progress/TaskProgress.h"
13 #include "../../common/StringUtils.h"
14 #include "../../dataaccess/utils/Utils.h"
15 #include "../../dataaccess/dataset/DataSetAdapter.h"
16 #include "../../dataaccess/dataset/DataSetTypeConverter.h"
17 #include "../../geometry/GeometryProperty.h"
18 #include "../../geometry/GEOSWriter.h"
19 #include "../../raster.h"
20 #include "../../srs/SpatialReferenceSystemManager.h"
21 
22 #include <geos/geom/Point.h>
23 #include <geos/geom/Polygon.h>
24 #include <geos/geosAlgorithm.h>
25 
26 te::mnt::Volume::Volume() = default;
27 
28 te::mnt::Volume::~Volume() = default;
29 
31  std::string inrstDsetName,
32  std::unique_ptr<te::da::DataSetType> inrstDsetType,
33  te::da::DataSourcePtr invecDsrc,
34  std::string invecDsetName,
35  std::unique_ptr<te::da::DataSetType> invecDsetType,
36  const te::da::ObjectIdSet* OidSet)
37 {
38  m_inrstDsrc = inrstDsrc;
39  m_inrstDsetName = inrstDsetName;
40  m_inrstDsetType = std::move(inrstDsetType);
41 
42  m_invecDsrc = invecDsrc;
43  m_invecDsetName = invecDsetName;
44  m_invecDsetType = std::move(invecDsetType);
45 
46  m_OidSet = OidSet;
47 
48 }
49 
50 void te::mnt::Volume::setParams(double quota, std::string &attr, double dummy)
51 {
52  m_quota = quota;
53  m_attr = attr;
54  m_dummy = dummy;
55 }
56 
58 {
60  std::unique_ptr<te::da::DataSet> dsRaster = m_inrstDsrc->getDataSet(m_inrstDsetName);
61  m_raster = dsRaster->getRaster(rasterProp->getName());
62  m_raster->getBand(0)->getProperty()->m_noDataValue = m_dummy;
63 
64  std::unique_ptr<te::da::DataSet> inDsetSrc;
65 
66  if (m_OidSet == nullptr)
67  inDsetSrc = m_invecDsrc->getDataSet(m_invecDsetName);
68  else
69  inDsetSrc = m_invecDsrc->getDataSet(m_invecDsetName, m_OidSet);
70 
71  std::size_t geo_pos = te::da::GetFirstPropertyPos(inDsetSrc.get(), te::dt::GEOMETRY_TYPE);
72  te::gm::GeomType gtype = (te::da::GetFirstGeomProperty(m_invecDsetType.get()))->getGeometryType();
73 
74  inDsetSrc->moveBeforeFirst();
75 
76  int flin, llin, fcol, lcol;
77  std::vector <te::gm::Polygon *> pols;
78 
79  while (inDsetSrc->moveNext())
80  {
81  std::unique_ptr<te::gm::Geometry> gin = inDsetSrc->getGeometry(geo_pos);
82  if (!gin.get())
83  continue;
84 
85  if (gtype == te::gm::PolygonType)
86  {
87  pols.push_back(dynamic_cast<te::gm::Polygon*>(gin.get()->clone()));
88  }
89  if (gtype == te::gm::MultiPolygonType)
90  {
91  te::gm::MultiPolygon* mp = dynamic_cast<te::gm::MultiPolygon*>(gin.get());
92  if (mp)
93  {
94  for (size_t p = 0; p < dynamic_cast<te::gm::GeometryCollection*>(mp)->getNumGeometries(); p++)
95  {
96  pols.push_back(dynamic_cast<te::gm::Polygon*>(dynamic_cast<te::gm::GeometryCollection*>(mp)->getGeometryN(p)->clone()));
97  }
98  }
99  }
100 
101  for (size_t p = 0; p < pols.size(); p++)
102  if (DefLC(pols[p], flin, llin, fcol, lcol))
103  {
104  //Calculate the volumes.
105  std::string attr = inDsetSrc->getString(m_attr);
106  if (CVGrd(pols[p], flin, llin, fcol, lcol, attr))
107  {
108  m_polyvec.push_back(attr);
109  }
110  }
111  pols.clear();
112  }
113 
114  return true;
115 }
116 
117 bool te::mnt::Volume::DefLC(te::gm::Polygon *pol, int& flin, int& llin, int& fcol, int& lcol)
118 {
119  if (!pol)
120  return false;
121 
122  te::gm::Coord2D cg;
123 
124  const te::gm::Envelope *env = dynamic_cast<te::gm::Geometry*>(pol)->getMBR();
125 
126  te::gm::Point llpt(env->getLowerLeftX(), env->getLowerLeftY());
127  te::gm::Point urpt(env->getUpperRightX(), env->getUpperRightY());
128 
129  // Calculate lines and coluns intercepted
130  cg = m_raster->getGrid()->geoToGrid(llpt.getX(), llpt.getY());
131  fcol = te::rst::Round(cg.getX());
132  llin = te::rst::Round(cg.getY());
133  cg = m_raster->getGrid()->geoToGrid(urpt.getX(), urpt.getY());
134  lcol = te::rst::Round(cg.getX());
135  flin = te::rst::Round(cg.getY());
136 
137  if (((int)m_raster->getNumberOfColumns() <= fcol) || (lcol < 0) ||
138  ((int)m_raster->getNumberOfRows() <= flin) || (llin < 0))
139  return false;
140 
141  if (fcol < 0)
142  fcol = 0;
143  if (flin < 0)
144  flin = 0;
145  if ((int)m_raster->getNumberOfColumns() <= lcol)
146  lcol = (int)m_raster->getNumberOfColumns() - 1;
147  if ((int)m_raster->getNumberOfRows() <= llin)
148  llin = (int)m_raster->getNumberOfRows() - 1;
149 
150  return true;
151 }
152 
153 bool te::mnt::Volume::CVGrd(te::gm::Polygon *pol, int flin, int llin, int fcol, int lcol, std::string &polid)
154 {
155  double somazs = 0.0;
156  double nrozs = 0.0;
157  double areabase;
158  double areatotal;
159  double volcorte = 0.;
160  double volaterro = 0.;
161  double polarea = dynamic_cast<te::gm::CurvePolygon*>(pol)->getArea();
162 
163  std::string msg("Calculating Volume - Pol(");
164  msg += polid;
165  msg += ")...";
166  te::common::TaskProgress task(msg, te::common::TaskProgress::UNDEFINED, (int)(llin-flin)*(lcol-fcol));
167 
168  double rx = m_raster->getResolutionX();
169  double ry = m_raster->getResolutionY();
170 
171  areabase = rx * ry;
172 
173  if (m_srid)
174  {
177 
178  if (unitin->getId() != te::common::UOM_Metre)
179  {
180  convertAngleToPlanar(areabase, unitout, 1);
181  convertAngleToPlanar(polarea, unitout, 1);
182  }
183  }
184 
185  areatotal = areabase;
186 
187  te::gm::Coord2D cg;
188  geos::geom::Coordinate pt;
189  double value;
190  bool contr = true;
191  std::unique_ptr<geos::geom::Geometry> polGeom(te::gm::GEOSWriter::write(pol)->clone());
192  geos::algorithm::PointLocator loc;
193 
194  // Take each line and column of the bounding box.
195  for (int i = flin; i <= llin; i++)
196  {
197  for (int j = fcol; j <= lcol; j++)
198  {
199  task.pulse();
200  cg = m_raster->getGrid()->gridToGeo(j, i);
201  geos::geom::Coordinate pt1(cg.getX(), cg.getY());
202 
203  if (loc.locate(pt1, polGeom.get()) == geos::geom::Location::INTERIOR)
204  {
205  // Verify ans atualize the profile volume or embankment volume.
206  m_raster->getValue((unsigned)j, (unsigned)i, value);
207  if (value >= m_dummy)
208  {
209  contr = false;
210  i = llin + 1;
211  j = lcol + 1;
212  break;
213  }
214  else{
215  if (value < m_quota)
216  volaterro += (m_quota - value);
217  else
218  volcorte += (value - m_quota);
219  areatotal += areabase;
220  somazs += value;
221  nrozs = nrozs + (double)1.0;
222  }
223  }
224  }
225  if (!contr) break;
226  }
227 
228  polGeom.release();
229 
230  volaterro *= areabase;
231  volcorte *= areabase;
232 
233  // Show the volumes values.
234  m_cortevec.push_back(te::common::Convert2String(volcorte,2));
235  m_aterrovec.push_back(te::common::Convert2String(volaterro,2));
236  m_areavec.push_back(te::common::Convert2String(polarea,2));
237 
238  if (nrozs != 0)
239  m_iquotavec.push_back(te::common::Convert2String(somazs / nrozs,2));
240  else
242 
243  return true;
244 }
245 
246 void te::mnt::Volume::getResults(std::vector<std::string> &polyvec,
247  std::vector<std::string> &cortevec,
248  std::vector<std::string> &aterrovec,
249  std::vector<std::string> &areavec,
250  std::vector<std::string> &iquotavec)
251 {
252  polyvec = m_polyvec;
253  cortevec = m_cortevec;
254  aterrovec = m_aterrovec;
255  areavec = m_areavec;
256  iquotavec = m_iquotavec;
257 }
258 
259 
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
boost::shared_ptr< DataSource > DataSourcePtr
double m_dummy
Definition: Volume.h:71
bool DefLC(te::gm::Polygon *pol, int &flin, int &llin, int &fcol, int &lcol)
Definition: Volume.cpp:117
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
void setParams(double quota, std::string &attr, double dummy)
Definition: Volume.cpp:50
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
double getY() const
It returns the y-coordinate.
Definition: Coord2D.h:108
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
Raster property.
std::unique_ptr< te::da::DataSetType > m_inrstDsetType
Definition: Volume.h:59
te::da::DataSourcePtr m_inrstDsrc
Definition: Volume.h:57
void getResults(std::vector< std::string > &polyvec, std::vector< std::string > &cortevec, std::vector< std::string > &aterrovec, std::vector< std::string > &areavec, std::vector< std::string > &iquotavec)
Definition: Volume.cpp:246
void setInput(te::da::DataSourcePtr inrstDsrc, std::string inrstDsetName, std::unique_ptr< te::da::DataSetType > inrstDsetType, te::da::DataSourcePtr invecDsrc, std::string invecDsetName, std::unique_ptr< te::da::DataSetType > invecDsetType, const te::da::ObjectIdSet *OidSet)
Definition: Volume.cpp:30
static SpatialReferenceSystemManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
std::unique_ptr< te::rst::Raster > m_raster
Definition: Volume.h:67
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
te::da::DataSourcePtr m_invecDsrc
Definition: Volume.h:61
te::gm::Point * pt1
int m_srid
Attribute with spatial reference information.
Definition: Volume.h:73
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
te::gm::Polygon * p
CurvePolygon is a planar surface defined by 1 exterior boundary and 0 or more interior boundaries...
Definition: CurvePolygon.h:57
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
std::string m_attr
Definition: Volume.h:70
Utility functions for the data access module.
static geos::geom::Geometry * write(const Geometry *teGeom)
It reads a TerraLib geometry and make a GEOS geometry.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
bool CVGrd(te::gm::Polygon *pol, int flin, int llin, int fcol, int lcol, std::string &polid)
Definition: Volume.cpp:153
std::vector< std::string > m_polyvec
Attribute with structure to save results.
Definition: Volume.h:75
bool run()
Definition: Volume.cpp:57
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
double getX() const
It returns the x-coordinate.
Definition: Coord2D.h:102
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
std::vector< std::string > m_aterrovec
Attribute with structure to save results.
Definition: Volume.h:77
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
std::vector< std::string > m_cortevec
Attribute with structure to save results.
Definition: Volume.h:76
const te::da::ObjectIdSet * m_OidSet
Definition: Volume.h:65
double m_quota
Definition: Volume.h:69
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
This file contains a class to calculate volume. Adapted from SPRING.
std::unique_ptr< te::da::DataSetType > m_invecDsetType
Definition: Volume.h:63
std::vector< std::string > m_areavec
Attribute with structure to save results.
Definition: Volume.h:78
std::vector< std::string > m_iquotavec
Attribute with structure to save results.
Definition: Volume.h:79
std::string m_invecDsetName
Definition: Volume.h:62
TEMNTEXPORT bool convertAngleToPlanar(double &val, te::common::UnitOfMeasurePtr planar, int type)
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
std::string m_inrstDsetName
Definition: Volume.h:58