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 "../core/logger/Logger.h"
30 #include "../core/translator/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 
50 
52 {
53  std::unique_ptr<te::da::DataSetType> outDsType = buildOutDataSetType();
54 
56 
57  std::string geomName = geomProp->getName();
58  std::size_t geomPos = te::da::GetPropertyPos(m_converter->getResult(), geomName);
59 
60  std::unique_ptr<te::da::DataSet> inDsetSrc;
61 
62  if(m_oidSet == nullptr)
63  inDsetSrc = m_inDsrc->getDataSet(m_inDsetName);
64  else
65  inDsetSrc = m_inDsrc->getDataSet(m_inDsetName, m_oidSet);
66 
67  std::unique_ptr<te::da::DataSetAdapter> inDset(te::da::CreateAdapter(inDsetSrc.get(), m_converter.get()));
68 
69  std::unique_ptr<te::mem::DataSet> outDSet(new te::mem::DataSet(outDsType.get()));
70 
71  te::common::TaskProgress task("Processing...");
72  task.setTotalSteps((int)inDset->size());
73  task.useTimer(true);
74 
75  inDset->moveBeforeFirst();
76  while(inDset->moveNext())
77  {
78  try
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 (inDset->isNull(geomPos))
86  continue;
87 
88  if(outDsItem->getPropertyDataType(i) != te::dt::GEOMETRY_TYPE)
89  {
90  outDsItem->setValue(i, inDset->getValue(outDsItem->getPropertyName(i)).get()->clone());
91  }
92  else
93  {
94  std::unique_ptr<te::gm::Geometry> geom = inDset->getGeometry(geomPos);
95  if(!geom->isValid())
96  {
97  geomState = false;
98  continue;
99  }
100 
101  std::unique_ptr<te::gm::MultiLineString> lineResult = polygon2Line(geom.get());
102  if (!lineResult->isValid())
103  {
104  geomState = false;
105  continue;
106  }
107 
108  outDsItem->setGeometry(i, lineResult.release());
109  }
110  }
111  if (!geomState)
112  {
113  delete outDsItem;
114  continue;
115  }
116 
117  outDSet->add(outDsItem);
118 
119  if (task.isActive() == false)
120  throw te::vp::Exception(TE_TR("Operation canceled!"));
121 
122  task.pulse();
123  }
124  catch(te::common::Exception& e)
125  {
126  TE_LOG_ERROR("Vector Processing - Polygon to Line - " + e.what());
127  task.pulse();
128  continue;
129  }
130  }
131 
132  te::vp::Save(m_outDsrc.get(), outDSet.get(), outDsType.get());
133  return true;
134 }
135 
136 std::unique_ptr<te::gm::MultiLineString> te::vp::PolygonToLineMemory::polygon2Line(te::gm::Geometry* geom)
137 {
138  std::vector<te::gm::Geometry*> geomVec;
139  te::gm::Multi2Single(geom, geomVec);
140 
141  std::unique_ptr<te::gm::MultiLineString> lineResult(new te::gm::MultiLineString(0, te::gm::MultiLineStringType, geom->getSRID()));
142 
143  for (auto i : geomVec)
144  {
145  std::vector<te::gm::LineString*> lines;
146  getLines(i, lines);
147 
148  for (auto j : lines)
149  lineResult->add(j);
150  }
151 
152  return lineResult;
153 }
154 
155 void te::vp::PolygonToLineMemory::getLines(te::gm::Geometry* geom, std::vector<te::gm::LineString*>& lines)
156 {
157  if(geom == nullptr)
158  return;
159 
160  switch(geom->getGeomTypeId())
161  {
163  getLines(dynamic_cast<te::gm::GeometryCollection*>(geom), lines);
164  break;
165 
166  case te::gm::PolygonType:
167  getLines(dynamic_cast<te::gm::Polygon*>(geom), lines);
168  break;
169 
171  getLines(dynamic_cast<te::gm::LineString*>(geom), lines);
172  break;
173 
174  default:
175  return;
176  }
177 }
178 
179 void te::vp::PolygonToLineMemory::getLines(te::gm::GeometryCollection* gc, std::vector<te::gm::LineString*>& lines)
180 {
181  assert(gc);
182 
183  for(std::size_t i = 0; i < gc->getNumGeometries(); ++i)
184  getLines(gc->getGeometryN(i), lines);
185 }
186 
187 void te::vp::PolygonToLineMemory::getLines(te::gm::Polygon* p, std::vector<te::gm::LineString*>& lines)
188 {
189  assert(p);
190 
191  std::vector<te::gm::Curve*>& rings = p->getRings();
192 
193  for(std::size_t i = 0; i < rings.size(); ++i)
194  {
195  te::gm::LineString* lsCurve = dynamic_cast<te::gm::LineString*>(rings[i]);
196  te::gm::LineString* ls = new te::gm::LineString(*lsCurve);
197 
198  getLines(ls, lines);
199  }
200 }
201 
202 void te::vp::PolygonToLineMemory::getLines(te::gm::LineString* l, std::vector<te::gm::LineString*>& lines)
203 {
204  assert(l);
205  lines.push_back(l);
206 }
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
Geometric property.
std::vector< Curve * > & getRings()
It returns the polygon rings.
Definition: CurvePolygon.h:294
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
std::string getPropertyName(std::size_t pos) const
It returns the name of the pos-th property.
int getPropertyDataType(std::size_t pos) const
It returns the type of the pos-th property.
Base exception class for plugin module.
virtual const char * what() const
It outputs the exception message.
void useTimer(bool flag)
Used to define if task use progress timer information.
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
Polygon to Line Vector Processing functions.
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)
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:242
std::size_t getNumProperties() const
It returns the number of properties.
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
bool isActive() const
Verify if the task is active.
void setTotalSteps(int value)
Set the task total stepes.
te::gm::GeometryCollection * gc
std::unique_ptr< te::gm::MultiLineString > polygon2Line(te::gm::Geometry *geom)
TEVPEXPORT void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType, const bool &enableProgress=true)
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
URI C++ Library.
Definition: Attributes.h:37
std::unique_ptr< te::da::DataSetTypeConverter > m_converter
te::gm::Polygon * p
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
void getLines(te::gm::Geometry *geom, std::vector< te::gm::LineString * > &lines)
Utility functions for the data access module.
te::da::DataSourcePtr m_inDsrc
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
te::da::DataSourcePtr m_outDsrc
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...
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
#define TE_LOG_ERROR(message)
Use this tag in order to log a message to the TerraLib default logger with the ERROR level...
Definition: Logger.h:337
const te::da::ObjectIdSet * m_oidSet
It is a collection of other geometric objects.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
std::unique_ptr< te::da::DataSetType > buildOutDataSetType()
const std::string & getName() const
It returns the property name.
Definition: Property.h:127