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 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 "terralib_config.h"
28 #include "../common/STLUtils.h"
29 #include "../common/Translator.h"
30 #include "Envelope.h"
31 #include "Exception.h"
32 #include "GeometryCollection.h"
33 
34 // STL
35 #include <cassert>
36 
37 const std::string te::gm::GeometryCollection::sm_typeName("GeometryCollection");
38 
40  : te::gm::Geometry(t, srid, mbr),
41  m_geometries(nGeom)
42 {
43 }
44 
46  : Geometry(rhs)
47 {
49 }
50 
52 {
53  te::common::FreeContents(m_geometries);
54 }
55 
57 {
58  if(this != &rhs)
59  {
61 
62  te::common::FreeContents(m_geometries);
63 
64  m_geometries.clear();
65 
66  te::common::Clone(rhs.m_geometries, m_geometries);
67  }
68 
69  return *this;
70 }
71 
73 {
74  return new GeometryCollection(*this);
75 }
76 
78 {
79  Dimensionality maxDim = P;
80 
81  std::size_t size = m_geometries.size();
82 
83  for(std::size_t i = 0; i < size; ++i)
84  {
85  if(maxDim < m_geometries[i]->getDimension())
86  maxDim = m_geometries[i]->getDimension();
87  }
88 
89  return maxDim;
90 }
91 
92 const std::string& te::gm::GeometryCollection::getGeometryType() const throw()
93 {
94  return sm_typeName;
95 }
96 
98 {
99  std::size_t n = m_geometries.size();
100 
101  for(std::size_t i = 0; i < n; ++i)
102  m_geometries[i]->setSRID(srid);
103 
104  m_srid = srid;
105 }
106 
108 {
109 #ifdef TERRALIB_MOD_SRS_ENABLED
110  if(srid == m_srid)
111  return;
112 
113  std::size_t nGeoms = m_geometries.size();
114 
115  for(std::size_t i = 0; i < nGeoms; ++i)
116  m_geometries[i]->transform(srid);
117 
118  m_srid = srid;
119 
120  if(m_mbr)
121  computeMBR(false); // the above transform will do the job for the parts, so don't compute mbr again!
122 #else
123  throw Exception(TE_TR("transform method is not supported!"));
124 #endif // TERRALIB_MOD_SRS_ENABLED
125 }
126 
127 void te::gm::GeometryCollection::computeMBR(bool cascade) const throw()
128 {
129  if(m_mbr == 0)
130  m_mbr = new Envelope;
131  else
132  m_mbr->makeInvalid();
133 
134  std::size_t nGeoms = m_geometries.size();
135 
136  if(nGeoms == 0)
137  return;
138 
139  if(cascade)
140  m_geometries[0]->computeMBR(true);
141 
142  *m_mbr = *(m_geometries[0]->getMBR());
143 
144  for(std::size_t i = 1; i < nGeoms; ++i)
145  {
146  if(cascade)
147  m_geometries[i]->computeMBR(true);
148 
149  m_mbr->Union(*(m_geometries[i]->getMBR()));
150  }
151 }
152 
153 std::size_t te::gm::GeometryCollection::getNPoints() const throw()
154 {
155  std::size_t n = m_geometries.size();
156 
157  std::size_t sum = 0;
158 
159  for(std::size_t i = 0; i < n; ++i)
160  {
161  assert(m_geometries[i] != 0);
162  sum += m_geometries[i]->getNPoints();
163  }
164 
165  return sum;
166 }
167 
169 {
170  if(size < m_geometries.size())
171  {
172  std::size_t oldSize = m_geometries.size();
173  for(std::size_t i = size; i < oldSize; ++i)
174  delete m_geometries[i];
175  }
176 
177  m_geometries.resize(size);
178 }
179 
181 {
182  assert(i < m_geometries.size());
183  return m_geometries[i];
184 }
185 
187 {
188  assert(i < m_geometries.size());
189  return m_geometries[i];
190 }
191 
193 {
194  assert((i < m_geometries.size()) && (m_geometries[i] == 0));
195  delete m_geometries[i];
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  m_geometries.push_back(g);
209 }
210 
212 {
213  te::common::FreeContents(m_geometries);
214  m_geometries.clear();
215 }
216 
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:78
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:347
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...