PolygonToLineMemory.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 PolygonToLineMemory.h
22 
23  \brief Polygon to Line 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 "PolygonToLineMemory.h"
42 #include "Utils.h"
43 
44 // STL
45 #include <string>
46 
48 {}
49 
51 {}
52 
53 bool te::vp::PolygonToLineMemory::run() throw(te::common::Exception)
54 {
55  std::auto_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
56 
57  te::gm::GeometryProperty* geomProp = te::da::GetFirstGeomProperty(m_converter->getResult());
58 
59  std::string geomName = geomProp->getName();
60  std::size_t geomPos = te::da::GetPropertyPos(m_converter->getResult(), geomName);
61 
62  std::auto_ptr<te::da::DataSet> inDsetSrc;
63 
64  if(m_oidSet == 0)
65  inDsetSrc = m_inDsrc->getDataSet(m_inDsetName);
66  else
67  inDsetSrc = m_inDsrc->getDataSet(m_inDsetName, m_oidSet);
68 
69  std::auto_ptr<te::da::DataSetAdapter> inDset(te::da::CreateAdapter(inDsetSrc.get(), m_converter.get()));
70 
71  std::auto_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(outDsType.get()));
72 
73  te::common::TaskProgress task("Processing...");
74  task.setTotalSteps((int)inDset->size());
75  task.useTimer(true);
76 
77  inDset->moveBeforeFirst();
78  while(inDset->moveNext())
79  {
80  te::mem::DataSetItem* outDsItem = new te::mem::DataSetItem(outDSet.get());
81  bool geomState = true;
82 
83  for(size_t i = 0; i < outDsItem->getNumProperties(); ++i)
84  {
85  if(outDsItem->getPropertyDataType(i) != te::dt::GEOMETRY_TYPE)
86  {
87  outDsItem->setValue(i, inDset->getValue(outDsItem->getPropertyName(i)).get()->clone());
88  }
89  else
90  {
91  std::auto_ptr<te::gm::Geometry> geom = inDset->getGeometry(geomPos);
92  if(!geom->isValid())
93  {
94  geomState = false;
95  continue;
96  }
97 
98  std::auto_ptr<te::gm::MultiLineString> lineResult = polygon2Line(geom.get());
99  if (!lineResult->isValid())
100  {
101  geomState = false;
102  continue;
103  }
104 
105  outDsItem->setGeometry(i, lineResult.release());
106  }
107  }
108  if (!geomState)
109  {
110  delete outDsItem;
111  continue;
112  }
113 
114  outDSet->add(outDsItem);
115 
116  if (task.isActive() == false)
117  throw te::vp::Exception(TE_TR("Operation canceled!"));
118 
119  task.pulse();
120  }
121 
122  te::vp::Save(m_outDsrc.get(), outDSet.get(), outDsType.get());
123  return true;
124 }
125 
126 std::auto_ptr<te::gm::MultiLineString> te::vp::PolygonToLineMemory::polygon2Line(te::gm::Geometry* geom)
127 {
128  std::vector<te::gm::LineString*> lines;
129  std::auto_ptr<te::gm::MultiLineString> lineResult(new te::gm::MultiLineString(0, te::gm::MultiLineStringType, geom->getSRID()));
130 
131  getLines(geom, lines);
132 
133 
134  lineResult->add(lines[0]);
135  for (size_t i = 1; i < lines.size(); ++i)
136  lineResult->Union(lines[i]);
137 
138  return lineResult;
139 }
140 
141 void te::vp::PolygonToLineMemory::getLines(te::gm::Geometry* geom, std::vector<te::gm::LineString*>& lines)
142 {
143  if(geom == 0)
144  return;
145 
146  switch(geom->getGeomTypeId())
147  {
149  getLines(dynamic_cast<te::gm::GeometryCollection*>(geom), lines);
150  break;
151 
152  case te::gm::PolygonType:
153  getLines(dynamic_cast<te::gm::Polygon*>(geom), lines);
154  break;
155 
157  getLines(dynamic_cast<te::gm::LineString*>(geom), lines);
158  break;
159 
160  default:
161  return;
162  }
163 }
164 
165 void te::vp::PolygonToLineMemory::getLines(te::gm::GeometryCollection* gc, std::vector<te::gm::LineString*>& lines)
166 {
167  assert(gc);
168 
169  for(std::size_t i = 0; i < gc->getNumGeometries(); ++i)
170  getLines(gc->getGeometryN(i), lines);
171 }
172 
173 void te::vp::PolygonToLineMemory::getLines(te::gm::Polygon* p, std::vector<te::gm::LineString*>& lines)
174 {
175  assert(p);
176 
177  std::vector<te::gm::Curve*>& rings = p->getRings();
178 
179  for(std::size_t i = 0; i < rings.size(); ++i)
180  {
181  te::gm::LineString* lsCurve = dynamic_cast<te::gm::LineString*>(rings[i]);
182  te::gm::LineString* ls = new te::gm::LineString(*lsCurve);
183 
184  getLines(ls, lines);
185  }
186 }
187 
188 void te::vp::PolygonToLineMemory::getLines(te::gm::LineString* l, std::vector<te::gm::LineString*>& lines)
189 {
190  assert(l);
191  lines.push_back(l);
192 }
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
Geometric property.
std::vector< Curve * > & getRings()
It returns the polygon rings.
Definition: CurvePolygon.h:300
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::MultiLineString > polygon2Line(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.
Polygon to Line Vector Processing functions.
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.
void setTotalSteps(int value)
Set the task total stepes.
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
URI C++ Library.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
void getLines(te::gm::Geometry *geom, std::vector< te::gm::LineString * > &lines)
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.
MultiLineString is a MultiCurve whose elements are LineStrings.
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
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