All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CurvePolygon.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/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 "terralib_config.h"
28 #include "../common/STLUtils.h"
29 #include "../common/Translator.h"
30 #include "Config.h"
31 #include "Coord2D.h"
32 #include "Curve.h"
33 #include "CurvePolygon.h"
34 #include "Envelope.h"
35 #include "GEOSWriter.h"
36 #include "Point.h"
37 
38 #ifdef TERRALIB_GEOS_ENABLED
39 // GEOS
40 #include <geos/algorithm/CentroidArea.h>
41 #include <geos/geom/Geometry.h>
42 #include <geos/geom/IntersectionMatrix.h>
43 #include <geos/operation/buffer/OffsetCurveBuilder.h>
44 #include <geos/util/GEOSException.h>
45 #endif
46 
47 const std::string te::gm::CurvePolygon::sm_typeName("CurvePolygon");
48 
49 te::gm::CurvePolygon::CurvePolygon(std::size_t nRings, GeomType t, int srid, Envelope* mbr)
50  : Surface(t, srid, mbr),
51  m_rings(nRings)
52 {
53 }
54 
56  : Surface(rhs)
57 {
59 }
60 
62 {
63  te::common::FreeContents(m_rings);
64 }
65 
67 {
68  if(this != &rhs)
69  {
70  Surface::operator=(rhs);
71 
72  te::common::FreeContents(m_rings);
73 
74  m_rings.clear();
75 
76  te::common::Clone(rhs.m_rings, m_rings);
77  }
78 
79  return *this;
80 }
81 
83 {
84  return new CurvePolygon(*this);
85 }
86 
88 {
89  assert(m_rings.size() > 0);
90  return m_rings[0];
91 }
92 
94 {
95  return (m_rings.size() > 0) ? (m_rings.size() - 1) : 0;
96 }
97 
98 void te::gm::CurvePolygon::setNumRings(std::size_t size)
99 {
100  if(size < m_rings.size())
101  {
102  std::size_t oldSize = m_rings.size();
103  for(std::size_t i = size; i < oldSize; ++i)
104  delete m_rings[i];
105  }
106 
107  m_rings.resize(size);
108 }
109 
111 {
112  assert(i < getNumInteriorRings());
113  return m_rings[i + 1];
114 }
115 
116 void te::gm::CurvePolygon::setRingN(std::size_t i, Curve* r)
117 {
118  assert(i < m_rings.size());
119  delete m_rings[i];
120  m_rings[i] = r;
121 }
122 
124 {
125  assert(i < m_rings.size());
126  delete m_rings[i];
127  m_rings.erase(m_rings.begin() + i);
128 }
129 
131 {
132  te::common::FreeContents(m_rings);
133  m_rings.clear();
134 }
135 
137 {
138 #ifdef TERRALIB_GEOS_ENABLED
139  std::auto_ptr<geos::geom::Geometry> g(GEOSWriter::write(this));
140 
141  return g->getArea();
142 
143 #else
144  throw Exception(TE_TR("getArea routine is supported by GEOS! Please, enable the GEOS support."));
145 #endif
146 }
147 
149 {
150 #ifdef TERRALIB_GEOS_ENABLED
151  std::auto_ptr<geos::geom::Geometry> thisGeom(GEOSWriter::write(this));
152 
153  geos::algorithm::CentroidArea c;
154 
155  c.add(thisGeom.get());
156 
157  geos::geom::Coordinate coord;
158 
159  if(c.getCentroid(coord))
160  {
161  Point* pt = new Point(coord.x, coord.y, m_srid, 0);
162 
163  return pt;
164  }
165 
166  return 0;
167 
168 #else
169  throw te::common::Exception(TE_TR("getCentroid routine is supported by GEOS! Please, enable the GEOS support."));
170 #endif
171 }
172 
174 {
175  te::gm::Point * p = getCentroid();
176 
177  if(p)
178  {
179  te::gm::Coord2D* coord = new te::gm::Coord2D(p->getX(), p->getY());
180 
181  delete p;
182 
183  return coord;
184  }
185 
186  return 0;
187 }
188 
190 {
191  return 0;
192 }
193 
195 {
196  return 0;
197 }
198 
200 {
201 #ifdef TERRALIB_GEOS_ENABLED
202  std::auto_ptr<geos::geom::Geometry> g(GEOSWriter::write(this));
203 
204  return g->getLength();
205 
206 #else
207  throw Exception(TE_TR("getLength routine is supported by GEOS! Please, enable the GEOS support."));
208 #endif
209 }
210 
211 const std::string& te::gm::CurvePolygon::getGeometryType() const throw()
212 {
213  return sm_typeName;
214 }
215 
216 void te::gm::CurvePolygon::setSRID(int srid) throw()
217 {
218  std::size_t n = m_rings.size();
219 
220  for(std::size_t i = 0; i < n; ++i)
221  m_rings[i]->setSRID(srid);
222 
223  m_srid = srid;
224 }
225 
227 {
228 #ifdef TERRALIB_MOD_SRS_ENABLED
229  if(srid == m_srid)
230  return;
231 
232  std::size_t n = m_rings.size();
233 
234  for(std::size_t i = 0; i < n; ++i)
235  m_rings[i]->transform(srid);
236 
237  if(m_mbr)
238  computeMBR(true); // just update the polygon MBR
239 
240  m_srid = srid;
241 #else
242  throw Exception(TE_TR("transform method is not supported!"));
243 #endif // TERRALIB_MOD_SRS_ENABLED
244 }
245 
246 void te::gm::CurvePolygon::computeMBR(bool cascade) const throw()
247 {
248  if(m_mbr == 0)
249  m_mbr = new Envelope;
250  else
251  m_mbr->makeInvalid();
252 
253  std::size_t n = m_rings.size();
254 
255  if(n == 0)
256  return;
257 
258  if(cascade)
259  m_rings[0]->computeMBR(true);
260 
261  *m_mbr = *(m_rings[0]->getMBR());
262 
263  if(cascade)
264  for(std::size_t i = 1; i < n; ++i)
265  m_rings[i]->computeMBR(true);
266 }
267 
268 std::size_t te::gm::CurvePolygon::getNPoints() const throw()
269 {
270  std::size_t n = m_rings.size();
271 
272  std::size_t sum = 0;
273 
274  for(std::size_t i = 0; i < n; ++i)
275  sum += m_rings[i]->getNPoints();
276 
277  return sum;
278 }
279 
virtual CurvePolygon & operator=(const CurvePolygon &rhs)
Assignment operator.
Curve * getInteriorRingN(std::size_t i) const
It returns the n-th interior ring for this curve polygon as a curve.
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 const std::string & getGeometryType() const
The name of the geometry subtype for curve polygons is: CurvePolygon.
void transform(int srid)
It converts the coordinate values of the geometry to the new spatial reference system.
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
Curve is an abstract class that represents 1-dimensional geometric objects stored as a sequence of co...
Definition: Curve.h:58
std::size_t getNumInteriorRings() const
It returns the number of interior rings in this CurvePolygon.
double getPerimeter() const
It returns the length of the boundary for the surface.
A point with x and y coordinate values.
Curve * getExteriorRing() const
It returns the exterior ring of this CurvePolygon.
void removeRingN(std::size_t i)
It removes the n-th ring in this CurvePolygon.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void setNumRings(std::size_t size)
It sets the number of rings in this curve polygon.
An Envelope defines a 2D rectangular region.
static const std::string sm_typeName
Definition: CurvePolygon.h:441
std::size_t getNPoints() const
it returns the number of points (vertexes) in the geometry.
const double & getY() const
It returns the Point y-coordinate value.
Definition: Point.h:150
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
CurvePolygon is a planar surface defined by 1 exterior boundary and 0 or more interior boundaries...
Point * getPointOnSurface() const
It returns a point guaranteed to be on this surface.
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...
CurvePolygon is a planar surface defined by 1 exterior boundary and 0 or more interior boundaries...
Definition: CurvePolygon.h:57
virtual ~CurvePolygon()
Virtual destructor.
virtual Surface & operator=(const Surface &rhs)
Assignment operator.
Definition: Surface.cpp:59
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
Coord2D * getCoordOnSurface() const
It returns a coordinate guaranteed to be on this surface.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Curve is an abstract class that represents 1-dimensional geometric objects stored as a sequence of co...
A class that converts a TerraLib geometry to a GEOS geometry.
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
void computeMBR(bool cascade) const
It computes the minimum bounding rectangle for the curve polygon.
Coord2D * getCentroidCoord() const
It returns the mathematical centroid for this surface as a coordinate.
Point * getCentroid() const
It returns the mathematical centroid for this surface as a point.
void clear()
It deletes all the rings of the CurvePolygon and clear it.
virtual te::dt::AbstractData * clone() const
It clones the linestring.
Configuration flags for the Vector Geometry Model of TerraLib.
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
An utility struct for representing 2D coordinates.
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
const double & getX() const
It returns the Point x-coordinate value.
Definition: Point.h:136
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.
Surface is an abstract class that represents a 2-dimensional geometric objects.
Definition: Surface.h:54
std::vector< Curve * > m_rings
An array with the ring list.
Definition: CurvePolygon.h:439