LineToPolygonMemory.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 LineToPolygonMemory.h
22 
23  \brief Line to Polygon Vector Processing functions.
24 */
25 
26 //Terralib
27 
28 #include "../common/progress/TaskProgress.h"
29 #include "../common/Logger.h"
30 #include "../common/Translator.h"
31 
32 #include "../dataaccess/dataset/DataSet.h"
33 #include "../dataaccess/dataset/DataSetAdapter.h"
34 #include "../dataaccess/utils/Utils.h"
35 
36 #include "../geometry/GeometryProperty.h"
37 
38 #include "../memory/DataSet.h"
39 #include "../memory/DataSetItem.h"
40 
41 #include "LineToPolygonMemory.h"
42 #include "Utils.h"
43 
44 // STL
45 #include <iostream>
46 #include <string>
47 
49 {}
50 
52 {}
53 
54 bool te::vp::LineToPolygonMemory::run() throw (te::common::Exception)
55 {
56  std::auto_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
57 
58  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(m_converter->getResult());
59 
60  std::string geomName = geomProp->getName();
61  std::size_t geomPos = te::da::GetPropertyPos(m_converter->getResult(), geomName);
62 
63  std::auto_ptr<te::da::DataSet> inDsetSrc;
64 
65  if(m_oidSet == 0)
66  inDsetSrc = m_inDsrc->getDataSet(m_inDsetName);
67  else
68  inDsetSrc = m_inDsrc->getDataSet(m_inDsetName, m_oidSet);
69 
70  std::auto_ptr<te::da::DataSetAdapter> inDset(te::da::CreateAdapter(inDsetSrc.get(), m_converter.get()));
71 
72  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(outDsType.get()));
73 
74  te::common::TaskProgress task("Processing...");
75  task.setTotalSteps((int)inDset->size());
76  task.useTimer(true);
77 
78  inDset->moveBeforeFirst();
79  while(inDset->moveNext())
80  {
81  te::mem::DataSetItem* outDsItem = new te::mem::DataSetItem(outDSet.get());
82  bool geomState = true;
83 
84  for(size_t i = 0; i < outDsItem->getNumProperties(); ++i)
85  {
86  if(outDsItem->getPropertyDataType(i) != te::dt::GEOMETRY_TYPE)
87  {
88  outDsItem->setValue(i, inDset->getValue(outDsItem->getPropertyName(i)).get()->clone());
89  }
90  else
91  {
92  std::auto_ptr<te::gm::Geometry> geom = inDset->getGeometry(geomPos);
93  if(!geom->isValid())
94  {
95  geomState = false;
96  continue;
97  }
98 
99  std::auto_ptr<te::gm::MultiPolygon> polygonResult = line2Polygon(geom.get());
100  if(!polygonResult->isValid() || polygonResult->isEmpty())
101  geomState = false;
102 
103  outDsItem->setGeometry(i, polygonResult.release());
104  }
105  }
106  if(!geomState)
107  continue;
108 
109  outDSet->add(outDsItem);
110 
111  if (task.isActive() == false)
112  throw te::vp::Exception(TE_TR("Operation canceled!"));
113 
114  task.pulse();
115  }
116 
117  te::vp::Save(m_outDsrc.get(), outDSet.get(), outDsType.get());
118  return true;
119 }
120 
121 std::auto_ptr<te::gm::MultiPolygon> te::vp::LineToPolygonMemory::line2Polygon(te::gm::Geometry* geom)
122 {
123  std::vector<te::gm::Polygon*> polygons;
124  std::auto_ptr<te::gm::MultiPolygon> polygonResult(new te::gm::MultiPolygon(0, te::gm::MultiPolygonType, geom->getSRID()));
125 
126  getPolygons(geom, polygons);
127 
128  if(polygons.size() > 1)
129  {
130  for(std::size_t i = 0; i < polygons.size(); ++i)
131  polygonResult->add(polygons[i]);
132  }
133  else
134  {
135  polygonResult->add(polygons[0]);
136  }
137 
138  return polygonResult;
139 }
140 
141 void te::vp::LineToPolygonMemory::getPolygons(te::gm::Geometry* geom, std::vector<te::gm::Polygon*>& polygons)
142 {
143  if(geom == 0)
144  return;
145 
146  switch(geom->getGeomTypeId())
147  {
149  getPolygons(dynamic_cast<te::gm::GeometryCollection*>(geom), polygons);
150  break;
151 
153  getPolygons(dynamic_cast<te::gm::LineString*>(geom), polygons);
154  break;
155 
156  default:
157  break;
158  }
159 }
160 
161 void te::vp::LineToPolygonMemory::getPolygons(te::gm::GeometryCollection* gc, std::vector<te::gm::Polygon*>& polygons)
162 {
163  assert(gc);
164 
165  for(std::size_t i = 0; i < gc->getNumGeometries(); ++i)
166  getPolygons(gc->getGeometryN(i), polygons);
167 }
168 
169 void te::vp::LineToPolygonMemory::getPolygons(te::gm::LineString* l, std::vector<te::gm::Polygon*>& polygons)
170 {
171  assert(l);
172 
173  if(l->isClosed())
174  {
176 
178 
179  for(std::size_t i = 0; i < l->getNPoints(); ++i)
180  ring->setPoint(i, l->getX(i), l->getY(i));
181 
182  p->add(ring);
183 
184  getPolygons(p, polygons);
185  }
186 }
187 
188 void te::vp::LineToPolygonMemory::getPolygons(te::gm::Polygon* p, std::vector<te::gm::Polygon*>& polygons)
189 {
190  assert(p);
191  polygons.push_back(p);
192 }
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
void add(Curve *ring)
It adds the ring to the curve polygon.
Definition: CurvePolygon.h:267
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
Geometric property.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
Utility functions for the data access module.
std::string getPropertyName(std::size_t pos) const
It returns the name of the pos-th property.
std::auto_ptr< te::gm::MultiPolygon > line2Polygon(te::gm::Geometry *geom)
int getPropertyDataType(std::size_t pos) const
It returns the type of the pos-th property.
void useTimer(bool flag)
Used to define if task use progress timer information.
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
Definition: Utils.cpp:172
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
Definition: Utils.cpp:500
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:346
std::size_t getNumProperties() const
It returns the number of properties.
bool isActive() const
Verify if the task is active.
bool isClosed() const
It returns true if the curve is closed (startPoint = endPoint).
Definition: LineString.cpp:259
const double & getY(std::size_t i) const
It returns the n-th y coordinate value.
Definition: LineString.cpp:391
void setTotalSteps(int value)
Set the task total stepes.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
void getPolygons(te::gm::Geometry *geom, std::vector< te::gm::Polygon * > &polygons)
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:353
const double & getX(std::size_t i) const
It returns the n-th x coordinate value.
Definition: LineString.cpp:385
URI C++ Library.
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.
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
Definition: DataSetItem.h:56
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
It is a collection of other geometric objects.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
Line to Polygon Vector Processing functions.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
Definition: Utils.cpp:644
const std::string & getName() const
It returns the property name.
Definition: Property.h:127