All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GEOSWriter.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/GEOSWriter.cpp
22 
23  \brief A class that converts a TerraLib geometry to a GEOS geometry.
24 */
25 
26 // TerraLib
27 #include "GEOSWriter.h"
28 
29 #ifdef TERRALIB_GEOS_ENABLED
30 // TerraLib
31 #include "../common/Translator.h"
32 #include "Exception.h"
33 #include "GeometryCollection.h"
34 #include "GEOSGeometryFactory.h"
35 #include "LineString.h"
36 #include "LinearRing.h"
37 #include "MultiLineString.h"
38 #include "MultiPoint.h"
39 #include "MultiPolygon.h"
40 #include "Point.h"
41 #include "PointM.h"
42 #include "PointZ.h"
43 #include "PointZM.h"
44 #include "Polygon.h"
45 
46 // GEOS
47 #include <geos/geom/Coordinate.h>
48 #include <geos/geom/CoordinateArraySequence.h>
49 #include <geos/geom/GeometryCollection.h>
50 #include <geos/geom/LineString.h>
51 #include <geos/geom/LinearRing.h>
52 #include <geos/geom/MultiLineString.h>
53 #include <geos/geom/MultiPoint.h>
54 #include <geos/geom/MultiPolygon.h>
55 #include <geos/geom/Point.h>
56 #include <geos/geom/Polygon.h>
57 
58 // STL
59 #include <cassert>
60 
61 geos::geom::Geometry* te::gm::GEOSWriter::write(const Geometry* geom)
62 {
63  assert(geom);
64 
65  switch(geom->getGeomTypeId())
66  {
67  case PointType:
68  return write(static_cast<const Point*>(geom));
69 
70  case PointZType:
71  return write(static_cast<const PointZ*>(geom));
72 
73  case PointMType:
74  return write(static_cast<const PointM*>(geom));
75 
76  case PointZMType:
77  return write(static_cast<const PointZM*>(geom));
78 
79 
80  case LineStringType:
81  case LineStringZType:
82  case LineStringMType:
83  case LineStringZMType:
84  return write(static_cast<const LineString*>(geom));
85 
86  case PolygonType:
87  case PolygonZType:
88  case PolygonMType:
89  case PolygonZMType:
90  return write(static_cast<const Polygon*>(geom));
91 
92  case MultiPolygonType:
93  case MultiPolygonZType:
94  case MultiPolygonMType:
95  case MultiPolygonZMType:
96  return write(static_cast<const MultiPolygon*>(geom));
97 
98  case MultiPointType:
99  case MultiPointZType:
100  case MultiPointMType:
101  case MultiPointZMType:
102  return write(static_cast<const MultiPoint*>(geom));
103 
104  case MultiLineStringType:
108  return write(static_cast<const MultiLineString*>(geom));
109 
114  return write(static_cast<const GeometryCollection*>(geom));
115 
116  /*case TriangleType:
117  case TriangleZType:
118  case TriangleMType:
119  case TriangleZMType:
120  return write(static_cast<const Triangle*>(geom));*/
121 
122  /*case TINType:
123  case TINZType:
124  case TINMType:
125  case TINZMType:
126  return write(static_cast<const TIN*>(geom));*/
127 
128  /*case PolyhedralSurfaceType:
129  case PolyhedralSurfaceZType:
130  case PolyhedralSurfaceMType:
131  case PolyhedralSurfaceZMType:
132  return write(static_cast<const PolyhedralSurface*>(geom));*/
133 
134  default:
135  throw Exception(TE_TR("The type of informed geometry can not be converted from TerraLib to GEOS!"));
136  }
137 }
138 
139 geos::geom::Point* te::gm::GEOSWriter::write(const Point* tePt)
140 {
141  assert((tePt != 0) && (tePt->getGeomTypeId() == PointType));
142 
143  geos::geom::Coordinate c(tePt->getX(), tePt->getY());
144 
145  geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
146 
147  assert(p);
148 
149  p->setSRID(tePt->getSRID());
150 
151  return p;
152 }
153 
154 geos::geom::Point* te::gm::GEOSWriter::write(const PointM* tePt)
155 {
156  assert((tePt != 0) && (tePt->getGeomTypeId() == PointMType));
157 
158  geos::geom::Coordinate c(tePt->getX(), tePt->getY(), tePt->getM());
159 
160  geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
161 
162  assert(p);
163 
164  p->setSRID(tePt->getSRID());
165 
166  return p;
167 }
168 
169 geos::geom::Point* te::gm::GEOSWriter::write(const PointZ* tePt)
170 {
171  assert((tePt != 0) && (tePt->getGeomTypeId() == PointZType));
172 
173  geos::geom::Coordinate c(tePt->getX(), tePt->getY(), tePt->getZ());
174 
175  geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
176 
177  assert(p);
178 
179  p->setSRID(tePt->getSRID());
180 
181  return p;
182 }
183 
184 geos::geom::Point* te::gm::GEOSWriter::write(const PointZM* tePt)
185 {
186  assert((tePt != 0) && (tePt->getGeomTypeId() == PointZMType));
187 
188  geos::geom::Coordinate c(tePt->getX(), tePt->getY(), tePt->getZ());
189 
190  geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
191 
192  assert(p);
193 
194  p->setSRID(tePt->getSRID());
195 
196  return p;
197 }
198 
199 geos::geom::LineString* te::gm::GEOSWriter::write(const LineString* teLine)
200 {
201  geos::geom::CoordinateSequence* cl = getCoordinateSequence(teLine);
202 
203  geos::geom::LineString* ls = GEOSGeometryFactory::getGeomFactory()->createLineString(cl);
204 
205  ls->setSRID(teLine->getSRID());
206 
207  return ls;
208 }
209 
210 geos::geom::LinearRing* te::gm::GEOSWriter::write(const LinearRing* teRing)
211 {
212  geos::geom::CoordinateSequence* cl = getCoordinateSequence(teRing);
213 
214  geos::geom::LinearRing* r = GEOSGeometryFactory::getGeomFactory()->createLinearRing(cl);
215 
216  r->setSRID(teRing->getSRID());
217 
218  return r;
219 }
220 
221 geos::geom::Polygon* te::gm::GEOSWriter::write(const Polygon* tePoly)
222 {
223  assert((tePoly != 0) && (tePoly->getGeomTypeId() == PolygonType ||
224  tePoly->getGeomTypeId() == PolygonMType ||
225  tePoly->getGeomTypeId() == PolygonZType ||
226  tePoly->getGeomTypeId() == PolygonZMType));
227 
228  std::size_t n = tePoly->getNumRings();
229 
230  if(n == 0)
231  {
232  geos::geom::Polygon* poly = GEOSGeometryFactory::getGeomFactory()->createPolygon();
233 
234  poly->setSRID(tePoly->getSRID());
235 
236  return poly;
237  }
238 
239  std::vector<geos::geom::Geometry*>* holes = new std::vector<geos::geom::Geometry*>(n - 1);
240 
241  for(std::size_t i = 1; i < n; ++i)
242  (*holes)[i - 1] = write(static_cast<const LinearRing*>(tePoly->getRingN(i)));
243 
244  geos::geom::LinearRing* outer = write(static_cast<const LinearRing*>(tePoly->getRingN(0)));
245 
246  geos::geom::Polygon* poly = GEOSGeometryFactory::getGeomFactory()->createPolygon(outer, holes);
247 
248  poly->setSRID(tePoly->getSRID());
249 
250  return poly;
251 }
252 
253 geos::geom::MultiPolygon* te::gm::GEOSWriter::write(const MultiPolygon* teMPoly)
254 {
255  assert((teMPoly != 0) && (teMPoly->getGeomTypeId() == MultiPolygonType ||
256  teMPoly->getGeomTypeId() == MultiPolygonMType ||
257  teMPoly->getGeomTypeId() == MultiPolygonZType ||
258  teMPoly->getGeomTypeId() == MultiPolygonZMType));
259 
260  std::vector<geos::geom::Geometry*>* geoms = getGeometries(teMPoly);
261 
262  geos::geom::MultiPolygon* mpoly = GEOSGeometryFactory::getGeomFactory()->createMultiPolygon(geoms);
263 
264  assert(mpoly);
265 
266  mpoly->setSRID(teMPoly->getSRID());
267 
268  return mpoly;
269 }
270 
271 geos::geom::MultiLineString* te::gm::GEOSWriter::write(const MultiLineString* teMLine)
272 {
273  assert((teMLine != 0) && (teMLine->getGeomTypeId() == MultiLineStringType ||
274  teMLine->getGeomTypeId() == MultiLineStringMType ||
275  teMLine->getGeomTypeId() == MultiLineStringZType ||
276  teMLine->getGeomTypeId() == MultiLineStringZMType));
277 
278  std::vector<geos::geom::Geometry*>* geoms = getGeometries(teMLine);
279 
280  geos::geom::MultiLineString* mline = GEOSGeometryFactory::getGeomFactory()->createMultiLineString(geoms);
281 
282  assert(mline);
283 
284  mline->setSRID(teMLine->getSRID());
285 
286  return mline;
287 }
288 
289 geos::geom::MultiPoint* te::gm::GEOSWriter::write(const MultiPoint* teMPt)
290 {
291  assert((teMPt != 0) && (teMPt->getGeomTypeId() == MultiPointType ||
292  teMPt->getGeomTypeId() == MultiPointMType ||
293  teMPt->getGeomTypeId() == MultiPointZType ||
294  teMPt->getGeomTypeId() == MultiPointZMType));
295 
296  std::vector<geos::geom::Geometry*>* geoms = getGeometries(teMPt);
297 
298  geos::geom::MultiPoint* mpt = GEOSGeometryFactory::getGeomFactory()->createMultiPoint(geoms);
299 
300  assert(mpt);
301 
302  mpt->setSRID(teMPt->getSRID());
303 
304  return mpt;
305 }
306 
307 geos::geom::GeometryCollection* te::gm::GEOSWriter::write(const GeometryCollection* teGeomColl)
308 {
309  assert((teGeomColl != 0) && (teGeomColl->getGeomTypeId() == GeometryCollectionType ||
310  teGeomColl->getGeomTypeId() == GeometryCollectionMType ||
311  teGeomColl->getGeomTypeId() == GeometryCollectionZType ||
312  teGeomColl->getGeomTypeId() == GeometryCollectionZMType));
313 
314  std::vector<geos::geom::Geometry*>* geoms = getGeometries(teGeomColl);
315 
316  geos::geom::GeometryCollection* geomColl= GEOSGeometryFactory::getGeomFactory()->createGeometryCollection(geoms);
317 
318  assert(geomColl);
319 
320  geomColl->setSRID(teGeomColl->getSRID());
321 
322  return geomColl;
323 }
324 
325 geos::geom::CoordinateSequence* te::gm::GEOSWriter::getCoordinateSequence(const LineString* teLine)
326 {
327  assert((teLine != 0) && (teLine->getGeomTypeId() == LineStringType ||
328  teLine->getGeomTypeId() == LineStringMType ||
329  teLine->getGeomTypeId() == LineStringZType ||
330  teLine->getGeomTypeId() == LineStringZMType));
331 
332  const std::size_t nPts = teLine->size();
333 
334  std::vector<geos::geom::Coordinate>* geosCoords = new std::vector<geos::geom::Coordinate>(nPts);
335 
336  Coord2D* teCoords = teLine->getCoordinates();
337 
338  for(std::size_t i = 0; i < nPts; ++i)
339  {
340  (*geosCoords)[i].x = teCoords[i].x;
341  (*geosCoords)[i].y = teCoords[i].y;
342  }
343 
344  geos::geom::CoordinateSequence* cl = new geos::geom::CoordinateArraySequence(geosCoords);
345 
346  return cl;
347 }
348 
349 std::vector<geos::geom::Geometry*>* te::gm::GEOSWriter::getGeometries(const GeometryCollection* teGeomColl)
350 {
351  assert(teGeomColl);
352 
353  std::size_t size = teGeomColl->getNumGeometries();
354 
355  std::vector<geos::geom::Geometry*>* geoms = new std::vector<geos::geom::Geometry*>(size);
356 
357  for(std::size_t i = 0; i < size; ++i)
358  {
359  geos::geom::Geometry* g = write(teGeomColl->getGeometryN(i));
360 
361  assert(g);
362 
363  (*geoms)[i] = g;
364  }
365 
366  return geoms;
367 }
368 
369 #endif // TERRALIB_GEOS_ENABLED
370 
The global factory used by TerraLib in order to create GEOS geometries.
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.
MultiLineString is a MultiCurve whose elements are LineStrings.
MultiPolygon is a MultiSurface whose elements are Polygons.
A class that converts a TerraLib geometry to a GEOS geometry.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
LineString is a curve with linear interpolation between points.
A point with z-coordinate value.
A point with a z-coordinate value and an associated measurement.
A point with an associated measure.
It is a collection of other geometric objects.