All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultipartToSinglepart.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 MultipartToSinglepart.h
22 
23  \brief Aggregation 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/ObjectId.h"
34 #include "../dataaccess/dataset/ObjectIdSet.h"
35 #include "../dataaccess/utils/Utils.h"
36 #include "../datatype/Property.h"
37 #include "../datatype/SimpleProperty.h"
38 #include "../datatype/StringProperty.h"
39 
40 #include "../geometry/Geometry.h"
41 #include "../geometry/GeometryProperty.h"
42 #include "../geometry/MultiLineString.h"
43 #include "../geometry/MultiPoint.h"
44 #include "../geometry/MultiPolygon.h"
45 #include "../geometry/Utils.h"
46 
47 #include "../memory/DataSet.h"
48 #include "../memory/DataSetItem.h"
49 
50 #include "MultipartToSinglepart.h"
51 #include "Config.h"
52 #include "Exception.h"
53 #include "Utils.h"
54 
55 // STL
56 #include <map>
57 #include <math.h>
58 #include <string>
59 #include <vector>
60 
61 // BOOST
62 #include <boost/lexical_cast.hpp>
63 #include <boost/algorithm/string.hpp>
64 
66 {
67 }
68 
70 {
71 }
72 
74 {
75  te::dt::Property* prop = te::da::GetFirstSpatialProperty(m_inDsType.get());
76  te::gm::GeometryProperty* geomProp = dynamic_cast<te::gm::GeometryProperty*>(prop);
77 
78  if(!te::vp::IsMultiType(geomProp->getGeometryType()))
79  {
80  throw te::common::Exception(TE_TR("This layer has not a multipart geometry!"));
81  }
82 
83  if(m_oidSet)
84  return runSelected();
85  else
86  return runAll();
87 }
88 
90 {
91  std::auto_ptr<te::da::DataSet> inDs;
92 
93  inDs = m_inDsrc->getDataSet(m_inDsName);
94 
95  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(inDs.get());
96 
97  std::auto_ptr<te::da::DataSetType> outDst = getOutDst();
98 
99  std::auto_ptr<te::mem::DataSet> outDs(new te::mem::DataSet(outDst.get()));
100 
101  inDs->moveBeforeFirst();
102 
103  std::size_t pkCount = 0;
104 
105  while(inDs->moveNext())
106  {
107  std::auto_ptr<te::gm::Geometry> geom = inDs->getGeometry(geomPos);
108 
109  std::vector<te::gm::Geometry*> geoms;
110  te::vp::Multi2Single(geom.release(), geoms);
111 
112  if(geoms.size() == 1)
113  {
114  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDs.get());
115 
116  for(std::size_t i = 0; i < inDs->getNumProperties(); ++i)
117  {
118  if(i == 0)
119  {
120  item->setInt32(0, pkCount);
121  ++pkCount;
122  item->setValue(1, inDs->getValue(i).release());
123  }
124  else if(i != geomPos)
125  item->setValue((i+1), inDs->getValue(i).release());
126  else
127  item->setGeometry((i+1), geoms[0]);
128  }
129 
130  outDs->add(item);
131  }
132  else
133  {
134  for(std::size_t g = 0; g < geoms.size(); ++g)
135  {
136  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDs.get());
137 
138  for(std::size_t i = 0; i < inDs->getNumProperties(); ++i)
139  {
140  if(i == 0)
141  {
142  item->setInt32(0, pkCount);
143  ++pkCount;
144  item->setValue(1, inDs->getValue(i).release());
145  }
146  else if(i != geomPos)
147  item->setValue((i+1), inDs->getValue(i).release());
148  else
149  item->setGeometry((i+1), geoms[g]);
150  }
151 
152  outDs->add(item);
153  }
154  }
155  }
156 
157  te::vp::Save(m_outDsrc.get(), outDs.get(), outDst.get());
158 
159  return true;
160 }
161 
163 {
164  std::auto_ptr<te::da::DataSet> inDs;
165 
166  inDs = m_inDsrc->getDataSet(m_inDsName);
167 
168  std::size_t geomPos = te::da::GetFirstSpatialPropertyPos(inDs.get());
169 
170  std::auto_ptr<te::da::DataSetType> outDst = getOutDst(true);
171 
172  std::auto_ptr<te::mem::DataSet> outDs(new te::mem::DataSet(outDst.get()));
173 
174  inDs->moveBeforeFirst();
175 
176  std::size_t pkCount = 0;
177 
178  while(inDs->moveNext())
179  {
180  std::auto_ptr<te::gm::Geometry> geom = inDs->getGeometry(geomPos);
181 
182  te::da::ObjectId* geomOid = te::da::GenerateOID(inDs.get(), m_oidSet->getPropertyNames());
183 
184  std::vector<te::gm::Geometry*> geoms;
185  std::vector<te::gm::Geometry*> multiGeoms;
186 
187  if(m_oidSet->contains(geomOid))
188  {
189  te::vp::Multi2Single(geom.release(), geoms);
190 
191  for(std::size_t i = 0; i < geoms.size(); ++i)
192  {
193  multiGeoms.push_back(single2multi(geoms[i]));
194  }
195  }
196  else
197  {
198  geoms.push_back(geom.release());
199  }
200 
201  if(geoms.size() == 1)
202  {
203  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDs.get());
204 
205  for(std::size_t i = 0; i < inDs->getNumProperties(); ++i)
206  {
207  if(i == 0)
208  {
209  item->setInt32(0, pkCount);
210  ++pkCount;
211  item->setValue(1, inDs->getValue(i).release());
212  }
213  else if(i != geomPos)
214  item->setValue((i+1), inDs->getValue(i).release());
215  else
216  item->setGeometry((i+1), geoms[0]);
217  }
218 
219  outDs->add(item);
220  }
221  else
222  {
223  for(std::size_t g = 0; g < multiGeoms.size(); ++g)
224  {
225  te::mem::DataSetItem* item = new te::mem::DataSetItem(outDs.get());
226 
227  for(std::size_t i = 0; i < inDs->getNumProperties(); ++i)
228  {
229  if(i == 0)
230  {
231  item->setInt32(0, pkCount);
232  ++pkCount;
233  item->setValue(1, inDs->getValue(i).release());
234  }
235  else if(i != geomPos)
236  item->setValue((i+1), inDs->getValue(i).release());
237  else
238  item->setGeometry((i+1), multiGeoms[g]);
239  }
240 
241  outDs->add(item);
242  }
243  }
244  }
245 
246  te::vp::Save(m_outDsrc.get(), outDs.get(), outDst.get());
247  return true;
248 }
249 
251  std::string inDsName,
252  std::auto_ptr<te::da::DataSetType> inDsType,
253  const te::da::ObjectIdSet* oidSet)
254 {
255  m_inDsrc = inDsrc;
256  m_inDsName = inDsName;
257  m_inDsType = inDsType;
258  m_oidSet = oidSet;
259 }
260 
262 {
263  m_outDsrc = outDsrc;
264  m_outDsName = outDsName;
265 }
266 
267 std::auto_ptr<te::da::DataSetType> te::vp::MultipartToSinglepart::getOutDst(bool onlySelected)
268 {
269  std::auto_ptr<te::da::DataSetType> outDst(new te::da::DataSetType(m_outDsName));
270 
271  te::dt::Property* newId = new te::dt::SimpleProperty("newid", te::dt::INT32_TYPE, true);
272 
273  outDst->add(newId);
274 
275  for(std::size_t i = 0; i < m_inDsType->size(); ++i)
276  {
277  te::dt::Property* oldP = m_inDsType->getProperty(i);
278  te::dt::Property* newP = oldP->clone();
279 
280  if(!onlySelected)
281  {
282  if(newP->getType() == te::dt::GEOMETRY_TYPE)
283  {
284  te::gm::GeometryProperty* geomP = dynamic_cast<te::gm::GeometryProperty*>(newP);
285 
287  }
288  }
289 
290  outDst->add(newP);
291  }
292 
293  return outDst;
294 }
295 
297 {
298  switch(geom->getGeomTypeId())
299  {
301  {
303  g->add(geom);
304  g->setSRID(geom->getSRID());
305  return g;
306  }
308  {
310  g->add(geom);
311  g->setSRID(geom->getSRID());
312  return g;
313  }
315  {
317  g->add(geom);
318  g->setSRID(geom->getSRID());
319  return g;
320  }
322  {
324  g->add(geom);
325  g->setSRID(geom->getSRID());
326  return g;
327  }
328  case te::gm::PointType:
329  {
331  g->add(geom);
332  g->setSRID(geom->getSRID());
333  return g;
334  }
335  case te::gm::PointMType:
336  {
338  g->add(geom);
339  g->setSRID(geom->getSRID());
340  return g;
341  }
342  case te::gm::PointZType:
343  {
345  g->add(geom);
346  g->setSRID(geom->getSRID());
347  return g;
348  }
349  case te::gm::PointZMType:
350  {
352  g->add(geom);
353  g->setSRID(geom->getSRID());
354  return g;
355  }
356  case te::gm::PolygonType:
357  {
359  g->add(geom);
360  g->setSRID(geom->getSRID());
361  return g;
362  }
364  {
366  g->add(geom);
367  g->setSRID(geom->getSRID());
368  return g;
369  }
371  {
373  g->add(geom);
374  g->setSRID(geom->getSRID());
375  return g;
376  }
378  {
380  g->add(geom);
381  g->setSRID(geom->getSRID());
382  return g;
383  }
384  default:
385  return 0;
386  }
387 }
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.
An exception class for the Vector processing module.
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.
te::gm::Geometry * single2multi(te::gm::Geometry *geom)
void setGeometryType(GeomType t)
It sets the geometry subtype.
void Multi2Single(te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
Definition: Utils.cpp:263
An atomic property like an integer or double.
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
Definition: Utils.cpp:446
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
A class that models the description of a dataset.
Definition: DataSetType.h:72
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
Definition: Utils.cpp:213
virtual Property * clone() const =0
It returns a clone of the object.
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
std::auto_ptr< te::da::DataSetType > getOutDst(bool onlySelected=false)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
It models a property definition.
Definition: Property.h:59
void setInt32(std::size_t i, boost::int32_t value)
It sets the value of the i-th property.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
TEDATAACCESSEXPORT std::size_t GetFirstSpatialPropertyPos(const te::da::DataSet *dataset)
It returns the first dataset spatial property or NULL if none is found.
Definition: Utils.cpp:462
void setOutput(te::da::DataSourcePtr outDsrc, std::string outDsName)
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
This class represents an unique id for a data set element.
Definition: ObjectId.h:47
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
int getType() const
It returns the property data type.
Definition: Property.h:161
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
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry collection and all its parts.
void setInput(te::da::DataSourcePtr inDsrc, std::string inDsName, std::auto_ptr< te::da::DataSetType > inDsType, const te::da::ObjectIdSet *oidSet=0)
void add(Geometry *g)
It adds the geometry into the collection.
te::gm::GeomType GetSimpleType(te::gm::GeomType geomType)
Definition: Utils.cpp:297
Configuration flags for the Terrralib Vector Processing module.
Aggregation Vector Processing functions.
TEDATAACCESSEXPORT te::dt::Property * GetFirstSpatialProperty(const DataSetType *dt)
Definition: Utils.cpp:534
bool IsMultiType(te::gm::GeomType geomType)
Definition: Utils.cpp:275