All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GEOSReader.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/GEOSReader.cpp
22 
23  \brief A class that converts a GEOS geometry to a TerraLib geometry.
24 */
25 
26 // TerraLib
27 #include "GEOSReader.h"
28 
29 #ifdef TERRALIB_GEOS_ENABLED
30 // TerraLib
31 #include "../common/Translator.h"
32 #include "Exception.h"
33 #include "GeometryCollection.h"
34 #include "LineString.h"
35 #include "LinearRing.h"
36 #include "MultiLineString.h"
37 #include "MultiPoint.h"
38 #include "MultiPolygon.h"
39 #include "Point.h"
40 #include "Polygon.h"
41 
42 // GEOS
43 #include <geos/geom/Coordinate.h>
44 #include <geos/geom/CoordinateArraySequence.h>
45 #include <geos/geom/GeometryCollection.h>
46 #include <geos/geom/LineString.h>
47 #include <geos/geom/LinearRing.h>
48 #include <geos/geom/MultiLineString.h>
49 #include <geos/geom/MultiPoint.h>
50 #include <geos/geom/MultiPolygon.h>
51 #include <geos/geom/Point.h>
52 #include <geos/geom/Polygon.h>
53 
54 // STL
55 #include <cassert>
56 
57 te::gm::Geometry* te::gm::GEOSReader::read(const geos::geom::Geometry* geom)
58 {
59  assert(geom);
60 
61  switch(geom->getGeometryTypeId())
62  {
63  case geos::geom::GEOS_POINT:
64  return read(dynamic_cast<const geos::geom::Point*>(geom));
65 
66  case geos::geom::GEOS_LINESTRING:
67  return read(dynamic_cast<const geos::geom::LineString*>(geom));
68 
69  case geos::geom::GEOS_LINEARRING:
70  return read(dynamic_cast<const geos::geom::LinearRing*>(geom));
71 
72  case geos::geom::GEOS_POLYGON:
73  return read(dynamic_cast<const geos::geom::Polygon*>(geom));
74 
75  case geos::geom::GEOS_MULTIPOLYGON:
76  return read(dynamic_cast<const geos::geom::MultiPolygon*>(geom));
77 
78  case geos::geom::GEOS_MULTIPOINT:
79  return read(dynamic_cast<const geos::geom::MultiPoint*>(geom));
80 
81  case geos::geom::GEOS_MULTILINESTRING:
82  return read(dynamic_cast<const geos::geom::MultiLineString*>(geom));
83 
84  case geos::geom::GEOS_GEOMETRYCOLLECTION:
85  return read(dynamic_cast<const geos::geom::GeometryCollection*>(geom));
86 
87  default:
88  throw Exception(TE_TR("The type of informed geometry can not be converted from GEOS to TerraLib!"));
89  }
90 }
91 
92 te::gm::Point* te::gm::GEOSReader::read(const geos::geom::Point* geosPt)
93 {
94  assert((geosPt != 0) && (geosPt->getGeometryTypeId() == geos::geom::GEOS_POINT));
95 
96  Point* pt = new Point(geosPt->getX(), geosPt->getY(), geosPt->getSRID(), 0);
97 
98  return pt;
99 }
100 
101 te::gm::LineString* te::gm::GEOSReader::read(const geos::geom::LineString* geosLine)
102 {
103  assert((geosLine != 0) && (geosLine->getGeometryTypeId() == geos::geom::GEOS_LINESTRING));
104 
105  const geos::geom::CoordinateSequence* cs = geosLine->getCoordinatesRO();
106 
107  std::size_t size = cs->getSize();
108 
109  LineString* l = new LineString(size, LineStringType, geosLine->getSRID(), 0);
110 
111  for(std::size_t i = 0; i < size; ++i)
112  {
113  l->setX(i, cs->getX(i));
114  l->setY(i, cs->getY(i));
115  }
116 
117  return l;
118 }
119 
120 te::gm::LinearRing* te::gm::GEOSReader::read(const geos::geom::LinearRing* geosRing)
121 {
122  assert((geosRing != 0) && (geosRing->getGeometryTypeId() == geos::geom::GEOS_LINEARRING));
123 
124  const geos::geom::CoordinateSequence* cs = geosRing->getCoordinatesRO();
125 
126  std::size_t size = cs->getSize();
127 
128  LinearRing* r = new LinearRing(size, LineStringType, geosRing->getSRID(), 0);
129 
130  for(std::size_t i = 0; i < size; ++i)
131  {
132  r->setX(i, cs->getX(i));
133  r->setY(i, cs->getY(i));
134  }
135 
136  return r;
137 
138 }
139 
140 te::gm::Polygon* te::gm::GEOSReader::read(const geos::geom::Polygon* geosPoly)
141 {
142  assert((geosPoly != 0) && (geosPoly->getGeometryTypeId() == geos::geom::GEOS_POLYGON));
143 
144  if(geosPoly->isEmpty())
145  return new Polygon(0, PolygonType, geosPoly->getSRID());
146 
147  std::size_t holesSize = geosPoly->getNumInteriorRing();
148 
149  Polygon* poly = new Polygon(holesSize + 1, PolygonType, geosPoly->getSRID(), 0);
150 
151  LinearRing* r = read(static_cast<const geos::geom::LinearRing*>(geosPoly->getExteriorRing()));
152 
153  poly->setRingN(0, r);
154 
155  for(std::size_t i = 0; i < holesSize; ++i)
156  {
157  LinearRing* r = read(static_cast<const geos::geom::LinearRing*>(geosPoly->getInteriorRingN(i)));
158 
159  poly->setRingN(i + 1, r);
160  }
161 
162  return poly;
163 }
164 
165 te::gm::MultiPolygon* te::gm::GEOSReader::read(const geos::geom::MultiPolygon* geosMPoly)
166 {
167  assert((geosMPoly != 0) && (geosMPoly->getGeometryTypeId() == geos::geom::GEOS_MULTIPOLYGON));
168 
169  const std::size_t size = geosMPoly->getNumGeometries();
170 
171  MultiPolygon* mpoly = new MultiPolygon(size, MultiPolygonType, geosMPoly->getSRID(), 0);
172 
173  for(std::size_t i = 0; i < size; ++i)
174  mpoly->setGeometryN(i, read(geosMPoly->getGeometryN(i)));
175 
176  return mpoly;
177 }
178 
179 te::gm::MultiLineString* te::gm::GEOSReader::read(const geos::geom::MultiLineString* geosMLine)
180 {
181  assert((geosMLine != 0) && (geosMLine->getGeometryTypeId() == geos::geom::GEOS_MULTILINESTRING));
182 
183  const std::size_t size = geosMLine->getNumGeometries();
184 
185  MultiLineString* mline = new MultiLineString(size, MultiLineStringType, geosMLine->getSRID(), 0);
186 
187  for(std::size_t i = 0; i < size; ++i)
188  mline->setGeometryN(i, read(geosMLine->getGeometryN(i)));
189 
190  return mline;
191 }
192 
193 te::gm::MultiPoint* te::gm::GEOSReader::read(const geos::geom::MultiPoint* geosMPt)
194 {
195  assert((geosMPt != 0) && (geosMPt->getGeometryTypeId() == geos::geom::GEOS_MULTIPOINT));
196 
197  const std::size_t size = geosMPt->getNumGeometries();
198 
199  MultiPoint* mpt = new MultiPoint(size,MultiPointType, geosMPt->getSRID(), 0);
200 
201  for(std::size_t i = 0; i < size; ++i)
202  mpt->setGeometryN(i, read(geosMPt->getGeometryN(i)));
203 
204  return mpt;
205 }
206 
207 te::gm::GeometryCollection* te::gm::GEOSReader::read(const geos::geom::GeometryCollection* geosGeomColl)
208 {
209  assert((geosGeomColl != 0) && (geosGeomColl->getGeometryTypeId() == geos::geom::GEOS_GEOMETRYCOLLECTION));
210 
211  const std::size_t size = geosGeomColl->getNumGeometries();
212 
213  GeometryCollection* geomColl = new GeometryCollection(size, GeometryCollectionType, geosGeomColl->getSRID(), 0);
214 
215  for(std::size_t i = 0; i < size; ++i)
216  geomColl->setGeometryN(i, read(geosGeomColl->getGeometryN(i)));
217 
218  return geomColl;
219 }
220 
221 #endif // TERRALIB_GEOS_ENABLED
222 
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
A LinearRing is a LineString that is both closed and simple.
A point with x and y coordinate values.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
MultiPoint is a GeometryCollection whose elements are restricted to points.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
A point with x and y coordinate values.
Definition: Point.h:50
MultiLineString is a MultiCurve whose elements are LineStrings.
MultiPolygon is a MultiSurface whose elements are Polygons.
A class that converts a GEOS geometry to a TerraLib geometry.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
MultiLineString is a MultiCurve whose elements are LineStrings.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
LineString is a curve with linear interpolation between points.
It is a collection of other geometric objects.
It is a collection of other geometric objects.