All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/utils/Utils.h"
34 
35 #include "../geometry/GeometryProperty.h"
36 
37 #include "../memory/DataSet.h"
38 #include "../memory/DataSetItem.h"
39 
40 #include "LineToPolygonMemory.h"
41 #include "Utils.h"
42 
43 // STL
44 #include <iostream>
45 #include <string>
46 
48 {}
49 
51 {}
52 
53 bool te::vp::LineToPolygonMemory::run() throw (te::common::Exception)
54 {
55  std::auto_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
56 
57  std::auto_ptr<te::da::DataSetType> inDsType = m_inDsrc->getDataSetType(m_inDsetName);
58  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(inDsType.get());
59  std::string geomName = geomProp->getName();
60 
61  std::auto_ptr<te::da::DataSet> inDset;
62 
63  if(m_oidSet == 0)
64  inDset = m_inDsrc->getDataSet(m_inDsetName);
65  else
66  inDset = m_inDsrc->getDataSet(m_inDsetName, m_oidSet);
67 
68  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(outDsType.get()));
69 
70  te::common::TaskProgress task("Processing...");
71  task.setTotalSteps(inDset->size());
72  task.useTimer(true);
73 
74  inDset->moveBeforeFirst();
75  while(inDset->moveNext())
76  {
77  te::mem::DataSetItem* outDsItem = new te::mem::DataSetItem(outDSet.get());
78  bool geomState = true;
79 
80  for(size_t i = 0; i < outDsItem->getNumProperties(); ++i)
81  {
82  if(outDsItem->getPropertyDataType(i) != te::dt::GEOMETRY_TYPE)
83  {
84  outDsItem->setValue(i, inDset->getValue(outDsItem->getPropertyName(i)).get()->clone());
85  }
86  else
87  {
88  std::auto_ptr<te::gm::Geometry> geom = inDset->getGeometry(geomName);
89  if(!geom->isValid())
90  {
91  geomState = false;
92  continue;
93  }
94 
95  std::auto_ptr<te::gm::MultiPolygon> polygonResult = line2Polygon(geom.get());
96  if(!polygonResult->isValid() || polygonResult->isEmpty())
97  geomState = false;
98 
99  outDsItem->setGeometry(i, polygonResult.release());
100  }
101  }
102  if(!geomState)
103  continue;
104 
105  outDSet->add(outDsItem);
106 
107  if (task.isActive() == false)
108  throw te::vp::Exception(TE_TR("Operation canceled!"));
109 
110  task.pulse();
111  }
112 
113  te::vp::Save(m_outDsrc.get(), outDSet.get(), outDsType.get());
114  return true;
115 }
116 
117 std::auto_ptr<te::gm::MultiPolygon> te::vp::LineToPolygonMemory::line2Polygon(te::gm::Geometry* geom)
118 {
119  std::vector<te::gm::Polygon*> polygons;
120  std::auto_ptr<te::gm::MultiPolygon> polygonResult(new te::gm::MultiPolygon(0, te::gm::MultiPolygonType, geom->getSRID()));
121 
122  getPolygons(geom, polygons);
123 
124  if(polygons.size() > 1)
125  {
126  for(std::size_t i = 0; i < polygons.size(); ++i)
127  polygonResult->add(polygons[i]);
128  }
129  else
130  {
131  polygonResult->add(polygons[0]);
132  }
133 
134  return polygonResult;
135 }
136 
137 void te::vp::LineToPolygonMemory::getPolygons(te::gm::Geometry* geom, std::vector<te::gm::Polygon*>& polygons)
138 {
139  if(geom == 0)
140  return;
141 
142  switch(geom->getGeomTypeId())
143  {
145  getPolygons(dynamic_cast<te::gm::GeometryCollection*>(geom), polygons);
146  break;
147 
149  getPolygons(dynamic_cast<te::gm::LineString*>(geom), polygons);
150  break;
151 
152  default:
153  break;
154  }
155 }
156 
157 void te::vp::LineToPolygonMemory::getPolygons(te::gm::GeometryCollection* gc, std::vector<te::gm::Polygon*>& polygons)
158 {
159  assert(gc);
160 
161  for(std::size_t i = 0; i < gc->getNumGeometries(); ++i)
162  getPolygons(gc->getGeometryN(i), polygons);
163 }
164 
165 void te::vp::LineToPolygonMemory::getPolygons(te::gm::LineString* l, std::vector<te::gm::Polygon*>& polygons)
166 {
167  assert(l);
168 
169  if(l->isClosed())
170  {
172 
174 
175  for(std::size_t i = 0; i < l->getNPoints(); ++i)
176  ring->setPoint(i, l->getX(i), l->getY(i));
177 
178  p->add(ring);
179 
180  getPolygons(p, polygons);
181  }
182 }
183 
184 void te::vp::LineToPolygonMemory::getPolygons(te::gm::Polygon* p, std::vector<te::gm::Polygon*>& polygons)
185 {
186  assert(p);
187  polygons.push_back(p);
188 }
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:213
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
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:347
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
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.
const std::string & getName() const
It returns the property name.
Definition: Property.h:127