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 "../core/translator/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 #include <geos/geom/Dimension.h>
54 
55 // STL
56 #include <cassert>
57 
58 te::gm::Geometry* te::gm::GEOSReader::read(const geos::geom::Geometry* geom)
59 {
60  assert(geom);
61 
62  switch(geom->getGeometryTypeId())
63  {
64  case geos::geom::GEOS_POINT:
65  return read(dynamic_cast<const geos::geom::Point*>(geom));
66 
67  case geos::geom::GEOS_LINESTRING:
68  return read(dynamic_cast<const geos::geom::LineString*>(geom));
69 
70  case geos::geom::GEOS_LINEARRING:
71  return read(dynamic_cast<const geos::geom::LinearRing*>(geom));
72 
73  case geos::geom::GEOS_POLYGON:
74  return read(dynamic_cast<const geos::geom::Polygon*>(geom));
75 
76  case geos::geom::GEOS_MULTIPOLYGON:
77  return read(dynamic_cast<const geos::geom::MultiPolygon*>(geom));
78 
79  case geos::geom::GEOS_MULTIPOINT:
80  return read(dynamic_cast<const geos::geom::MultiPoint*>(geom));
81 
82  case geos::geom::GEOS_MULTILINESTRING:
83  return read(dynamic_cast<const geos::geom::MultiLineString*>(geom));
84 
85  case geos::geom::GEOS_GEOMETRYCOLLECTION:
86  return read(dynamic_cast<const geos::geom::GeometryCollection*>(geom));
87 
88  default:
89  throw Exception(TE_TR("The type of informed geometry can not be converted from GEOS to TerraLib!"));
90  }
91 }
92 
93 te::gm::Point* te::gm::GEOSReader::read(const geos::geom::Point* geosPt)
94 {
95  assert((geosPt != nullptr) && (geosPt->getGeometryTypeId() == geos::geom::GEOS_POINT));
96 
97  te::gm::GeomType gtype;
98  Point* pt;
99  switch (geosPt->getCoordinateDimension())
100  {
101  case 3:
102  gtype = PointZType;
103  pt = new Point(geosPt->getSRID(), gtype);
104  pt->setX(geosPt->getX());
105  pt->setY(geosPt->getY());
106  pt->setZ(geosPt->getCoordinate()->z);
107  break;
108  case 4:
109  gtype = PointZMType;
110  pt = new Point(geosPt->getSRID(), gtype);
111  pt->setX(geosPt->getX());
112  pt->setY(geosPt->getY());
113  pt->setZ(geosPt->getCoordinate()->z);
114  pt->setM(geosPt->getCoordinate()->z);
115  break;
116  default:
117  gtype = PointType;
118  pt = new Point(geosPt->getX(), geosPt->getY(), geosPt->getSRID());
119  }
120 
121  return pt;
122 }
123 
124 te::gm::LineString* te::gm::GEOSReader::read(const geos::geom::LineString* geosLine)
125 {
126  assert((geosLine != nullptr) && (geosLine->getGeometryTypeId() == geos::geom::GEOS_LINESTRING));
127 
128  const geos::geom::CoordinateSequence* cs = geosLine->getCoordinatesRO();
129 
130  std::size_t size = cs->getSize();
131 
132  LineString* l = new LineString(size, LineStringType, geosLine->getSRID(), nullptr);
133 
134  for(std::size_t i = 0; i < size; ++i)
135  {
136  l->setX(i, cs->getX(i));
137  l->setY(i, cs->getY(i));
138  }
139 
140  return l;
141 }
142 
143 te::gm::LinearRing* te::gm::GEOSReader::read(const geos::geom::LinearRing* geosRing)
144 {
145  assert((geosRing != nullptr) && (geosRing->getGeometryTypeId() == geos::geom::GEOS_LINEARRING));
146 
147  const geos::geom::CoordinateSequence* cs = geosRing->getCoordinatesRO();
148 
149  std::size_t size = cs->getSize();
150 
151  te::gm::GeomType gtype;
152  switch (cs->getDimension())
153  {
154  case 3:
155  gtype = LineStringZType;
156  break;
157  case 4:
158  gtype = LineStringZMType;
159  break;
160  default:
161  gtype = LineStringType;
162  }
163 
164  LinearRing* r = new LinearRing(size, gtype, geosRing->getSRID(), nullptr);
165 
166  for(std::size_t i = 0; i < size; ++i)
167  {
168  r->setX(i, cs->getX(i));
169  r->setY(i, cs->getY(i));
170  if ((gtype & 0xF00) == 0x300)
171  r->setZ(i, cs->getOrdinate(i, 2)); //Z value
172  if ((gtype & 0xF00) == 0x700)
173  r->setM(i, cs->getOrdinate(i, 3)); //m value
174  }
175 
176  return r;
177 
178 }
179 
180 te::gm::Polygon* te::gm::GEOSReader::read(const geos::geom::Polygon* geosPoly)
181 {
182  assert((geosPoly != nullptr) && (geosPoly->getGeometryTypeId() == geos::geom::GEOS_POLYGON));
183 
184  if(geosPoly->isEmpty())
185  return new Polygon(0, PolygonType, geosPoly->getSRID());
186 
187  int coordDimension = geosPoly->getCoordinateDimension();
188 
189  te::gm::GeomType gtype;
190 
191  if (coordDimension == 3)
192  gtype = PolygonZType;
193  else if(coordDimension == 4)
194  gtype = PolygonZMType;
195  else
196  gtype = PolygonType;
197 
198  std::size_t holesSize = geosPoly->getNumInteriorRing();
199 
200  Polygon* poly = new Polygon(holesSize + 1, gtype, geosPoly->getSRID(), nullptr);
201 
202  LinearRing* r = read(static_cast<const geos::geom::LinearRing*>(geosPoly->getExteriorRing()));
203 
204  poly->setRingN(0, r);
205 
206  for(std::size_t i = 0; i < holesSize; ++i)
207  {
208  LinearRing* r = read(static_cast<const geos::geom::LinearRing*>(geosPoly->getInteriorRingN(i)));
209 
210  poly->setRingN(i + 1, r);
211  }
212 
213  return poly;
214 }
215 
216 te::gm::MultiPolygon* te::gm::GEOSReader::read(const geos::geom::MultiPolygon* geosMPoly)
217 {
218  assert((geosMPoly != nullptr) && (geosMPoly->getGeometryTypeId() == geos::geom::GEOS_MULTIPOLYGON));
219 
220  const std::size_t size = geosMPoly->getNumGeometries();
221 
222  MultiPolygon* mpoly = new MultiPolygon(size, MultiPolygonType, geosMPoly->getSRID(), nullptr);
223 
224  for(std::size_t i = 0; i < size; ++i)
225  mpoly->setGeometryN(i, read(geosMPoly->getGeometryN(i)));
226 
227  return mpoly;
228 }
229 
230 te::gm::MultiLineString* te::gm::GEOSReader::read(const geos::geom::MultiLineString* geosMLine)
231 {
232  assert((geosMLine != nullptr) && (geosMLine->getGeometryTypeId() == geos::geom::GEOS_MULTILINESTRING));
233 
234  const std::size_t size = geosMLine->getNumGeometries();
235 
236  MultiLineString* mline = new MultiLineString(size, MultiLineStringType, geosMLine->getSRID(), nullptr);
237 
238  for(std::size_t i = 0; i < size; ++i)
239  mline->setGeometryN(i, read(geosMLine->getGeometryN(i)));
240 
241  return mline;
242 }
243 
244 te::gm::MultiPoint* te::gm::GEOSReader::read(const geos::geom::MultiPoint* geosMPt)
245 {
246  assert((geosMPt != nullptr) && (geosMPt->getGeometryTypeId() == geos::geom::GEOS_MULTIPOINT));
247 
248  const std::size_t size = geosMPt->getNumGeometries();
249 
250  MultiPoint* mpt = new MultiPoint(size,MultiPointType, geosMPt->getSRID(), nullptr);
251 
252  for(std::size_t i = 0; i < size; ++i)
253  mpt->setGeometryN(i, read(geosMPt->getGeometryN(i)));
254 
255  return mpt;
256 }
257 
258 te::gm::GeometryCollection* te::gm::GEOSReader::read(const geos::geom::GeometryCollection* geosGeomColl)
259 {
260  assert((geosGeomColl != nullptr) && (geosGeomColl->getGeometryTypeId() == geos::geom::GEOS_GEOMETRYCOLLECTION));
261 
262  const std::size_t size = geosGeomColl->getNumGeometries();
263 
264  GeometryCollection* geomColl = new GeometryCollection(size, GeometryCollectionType, geosGeomColl->getSRID(), nullptr);
265 
266  for(std::size_t i = 0; i < size; ++i)
267  geomColl->setGeometryN(i, read(geosGeomColl->getGeometryN(i)));
268 
269  return geomColl;
270 }
271 
272 #endif // TERRALIB_GEOS_ENABLED
273 
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
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:242
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.
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.