All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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 terralib/vp/qt/Utils.cpp
22 
23  \brief Utility functions for Vector Processing.
24 */
25 
26 // TerraLib
27 #include "../dataaccess/dataset/DataSetTypeConverter.h"
28 #include "../dataaccess/dataset/DataSetTypeCapabilities.h"
29 #include "../dataaccess/datasource/DataSource.h"
30 #include "../dataaccess/datasource/DataSourceCapabilities.h"
31 #include "../dataaccess/datasource/DataSourceInfo.h"
32 #include "../dataaccess/datasource/DataSourceManager.h"
33 #include "../dataaccess/datasource/DataSourceTransactor.h"
34 #include "../dataaccess/utils/Utils.h"
35 #include "../geometry/Geometry.h"
36 #include "../geometry/GeometryCollection.h"
37 #include "../geometry/GeometryProperty.h"
38 #include "../geometry/MultiPoint.h"
39 #include "../geometry/MultiLineString.h"
40 #include "../geometry/MultiPolygon.h"
41 #include "../geometry/Point.h"
42 #include "../memory/DataSet.h"
43 #include "Utils.h"
44 
45 //STL
46 #include <vector>
47 #include <memory>
48 
49 // Boost
50 #include <boost/algorithm/string.hpp>
51 
52 te::gm::Geometry* te::vp::GetGeometryUnion(const std::vector<te::mem::DataSetItem*>& items, size_t geomIdx, te::gm::GeomType outGeoType)
53 {
54  te::gm::Geometry* resultGeometry(0);
55 
56  std::auto_ptr<te::gm::Geometry> seedGeometry = items[0]->getGeometry(geomIdx);
57 
58  if(items.size() < 2)
59  resultGeometry = seedGeometry.release();
60 
61  if(items.size() == 2)
62  {
63  std::auto_ptr<te::gm::Geometry> teGeom = items[1]->getGeometry(geomIdx);
64 
65  if(teGeom->isValid())
66  resultGeometry = seedGeometry->Union(teGeom.release());
67  else
68  resultGeometry = seedGeometry.release();
69  }
70  if(items.size() > 2)
71  {
72  te::gm::GeometryCollection* teGeomColl = new te::gm::GeometryCollection(0, te::gm::GeometryCollectionType, seedGeometry->getSRID());
73 
74  for(std::size_t i = 1; i < items.size(); ++i)
75  {
76  std::auto_ptr<te::gm::Geometry> currentGeom = items[i]->getGeometry(geomIdx);
77 
78  if(currentGeom->isValid())
79  teGeomColl->add(currentGeom.release());
80  }
81 
82  resultGeometry = seedGeometry->Union(teGeomColl);
83 
84  }
85 
86  if (resultGeometry->getGeomTypeId() != outGeoType)
87  {
88  if(resultGeometry->getGeomTypeId() == te::gm::GeometryCollectionType)
89  {
90  te::gm::GeometryCollection* gc = new te::gm::GeometryCollection(0, outGeoType, resultGeometry->getSRID());
91  std::vector<te::gm::Geometry*> geomVec = ((te::gm::GeometryCollection*)resultGeometry)->getGeometries();
92  for(std::size_t i = 0; i < geomVec.size(); ++i)
93  {
94  te::gm::GeometryCollection* gcIn = dynamic_cast<te::gm::GeometryCollection*>(geomVec[i]);
95  if(gcIn == 0)
96  gc->add(geomVec[i]);
97  else
98  SplitGeometryCollection(gcIn, gc);
99  }
100  return gc;
101  }
102  else
103  {
104  te::gm::GeometryCollection* gc = new te::gm::GeometryCollection(1, outGeoType, resultGeometry->getSRID());
105  gc->setGeometryN(0,resultGeometry);
106  return gc;
107  }
108  }
109  else
110  return resultGeometry;
111 }
112 
113 te::gm::Geometry* te::vp::GetGeometryUnion(const std::vector<te::mem::DataSetItem*>& items, size_t geomIdx)
114 {
115  te::gm::Geometry* resultGeometry(0);
116 
117  std::auto_ptr<te::gm::Geometry> seedGeometry = items[0]->getGeometry(geomIdx);
118 
119  if(items.size() < 2)
120  resultGeometry = seedGeometry.release();
121 
122  if(items.size() == 2)
123  {
124  std::auto_ptr<te::gm::Geometry> teGeom = items[1]->getGeometry(geomIdx);
125 
126  if(teGeom->isValid())
127  resultGeometry = seedGeometry->Union(teGeom.release());
128  else
129  resultGeometry = seedGeometry.release();
130  }
131  if(items.size() > 2)
132  {
133  te::gm::GeometryCollection* teGeomColl = new te::gm::GeometryCollection(0, te::gm::GeometryCollectionType, seedGeometry->getSRID());
134 
135  for(std::size_t i = 1; i < items.size(); ++i)
136  {
137  std::auto_ptr<te::gm::Geometry> currentGeom = items[i]->getGeometry(geomIdx);
138 
139  if(currentGeom->isValid())
140  teGeomColl->add(currentGeom.release());
141  }
142 
143  resultGeometry = seedGeometry->Union(teGeomColl);
144 
145  }
146  return resultGeometry;
147 }
148 
150 {
151  std::vector<te::gm::Geometry*> geomVec = ((te::gm::GeometryCollection*)gcIn)->getGeometries();
152  for(std::size_t i = 0; i < geomVec.size(); ++i)
153  {
154  te::gm::GeometryCollection* gc = dynamic_cast<te::gm::GeometryCollection*>(geomVec[i]);
155  if(gc == 0)
156  gcOut->add(geomVec[i]);
157  else
158  SplitGeometryCollection(gc, gcOut);
159  }
160 }
161 
162 std::string te::vp::GetSimpleTableName(std::string fullName)
163 {
164  std::size_t found = fullName.rfind(".");
165 
166  if(found >= std::string::npos)
167  return fullName;
168 
169  return fullName.substr(found + 1);
170 }
171 
173 {
174  if( (firstGeom == te::gm::PolygonType && secondGeom == te::gm::PolygonType) ||
175  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::MultiPolygonType) ||
176  (firstGeom == te::gm::PolygonType && secondGeom == te::gm::MultiPolygonType) ||
177  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::PolygonType))
179 
180  if((firstGeom == te::gm::PolygonType && secondGeom == te::gm::LineStringType)||
181  (firstGeom == te::gm::PolygonType && secondGeom == te::gm::MultiLineStringType)||
182  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::LineStringType)||
183  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::MultiLineStringType)||
184 
185  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::PolygonType)||
186  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::MultiPolygonType)||
187  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::PolygonType)||
188  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::MultiPolygonType)||
189 
190  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::LineStringType) ||
191  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::MultiLineStringType) ||
192  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::MultiLineStringType) ||
193  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::LineStringType))
195 
196  return te::gm::MultiPointType;
197 }
198 
200 {
201  if (firstGeom == te::gm::PolygonType)
203 
204  if (firstGeom == te::gm::LineStringType)
206 
207  if (firstGeom == te::gm::PointType)
208  return te::gm::MultiPointType;
209 
210  return firstGeom;
211 }
212 
214 {
215  // do any adaptation necessary to persist the output dataset
216  te::da::DataSetTypeConverter* converter = new te::da::DataSetTypeConverter(outDsType, source->getCapabilities());
217  te::da::DataSetType* dsTypeResult = converter->getResult();
218 
219  std::auto_ptr<te::da::DataSourceTransactor> t = source->getTransactor();
220 
221  std::map<std::string, std::string> options;
222 
223  try
224  {
225  if(source->getType() == "OGR")
226  {
227  // create the dataset
228  source->createDataSet(dsTypeResult, options);
229 
230  // copy from memory to output datasource
231  result->moveBeforeFirst();
232  std::string name = dsTypeResult->getName();
233  source->add(dsTypeResult->getName(),result, options);
234  }
235  else
236  {
237  t->begin();
238 
239  // create the dataset
240  t->createDataSet(dsTypeResult, options);
241 
242  // copy from memory to output datasource
243  result->moveBeforeFirst();
244  std::string name = dsTypeResult->getName();
245  t->add(dsTypeResult->getName(),result, options);
246 
247  t->commit();
248  }
249 
250  }
251  catch(te::common::Exception& e)
252  {
253  t->rollBack();
254  throw e;
255  }
256  catch(std::exception& e)
257  {
258  t->rollBack();
259  throw e;
260  }
261 }
262 
263 void te::vp::Multi2Single(te::gm::Geometry* g, std::vector<te::gm::Geometry*>& geoms)
264 {
266  if(gc)
267  {
268  for(std::size_t i = 0; i < gc->getNumGeometries(); ++i)
269  Multi2Single(gc->getGeometryN(i), geoms);
270  }
271  else
272  geoms.push_back(g);
273 }
274 
276 {
277  switch(geomType)
278  {
291  return true;
292  default:
293  return false;
294  }
295 }
296 
298 {
299  switch(geomType)
300  {
302  return te::gm::LineStringType;
310  return te::gm::PointType;
312  return te::gm::PointMType;
314  return te::gm::PointZType;
316  return te::gm::PointZMType;
318  return te::gm::PolygonType;
320  return te::gm::PolygonMType;
322  return te::gm::PolygonZType;
324  return te::gm::PolygonZMType;
325  default:
327  }
328 }
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
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
Utility functions for the data access module.
void Multi2Single(te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
Definition: Utils.cpp:263
A class that models the description of a dataset.
Definition: DataSetType.h:72
virtual void createDataSet(DataSetType *dt, const std::map< std::string, std::string > &options)
It creates the dataset schema definition in the target data source.
Definition: DataSource.cpp:445
virtual std::auto_ptr< DataSourceTransactor > getTransactor()=0
It returns an object that can execute transactions in the context of a data source.
te::gm::Geometry * GetGeometryUnion(const std::vector< te::mem::DataSetItem * > &items, size_t geomIdx, te::gm::GeomType outGeoType)
It returns the union of a geometry vector.
Definition: Utils.cpp:52
void Save(te::da::DataSource *source, te::da::DataSet *result, te::da::DataSetType *outDsType)
Definition: Utils.cpp:213
virtual void add(const std::string &datasetName, DataSet *d, const std::map< std::string, std::string > &options, std::size_t limit=0)
It adds data items to the dataset in the data source.
Definition: DataSource.cpp:471
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
Definition: DataSource.h:118
An converter for DataSetType.
void SplitGeometryCollection(te::gm::GeometryCollection *geomIn, te::gm::GeometryCollection *gcOut)
Definition: Utils.cpp:149
virtual std::string getType() const =0
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
virtual const DataSourceCapabilities & getCapabilities() const =0
It returns the known capabilities of the data source.
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
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
te::gm::GeomType GeomOpResultType(te::gm::GeomType firstGeom, te::gm::GeomType secondGeom)
Definition: Utils.cpp:172
void add(Geometry *g)
It adds the geometry into the collection.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
const std::vector< Geometry * > & getGeometries() const
It returns a reference to the internal list of geometries.
void setGeometryN(std::size_t i, Geometry *g)
It sets the n-th geometry in this geometry collection.
te::gm::GeomType GetSimpleType(te::gm::GeomType geomType)
Definition: Utils.cpp:297
virtual Geometry * Union(const Geometry *const rhs) const
It returns a geometric object that represents the point set union with another geometry.
Definition: Geometry.cpp:544
It is a collection of other geometric objects.
bool IsMultiType(te::gm::GeomType geomType)
Definition: Utils.cpp:275
std::string GetSimpleTableName(std::string fullName)
Definition: Utils.cpp:162