1 #ifndef OSMIUM_GEOM_GEOS_HPP
2 #define OSMIUM_GEOM_GEOS_HPP
47 #include <geos/geom/Coordinate.h>
48 #include <geos/geom/CoordinateSequence.h>
49 #include <geos/geom/CoordinateSequenceFactory.h>
50 #include <geos/geom/GeometryFactory.h>
51 #include <geos/geom/LinearRing.h>
52 #include <geos/geom/MultiPolygon.h>
53 #include <geos/geom/Point.h>
54 #include <geos/geom/Polygon.h>
55 #include <geos/geom/PrecisionModel.h>
56 #include <geos/util/GEOSException.h>
65 # define THROW std::throw_with_nested
73 geometry_error(
"geometry creation failed in GEOS library, see nested exception for details") {
82 class GEOSFactoryImpl {
84 geos::geom::PrecisionModel m_precision_model;
85 geos::geom::GeometryFactory m_geos_factory;
87 std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
88 std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
89 std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
93 typedef std::unique_ptr<geos::geom::Point> point_type;
94 typedef std::unique_ptr<geos::geom::LineString> linestring_type;
95 typedef std::unique_ptr<geos::geom::Polygon> polygon_type;
96 typedef std::unique_ptr<geos::geom::MultiPolygon> multipolygon_type;
97 typedef std::unique_ptr<geos::geom::LinearRing> ring_type;
99 explicit GEOSFactoryImpl(
int srid = -1) :
101 m_geos_factory(&m_precision_model, srid) {
108 return point_type(m_geos_factory.createPoint(geos::geom::Coordinate(xy.
x, xy.
y)));
109 }
catch (geos::util::GEOSException&) {
116 void linestring_start() {
118 m_coordinate_sequence.reset(m_geos_factory.getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
119 }
catch (geos::util::GEOSException&) {
126 m_coordinate_sequence->add(geos::geom::Coordinate(xy.
x, xy.
y));
127 }
catch (geos::util::GEOSException&) {
132 linestring_type linestring_finish(
size_t ) {
134 return linestring_type(m_geos_factory.createLineString(m_coordinate_sequence.release()));
135 }
catch (geos::util::GEOSException&) {
142 void multipolygon_start() {
146 void multipolygon_polygon_start() {
150 void multipolygon_polygon_finish() {
152 assert(!m_rings.empty());
153 auto inner_rings =
new std::vector<geos::geom::Geometry*>;
154 std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
157 m_polygons.emplace_back(m_geos_factory.createPolygon(m_rings[0].release(), inner_rings));
159 }
catch (geos::util::GEOSException&) {
164 void multipolygon_outer_ring_start() {
166 m_coordinate_sequence.reset(m_geos_factory.getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
167 }
catch (geos::util::GEOSException&) {
172 void multipolygon_outer_ring_finish() {
174 m_rings.emplace_back(m_geos_factory.createLinearRing(m_coordinate_sequence.release()));
175 }
catch (geos::util::GEOSException&) {
180 void multipolygon_inner_ring_start() {
182 m_coordinate_sequence.reset(m_geos_factory.getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
183 }
catch (geos::util::GEOSException&) {
188 void multipolygon_inner_ring_finish() {
190 m_rings.emplace_back(m_geos_factory.createLinearRing(m_coordinate_sequence.release()));
191 }
catch (geos::util::GEOSException&) {
198 m_coordinate_sequence->add(geos::geom::Coordinate(xy.
x, xy.
y));
199 }
catch (geos::util::GEOSException&) {
204 multipolygon_type multipolygon_finish() {
206 auto polygons =
new std::vector<geos::geom::Geometry*>;
207 std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
211 return multipolygon_type(m_geos_factory.createMultiPolygon(polygons));
212 }
catch (geos::util::GEOSException&) {
221 template <
class TProjection = IdentityProjection>
230 #endif // OSMIUM_GEOM_GEOS_HPP
double y
Definition: coordinates.hpp:50
Definition: factory.hpp:117
#define THROW
Definition: geos.hpp:65
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Definition: coordinates.hpp:47
Definition: factory.hpp:57
geos_geometry_error()
Definition: geos.hpp:72
double x
Definition: coordinates.hpp:49