29 #ifdef TERRALIB_GEOS_ENABLED
31 #include "../common/Translator.h"
32 #include "Exception.h"
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>
61 geos::geom::Geometry* te::gm::GEOSWriter::write(
const Geometry* geom)
65 switch(geom->getGeomTypeId())
68 return write(static_cast<const Point*>(geom));
71 return write(static_cast<const PointZ*>(geom));
74 return write(static_cast<const PointM*>(geom));
77 return write(static_cast<const PointZM*>(geom));
84 return write(static_cast<const LineString*>(geom));
90 return write(static_cast<const Polygon*>(geom));
96 return write(static_cast<const MultiPolygon*>(geom));
102 return write(static_cast<const MultiPoint*>(geom));
108 return write(static_cast<const MultiLineString*>(geom));
114 return write(static_cast<const GeometryCollection*>(geom));
135 throw Exception(
TE_TR(
"The type of informed geometry can not be converted from TerraLib to GEOS!"));
139 geos::geom::Point* te::gm::GEOSWriter::write(
const Point* tePt)
141 assert((tePt != 0) && (tePt->getGeomTypeId() ==
PointType));
143 geos::geom::Coordinate c(tePt->getX(), tePt->getY());
145 geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
149 p->setSRID(tePt->getSRID());
154 geos::geom::Point* te::gm::GEOSWriter::write(
const PointM* tePt)
156 assert((tePt != 0) && (tePt->getGeomTypeId() ==
PointMType));
158 geos::geom::Coordinate c(tePt->getX(), tePt->getY(), tePt->getM());
160 geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
164 p->setSRID(tePt->getSRID());
169 geos::geom::Point* te::gm::GEOSWriter::write(
const PointZ* tePt)
171 assert((tePt != 0) && (tePt->getGeomTypeId() ==
PointZType));
173 geos::geom::Coordinate c(tePt->getX(), tePt->getY(), tePt->getZ());
175 geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
179 p->setSRID(tePt->getSRID());
184 geos::geom::Point* te::gm::GEOSWriter::write(
const PointZM* tePt)
186 assert((tePt != 0) && (tePt->getGeomTypeId() ==
PointZMType));
188 geos::geom::Coordinate c(tePt->getX(), tePt->getY(), tePt->getZ());
190 geos::geom::Point* p = GEOSGeometryFactory::getGeomFactory()->createPoint(c);
194 p->setSRID(tePt->getSRID());
199 geos::geom::LineString* te::gm::GEOSWriter::write(
const LineString* teLine)
201 geos::geom::CoordinateSequence* cl = getCoordinateSequence(teLine);
203 geos::geom::LineString* ls = GEOSGeometryFactory::getGeomFactory()->createLineString(cl);
205 ls->setSRID(teLine->getSRID());
210 geos::geom::LinearRing* te::gm::GEOSWriter::write(
const LinearRing* teRing)
212 geos::geom::CoordinateSequence* cl = getCoordinateSequence(teRing);
214 geos::geom::LinearRing* r = GEOSGeometryFactory::getGeomFactory()->createLinearRing(cl);
216 r->setSRID(teRing->getSRID());
221 geos::geom::Polygon* te::gm::GEOSWriter::write(
const Polygon* tePoly)
223 assert((tePoly != 0) && (tePoly->getGeomTypeId() ==
PolygonType ||
228 std::size_t n = tePoly->getNumRings();
232 geos::geom::Polygon* poly = GEOSGeometryFactory::getGeomFactory()->createPolygon();
234 poly->setSRID(tePoly->getSRID());
239 std::vector<geos::geom::Geometry*>* holes =
new std::vector<geos::geom::Geometry*>(n - 1);
241 for(std::size_t i = 1; i < n; ++i)
242 (*holes)[i - 1] = write(static_cast<const LinearRing*>(tePoly->getRingN(i)));
244 geos::geom::LinearRing* outer = write(static_cast<const LinearRing*>(tePoly->getRingN(0)));
246 geos::geom::Polygon* poly = GEOSGeometryFactory::getGeomFactory()->createPolygon(outer, holes);
248 poly->setSRID(tePoly->getSRID());
253 geos::geom::MultiPolygon* te::gm::GEOSWriter::write(
const MultiPolygon* teMPoly)
260 std::vector<geos::geom::Geometry*>* geoms = getGeometries(teMPoly);
262 geos::geom::MultiPolygon* mpoly = GEOSGeometryFactory::getGeomFactory()->createMultiPolygon(geoms);
266 mpoly->setSRID(teMPoly->getSRID());
271 geos::geom::MultiLineString* te::gm::GEOSWriter::write(
const MultiLineString* teMLine)
278 std::vector<geos::geom::Geometry*>* geoms = getGeometries(teMLine);
280 geos::geom::MultiLineString* mline = GEOSGeometryFactory::getGeomFactory()->createMultiLineString(geoms);
284 mline->setSRID(teMLine->getSRID());
289 geos::geom::MultiPoint* te::gm::GEOSWriter::write(
const MultiPoint* teMPt)
291 assert((teMPt != 0) && (teMPt->getGeomTypeId() ==
MultiPointType ||
296 std::vector<geos::geom::Geometry*>* geoms = getGeometries(teMPt);
298 geos::geom::MultiPoint* mpt = GEOSGeometryFactory::getGeomFactory()->createMultiPoint(geoms);
302 mpt->setSRID(teMPt->getSRID());
307 geos::geom::GeometryCollection* te::gm::GEOSWriter::write(
const GeometryCollection* teGeomColl)
314 std::vector<geos::geom::Geometry*>* geoms = getGeometries(teGeomColl);
316 geos::geom::GeometryCollection* geomColl= GEOSGeometryFactory::getGeomFactory()->createGeometryCollection(geoms);
320 geomColl->setSRID(teGeomColl->getSRID());
325 geos::geom::CoordinateSequence* te::gm::GEOSWriter::getCoordinateSequence(
const LineString* teLine)
327 assert((teLine != 0) && (teLine->getGeomTypeId() ==
LineStringType ||
332 const std::size_t nPts = teLine->size();
334 std::vector<geos::geom::Coordinate>* geosCoords =
new std::vector<geos::geom::Coordinate>(nPts);
336 Coord2D* teCoords = teLine->getCoordinates();
338 for(std::size_t i = 0; i < nPts; ++i)
340 (*geosCoords)[i].x = teCoords[i].x;
341 (*geosCoords)[i].y = teCoords[i].y;
344 geos::geom::CoordinateSequence* cl =
new geos::geom::CoordinateArraySequence(geosCoords);
349 std::vector<geos::geom::Geometry*>* te::gm::GEOSWriter::getGeometries(
const GeometryCollection* teGeomColl)
353 std::size_t size = teGeomColl->getNumGeometries();
355 std::vector<geos::geom::Geometry*>* geoms =
new std::vector<geos::geom::Geometry*>(size);
357 for(std::size_t i = 0; i < size; ++i)
359 geos::geom::Geometry* g = write(teGeomColl->getGeometryN(i));
369 #endif // TERRALIB_GEOS_ENABLED
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.
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.