All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeometryCollection.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008-2013 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 "../common/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 {
52  te::common::FreeContents(m_geometries);
53 }
54 
56 {
57  if(this != &rhs)
58  {
60 
61  te::common::FreeContents(m_geometries);
62 
63  m_geometries.clear();
64 
65  te::common::Clone(rhs.m_geometries, m_geometries);
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 == 0)
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] != 0);
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] == 0));
194  delete m_geometries[i];
195  m_geometries[i] = g;
196 }
197 
199 {
200  assert(i < m_geometries.size());
201  delete m_geometries[i];
202  m_geometries.erase(m_geometries.begin() + i);
203 }
204 
206 {
207  m_geometries.push_back(g);
208 }
209 
211 {
212  te::common::FreeContents(m_geometries);
213  m_geometries.clear();
214 }
215 
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.
Definition: Envelope.h:430
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
virtual Geometry & operator=(const Geometry &rhs)
Assignment operator.
Definition: Geometry.cpp:77
void setNumGeometries(std::size_t size)
It sets the number of geometries in this GeometryCollection.
virtual const std::string & getGeometryType() const
The name of the Geometry subtype is: GeometryCollection.
GeometryCollection & operator=(const GeometryCollection &rhs)
Assignment operator.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:345
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.
An Envelope defines a 2D rectangular region.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
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".
Definition: Enums.h:142
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. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
An exception class for the Geometry module.
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.
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
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...