All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 "Envelope.h"
29 #include "GeometryCollection.h"
30 
31 // STL
32 #include <cassert>
33 
34 const std::string te::gm::GeometryCollection::sm_typeName("GeometryCollection");
35 
37  : te::gm::Geometry(t, srid, mbr),
38  m_geometries(nGeom)
39 {
40 }
41 
43  : Geometry(rhs)
44 {
46 }
47 
49 {
50  te::common::FreeContents(m_geometries);
51 }
52 
54 {
55  if(this != &rhs)
56  {
58 
59  te::common::FreeContents(m_geometries);
60 
61  m_geometries.clear();
62 
63  te::common::Clone(rhs.m_geometries, m_geometries);
64  }
65 
66  return *this;
67 }
68 
70 {
71  return new GeometryCollection(*this);
72 }
73 
75 {
76  Dimensionality maxDim = P;
77 
78  std::size_t size = m_geometries.size();
79 
80  for(std::size_t i = 0; i < size; ++i)
81  {
82  if(maxDim < m_geometries[i]->getDimension())
83  maxDim = m_geometries[i]->getDimension();
84  }
85 
86  return maxDim;
87 }
88 
89 const std::string& te::gm::GeometryCollection::getGeometryType() const throw()
90 {
91  return sm_typeName;
92 }
93 
95 {
96  std::size_t n = m_geometries.size();
97 
98  for(std::size_t i = 0; i < n; ++i)
99  m_geometries[i]->setSRID(srid);
100 
101  m_srid = srid;
102 }
103 
105 {
106  if(srid == m_srid)
107  return;
108 
109  std::size_t nGeoms = m_geometries.size();
110 
111  for(std::size_t i = 0; i < nGeoms; ++i)
112  m_geometries[i]->transform(srid);
113 
114  m_srid = srid;
115 
116  if(m_mbr)
117  computeMBR(false); // the above transform will do the job for the parts, so don't compute mbr again!
118 }
119 
120 void te::gm::GeometryCollection::computeMBR(bool cascade) const throw()
121 {
122  if(m_mbr == 0)
123  m_mbr = new Envelope;
124  else
125  m_mbr->makeInvalid();
126 
127  std::size_t nGeoms = m_geometries.size();
128 
129  if(nGeoms == 0)
130  return;
131 
132  if(cascade)
133  m_geometries[0]->computeMBR(true);
134 
135  *m_mbr = *(m_geometries[0]->getMBR());
136 
137  for(std::size_t i = 1; i < nGeoms; ++i)
138  {
139  if(cascade)
140  m_geometries[i]->computeMBR(true);
141 
142  m_mbr->Union(*(m_geometries[i]->getMBR()));
143  }
144 }
145 
146 std::size_t te::gm::GeometryCollection::getNPoints() const throw()
147 {
148  std::size_t n = m_geometries.size();
149 
150  std::size_t sum = 0;
151 
152  for(std::size_t i = 0; i < n; ++i)
153  {
154  assert(m_geometries[i] != 0);
155  sum += m_geometries[i]->getNPoints();
156  }
157 
158  return sum;
159 }
160 
162 {
163  if(size < m_geometries.size())
164  {
165  std::size_t oldSize = m_geometries.size();
166  for(std::size_t i = size; i < oldSize; ++i)
167  delete m_geometries[i];
168  }
169 
170  m_geometries.resize(size);
171 }
172 
174 {
175  assert(i < m_geometries.size());
176  return m_geometries[i];
177 }
178 
180 {
181  assert(i < m_geometries.size());
182  return m_geometries[i];
183 }
184 
186 {
187  assert((i < m_geometries.size()) && (m_geometries[i] == 0));
188  delete m_geometries[i];
189  m_geometries[i] = g;
190 }
191 
193 {
194  assert(i < m_geometries.size());
195  delete m_geometries[i];
196  m_geometries.erase(m_geometries.begin() + i);
197 }
198 
200 {
201  m_geometries.push_back(g);
202 }
203 
205 {
206  te::common::FreeContents(m_geometries);
207  m_geometries.clear();
208 }
209 
void setNumGeometries(std::size_t size)
It sets the number of geometries in this GeometryCollection.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
virtual Dimensionality getDimension() const
For non-homogeneous collections this method will return the largest dimension of the contained object...
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
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...
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
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
It is a collection of other geometric objects.
void computeMBR(bool cascade) const
It computes the minimum bounding rectangle for the geometry collection.
GeometryCollection & operator=(const GeometryCollection &rhs)
Assignment operator.
void add(Geometry *g)
It adds the geometry into the collection.
Dimensionality
From Wikipedia: &quot;in mathematics, the dimension of an object is an intrinsic property, independent of the space in which the object may happen to be embedded&quot;.
Definition: Enums.h:142
std::size_t getNPoints() const
it returns the number of points (vertexes) in the geometry.
void clear()
It deletes all the elements of the collection.
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
std::vector< Geometry * > m_geometries
The array of geometries that forms the collection.
static const std::string sm_typeName
Geometry type name for GeometryCollection.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
It is a collection of other geometric objects.
void transform(int srid)
It will transform the coordinates of the geometry collection to the new one.
virtual Geometry & operator=(const Geometry &rhs)
Assignment operator.
Definition: Geometry.cpp:77
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry collection and all its parts.
An Envelope defines a 2D rectangular region.
virtual te::dt::AbstractData * clone() const
It clones the geometry collection.
virtual const std::string & getGeometryType() const
The name of the Geometry subtype is: GeometryCollection.
void setGeometryN(std::size_t i, Geometry *g)
It sets the n-th geometry in this geometry collection.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void removeGeometryN(std::size_t i)
It removes the n-th geometry in this geometry collection.
void makeInvalid()
It will invalidated the envelope.
Definition: Envelope.h:430
virtual ~GeometryCollection()
Virtual destructor.