All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2011 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/datasource/DataSourceInfo.h"
28 #include "../dataaccess/datasource/DataSourceManager.h"
29 #include "../dataaccess/utils/Utils.h"
30 #include "../geometry/Geometry.h"
31 #include "../geometry/GeometryCollection.h"
32 #include "../geometry/GeometryProperty.h"
33 #include "../geometry/MultiPoint.h"
34 #include "../geometry/MultiLineString.h"
35 #include "../geometry/MultiPolygon.h"
36 #include "../geometry/Point.h"
37 #include "../memory/DataSet.h"
38 #include "Utils.h"
39 
40 // Qt
41 #include <QtGui/QTreeView>
42 
43 //STL
44 #include <vector>
45 #include <memory>
46 
47 // Boost
48 #include <boost/algorithm/string.hpp>
49 
50 te::gm::Geometry* te::vp::GetGeometryUnion(const std::vector<te::mem::DataSetItem*>& items, size_t geomIdx, te::gm::GeomType outGeoType)
51 {
52  te::gm::Geometry* resultGeometry(0);
53 
54  std::auto_ptr<te::gm::Geometry> seedGeometry = items[0]->getGeometry(geomIdx);
55 
56  if(items.size() < 2)
57  resultGeometry = seedGeometry.release();
58 
59  if(items.size() == 2)
60  {
61  std::auto_ptr<te::gm::Geometry> teGeom = items[1]->getGeometry(geomIdx);
62 
63  if(teGeom->isValid())
64  resultGeometry = seedGeometry->Union(teGeom.release());
65  else
66  resultGeometry = seedGeometry.release();
67  }
68  if(items.size() > 2)
69  {
70  te::gm::GeometryCollection* teGeomColl = new te::gm::GeometryCollection(0, te::gm::GeometryCollectionType, seedGeometry->getSRID());
71 
72  for(std::size_t i = 1; i < items.size(); ++i)
73  {
74  std::auto_ptr<te::gm::Geometry> currentGeom = items[i]->getGeometry(geomIdx);
75 
76  if(currentGeom->isValid())
77  teGeomColl->add(currentGeom.release());
78  }
79 
80  resultGeometry = seedGeometry->Union(teGeomColl);
81 
82  }
83 
84  if (resultGeometry->getGeomTypeId() != outGeoType)
85  {
86  if(resultGeometry->getGeomTypeId() == te::gm::GeometryCollectionType)
87  {
88  te::gm::GeometryCollection* gc = new te::gm::GeometryCollection(0, outGeoType, resultGeometry->getSRID());
89  std::vector<te::gm::Geometry*> geomVec = ((te::gm::GeometryCollection*)resultGeometry)->getGeometries();
90  for(std::size_t i = 0; i < geomVec.size(); ++i)
91  {
92  te::gm::GeometryCollection* gcIn = dynamic_cast<te::gm::GeometryCollection*>(geomVec[i]);
93  if(gcIn == 0)
94  gc->add(geomVec[i]);
95  else
96  SplitGeometryCollection(gcIn, gc);
97  }
98  return gc;
99  }
100  else
101  {
102  te::gm::GeometryCollection* gc = new te::gm::GeometryCollection(1, outGeoType, resultGeometry->getSRID());
103  gc->setGeometryN(0,resultGeometry);
104  return gc;
105  }
106  }
107  else
108  return resultGeometry;
109 }
110 
112 {
113  std::vector<te::gm::Geometry*> geomVec = ((te::gm::GeometryCollection*)gcIn)->getGeometries();
114  for(std::size_t i = 0; i < geomVec.size(); ++i)
115  {
116  te::gm::GeometryCollection* gc = dynamic_cast<te::gm::GeometryCollection*>(geomVec[i]);
117  if(gc == 0)
118  gcOut->add(geomVec[i]);
119  else
120  SplitGeometryCollection(gc, gcOut);
121  }
122 }
123 
124 std::string te::vp::GetSimpleTableName(std::string fullName)
125 {
126  std::size_t found = fullName.rfind(".");
127 
128  if(found >= std::string::npos)
129  return fullName;
130 
131  return fullName.substr(found + 1);
132 }
133 
135 {
136  if( (firstGeom == te::gm::PolygonType && secondGeom == te::gm::PolygonType) ||
137  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::MultiPolygonType) ||
138  (firstGeom == te::gm::PolygonType && secondGeom == te::gm::MultiPolygonType) ||
139  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::PolygonType))
141 
142  if((firstGeom == te::gm::PolygonType && secondGeom == te::gm::LineStringType)||
143  (firstGeom == te::gm::PolygonType && secondGeom == te::gm::MultiLineStringType)||
144  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::LineStringType)||
145  (firstGeom == te::gm::MultiPolygonType && secondGeom == te::gm::MultiLineStringType)||
146 
147  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::PolygonType)||
148  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::MultiPolygonType)||
149  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::PolygonType)||
150  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::MultiPolygonType)||
151 
152  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::LineStringType) ||
153  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::MultiLineStringType) ||
154  (firstGeom == te::gm::LineStringType && secondGeom == te::gm::MultiLineStringType) ||
155  (firstGeom == te::gm::MultiLineStringType && secondGeom == te::gm::LineStringType))
157 
158  return te::gm::MultiPointType;
159 }
160 
162 {
163  if (firstGeom == te::gm::PolygonType)
165 
166  if (firstGeom == te::gm::LineStringType)
168 
169  if (firstGeom == te::gm::PointType)
170  return te::gm::MultiPointType;
171 
172  return firstGeom;
173 }
int getSRID() const
It returns the Spatial Reference System ID associated to this geometric object.
Definition: Geometry.h:189
te::gm::GeomType GeomOpResultType(te::gm::GeomType firstGeom, te::gm::GeomType secondGeom)
Definition: Utils.cpp:134
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:50
const std::vector< Geometry * > & getGeometries() const
It returns a reference to the internal list of geometries.
void add(Geometry *g)
It adds the geometry into the collection.
Utility functions for the data access module.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
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:473
GeomType getGeomTypeId() const
It returns the geometry subclass type identifier.
Definition: Geometry.h:178
std::string GetSimpleTableName(std::string fullName)
Definition: Utils.cpp:124
It is a collection of other geometric objects.
void SplitGeometryCollection(te::gm::GeometryCollection *geomIn, te::gm::GeometryCollection *gcOut)
Definition: Utils.cpp:111
void setGeometryN(std::size_t i, Geometry *g)
It sets the n-th geometry in this geometry collection.