geometry/GeometryCollection.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/geometry/GeometryCollection.cpp
22 
23  \brief It is a collection of other geometric objects.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../core/translator/Translator.h"
29 #include "Envelope.h"
30 #include "Exception.h"
31 #include "GeometryCollection.h"
32 
33 // STL
34 #include <cassert>
35 
36 const std::string te::gm::GeometryCollection::sm_typeName("GeometryCollection");
37 
39  : te::gm::Geometry(t, srid, mbr),
40  m_geometries(nGeom)
41 {
42 }
43 
45  : Geometry(rhs)
46 {
48 }
49 
51 {
53 }
54 
56 {
57  if(this != &rhs)
58  {
60 
62 
63  m_geometries.clear();
64 
66  }
67 
68  return *this;
69 }
70 
72 {
73  return new GeometryCollection(*this);
74 }
75 
77 {
78  Dimensionality maxDim = P;
79 
80  std::size_t size = m_geometries.size();
81 
82  for(std::size_t i = 0; i < size; ++i)
83  {
84  if(maxDim < m_geometries[i]->getDimension())
85  maxDim = m_geometries[i]->getDimension();
86  }
87 
88  return maxDim;
89 }
90 
91 const std::string& te::gm::GeometryCollection::getGeometryType() const throw()
92 {
93  return sm_typeName;
94 }
95 
97 {
98  std::size_t n = m_geometries.size();
99 
100  for(std::size_t i = 0; i < n; ++i)
101  m_geometries[i]->setSRID(srid);
102 
103  m_srid = srid;
104 }
105 
107 {
108 #ifdef TERRALIB_MOD_SRS_ENABLED
109  if(srid == m_srid)
110  return;
111 
112  std::size_t nGeoms = m_geometries.size();
113 
114  for(std::size_t i = 0; i < nGeoms; ++i)
115  m_geometries[i]->transform(srid);
116 
117  m_srid = srid;
118 
119  if(m_mbr)
120  computeMBR(false); // the above transform will do the job for the parts, so don't compute mbr again!
121 #else
122  throw Exception(TE_TR("transform method is not supported!"));
123 #endif // TERRALIB_MOD_SRS_ENABLED
124 }
125 
126 void te::gm::GeometryCollection::computeMBR(bool cascade) const throw()
127 {
128  if(m_mbr == nullptr)
129  m_mbr = new Envelope;
130  else
131  m_mbr->makeInvalid();
132 
133  std::size_t nGeoms = m_geometries.size();
134 
135  if(nGeoms == 0)
136  return;
137 
138  if(cascade)
139  m_geometries[0]->computeMBR(true);
140 
141  *m_mbr = *(m_geometries[0]->getMBR());
142 
143  for(std::size_t i = 1; i < nGeoms; ++i)
144  {
145  if(cascade)
146  m_geometries[i]->computeMBR(true);
147 
148  m_mbr->Union(*(m_geometries[i]->getMBR()));
149  }
150 }
151 
152 std::size_t te::gm::GeometryCollection::getNPoints() const throw()
153 {
154  std::size_t n = m_geometries.size();
155 
156  std::size_t sum = 0;
157 
158  for(std::size_t i = 0; i < n; ++i)
159  {
160  assert(m_geometries[i] != nullptr);
161  sum += m_geometries[i]->getNPoints();
162  }
163 
164  return sum;
165 }
166 
168 {
169  if(size < m_geometries.size())
170  {
171  std::size_t oldSize = m_geometries.size();
172  for(std::size_t i = size; i < oldSize; ++i)
173  delete m_geometries[i];
174  }
175 
176  m_geometries.resize(size);
177 }
178 
180 {
181  assert(i < m_geometries.size());
182  return m_geometries[i];
183 }
184 
186 {
187  assert(i < m_geometries.size());
188  return m_geometries[i];
189 }
190 
192 {
193  assert((i < m_geometries.size()) && (m_geometries[i] == nullptr));
194  delete m_geometries[i];
195  g->setSRID(this->getSRID());
196  m_geometries[i] = g;
197 }
198 
200 {
201  assert(i < m_geometries.size());
202  delete m_geometries[i];
203  m_geometries.erase(m_geometries.begin() + i);
204 }
205 
207 {
208  g->setSRID(this->getSRID());
209  m_geometries.push_back(g);
210 }
211 
213 {
215  m_geometries.clear();
216 }
217 
virtual Dimensionality getDimension() const
For non-homogeneous collections this method will return the largest dimension of the contained object...
void computeMBR(bool cascade) const
It computes the minimum bounding rectangle for the geometry collection.
void makeInvalid()
It will invalidated the envelope.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
void setNumGeometries(std::size_t size)
It sets the number of geometries in this GeometryCollection.
Base exception class for plugin module.
virtual const std::string & getGeometryType() const
The name of the Geometry subtype is: GeometryCollection.
int m_srid
The Spatial Reference System code associated to the Geometry.
GeometryCollection & operator=(const GeometryCollection &rhs)
Assignment operator.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void removeGeometryN(std::size_t i)
It removes the n-th geometry in this geometry collection.
virtual te::dt::AbstractData * clone() const
It clones the geometry collection.
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
An Envelope defines a 2D rectangular region.
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
An Envelope defines a 2D rectangular region.
void clear()
It deletes all the elements of the collection.
std::vector< Geometry * > m_geometries
The array of geometries that forms the collection.
Dimensionality
From Wikipedia: "in mathematics, the dimension of an object is an intrinsic property, independent of the space in which the object may happen to be embedded".
URI C++ Library.
Definition: Attributes.h:37
void transform(int srid)
It will transform the coordinates of the geometry collection to the new one.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
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.
virtual Geometry & operator=(const Geometry &rhs) _NOEXCEPT_OP(true)
Assignment operator.
Envelope * m_mbr
The geometry minimum bounding rectangle.
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry collection and all its parts.
virtual ~GeometryCollection()
Virtual destructor.
void add(Geometry *g)
It adds the geometry into the collection.
virtual void setSRID(int srid) _NOEXCEPT_OP(true)=0
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
void setGeometryN(std::size_t i, Geometry *g)
It sets the n-th geometry in this geometry collection.
static const std::string sm_typeName
Geometry type name for GeometryCollection.
It is a collection of other geometric objects.
void Clone(const std::vector< T * > &src, std::vector< T * > &dst)
This function can be applied to a vector of pointers.
Definition: STLUtils.h:237
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
An exception class for the Geometry module.
std::size_t getNPoints() const
it returns the number of points (vertexes) in the geometry.
It is a collection of other geometric objects.
GeometryCollection(std::size_t nGeom, GeomType t, int srid=0, Envelope *mbr=0)
It initializes the geometry collection with the specified spatial reference system id and envelope...