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