All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CurvePolygon.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/CurvePolygon.cpp
22 
23  \brief CurvePolygon is a planar surface defined by 1 exterior boundary and 0 or more interior boundaries.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../common/Translator.h"
29 #include "Curve.h"
30 #include "CurvePolygon.h"
31 #include "Envelope.h"
32 #include "GEOSWriter.h"
33 #include "Point.h"
34 
35 #if TE_USE_GEOS
36 // GEOS
37 #include <geos/geom/Geometry.h>
38 #include <geos/geom/IntersectionMatrix.h>
39 #include <geos/operation/buffer/OffsetCurveBuilder.h>
40 #include <geos/util/GEOSException.h>
41 #endif
42 
43 const std::string te::gm::CurvePolygon::sm_typeName("CurvePolygon");
44 
45 te::gm::CurvePolygon::CurvePolygon(std::size_t nRings, GeomType t, int srid, Envelope* mbr)
46  : Surface(t, srid, mbr),
47  m_rings(nRings)
48 {
49 }
50 
52  : Surface(rhs)
53 {
55 }
56 
58 {
59  te::common::FreeContents(m_rings);
60 }
61 
63 {
64  if(this != &rhs)
65  {
66  Surface::operator=(rhs);
67 
68  te::common::FreeContents(m_rings);
69 
70  m_rings.clear();
71 
72  te::common::Clone(rhs.m_rings, m_rings);
73  }
74 
75  return *this;
76 }
77 
79 {
80  return new CurvePolygon(*this);
81 }
82 
84 {
85  assert(m_rings.size() > 0);
86  return m_rings[0];
87 }
88 
90 {
91  return (m_rings.size() > 0) ? (m_rings.size() - 1) : 0;
92 }
93 
94 void te::gm::CurvePolygon::setNumRings(std::size_t size)
95 {
96  if(size < m_rings.size())
97  {
98  std::size_t oldSize = m_rings.size();
99  for(std::size_t i = size; i < oldSize; ++i)
100  delete m_rings[i];
101  }
102 
103  m_rings.resize(size);
104 }
105 
107 {
108  assert(i < getNumInteriorRings());
109  return m_rings[i + 1];
110 }
111 
112 void te::gm::CurvePolygon::setRingN(std::size_t i, Curve* r)
113 {
114  assert(i < m_rings.size());
115  delete m_rings[i];
116  m_rings[i] = r;
117 }
118 
120 {
121  assert(i < m_rings.size());
122  delete m_rings[i];
123  m_rings.erase(m_rings.begin() + i);
124 }
125 
127 {
128  te::common::FreeContents(m_rings);
129  m_rings.clear();
130 }
131 
133 {
134 #if TE_USE_GEOS
135  std::auto_ptr<geos::geom::Geometry> g(GEOSWriter::write(this));
136 
137  return g->getArea();
138 
139 #else
140  throw Exception(TR_GEOM("getArea routine is supported by GEOS! Please, enable the GEOS support."));
141 #endif
142 }
143 
145 {
146  return 0;
147 }
148 
150 {
151  return 0;
152 }
153 
155 {
156  return 0;
157 }
158 
160 {
161  return 0;
162 }
163 
165 {
166  return 0.0;
167 }
168 
169 const std::string& te::gm::CurvePolygon::getGeometryType() const throw()
170 {
171  return sm_typeName;
172 }
173 
174 void te::gm::CurvePolygon::setSRID(int srid) throw()
175 {
176  std::size_t n = m_rings.size();
177 
178  for(std::size_t i = 0; i < n; ++i)
179  m_rings[i]->setSRID(srid);
180 
181  m_srid = srid;
182 }
183 
185 {
186  if(srid == m_srid)
187  return;
188 
189  std::size_t n = m_rings.size();
190 
191  for(std::size_t i = 0; i < n; ++i)
192  m_rings[i]->transform(srid);
193 
194  if(m_mbr)
195  computeMBR(true); // just update the polygon MBR
196 
197  m_srid = srid;
198 }
199 
200 void te::gm::CurvePolygon::computeMBR(bool cascade) const throw()
201 {
202  if(m_mbr == 0)
203  m_mbr = new Envelope;
204  else
205  m_mbr->makeInvalid();
206 
207  std::size_t n = m_rings.size();
208 
209  if(n == 0)
210  return;
211 
212  if(cascade)
213  m_rings[0]->computeMBR(true);
214 
215  *m_mbr = *(m_rings[0]->getMBR());
216 
217  if(cascade)
218  for(std::size_t i = 1; i < n; ++i)
219  m_rings[i]->computeMBR(true);
220 }
221 
222 std::size_t te::gm::CurvePolygon::getNPoints() const throw()
223 {
224  std::size_t n = m_rings.size();
225 
226  std::size_t sum = 0;
227 
228  for(std::size_t i = 0; i < n; ++i)
229  sum += m_rings[i]->getNPoints();
230 
231  return sum;
232 }
233 
virtual ~CurvePolygon()
Virtual destructor.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
CurvePolygon is a planar surface defined by 1 exterior boundary and 0 or more interior boundaries...
Definition: CurvePolygon.h:57
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
Curve * getInteriorRingN(std::size_t i) const
It returns the n-th interior ring for this curve polygon as a curve.
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
virtual te::dt::AbstractData * clone() const
It clones the linestring.
#define TR_GEOM(message)
It marks a string in order to get translated. This is a special mark used in the Vector Geometry modu...
Definition: Config.h:58
A class that converts a TerraLib geometry to a GEOS geometry.
virtual CurvePolygon & operator=(const CurvePolygon &rhs)
Assignment operator.
std::size_t getNumInteriorRings() const
It returns the number of interior rings in this CurvePolygon.
virtual Surface & operator=(const Surface &rhs)
Assignment operator.
Definition: Surface.cpp:39
Coord2D * getCentroidCoord() const
It returns the mathematical centroid for this surface as a coordinate.
std::vector< Curve * > m_rings
An array with the ring list.
Definition: CurvePolygon.h:439
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
Curve * getExteriorRing() const
It returns the exterior ring of this CurvePolygon.
Curve is an abstract class that represents 1-dimensional geometric objects stored as a sequence of co...
Definition: Curve.h:57
Point * getPointOnSurface() const
It returns a point guaranteed to be on this surface.
double getPerimeter() const
It returns the length of the boundary for the surface.
A point with x and y coordinate values.
void clear()
It deletes all the rings of the CurvePolygon and clear it.
A point with x and y coordinate values.
Definition: Point.h:50
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
void removeRingN(std::size_t i)
It removes the n-th ring in this CurvePolygon.
Coord2D * getCoordOnSurface() const
It returns a coordinate guaranteed to be on this surface.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
void computeMBR(bool cascade) const
It computes the minimum bounding rectangle for the curve polygon.
virtual const std::string & getGeometryType() const
The name of the geometry subtype for curve polygons is: CurvePolygon.
CurvePolygon is a planar surface defined by 1 exterior boundary and 0 or more interior boundaries...
void transform(int srid)
It converts the coordinate values of the geometry to the new spatial reference system.
Curve is an abstract class that represents 1-dimensional geometric objects stored as a sequence of co...
An Envelope defines a 2D rectangular region.
CurvePolygon(std::size_t nRings, GeomType t, int srid=0, Envelope *mbr=0)
It initializes the curve polygon with the specified spatial reference system id and envelope...
std::size_t getNPoints() const
it returns the number of points (vertexes) in the geometry.
Point * getCentroid() const
It returns the mathematical centroid for this surface as a point.
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
static const std::string sm_typeName
Definition: CurvePolygon.h:441
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void setNumRings(std::size_t size)
It sets the number of rings in this curve polygon.
Surface is an abstract class that represents a 2-dimensional geometric objects.
Definition: Surface.h:54
void makeInvalid()
It will invalidated the envelope.
Definition: Envelope.h:430