1#ifndef OSMIUM_GEOM_GEOS_HPP
2#define OSMIUM_GEOM_GEOS_HPP
36#include <geos/version.h>
37#if defined(GEOS_VERSION_MAJOR) && defined(GEOS_VERSION_MINOR) && (GEOS_VERSION_MAJOR < 3 || (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR <= 5))
39#define OSMIUM_WITH_GEOS
58#include <geos/geom/Coordinate.h>
59#include <geos/geom/CoordinateSequence.h>
60#include <geos/geom/CoordinateSequenceFactory.h>
61#include <geos/geom/GeometryFactory.h>
62#include <geos/geom/LinearRing.h>
63#include <geos/geom/MultiPolygon.h>
64#include <geos/geom/Point.h>
65#include <geos/geom/Polygon.h>
66#include <geos/geom/PrecisionModel.h>
67#include <geos/util/GEOSException.h>
81 struct geos_geometry_error :
public geometry_error {
83 explicit geos_geometry_error(
const char* message) :
84 geometry_error(
std::string{
"geometry creation failed in GEOS library: "} + message) {
94 class GEOSFactoryImpl {
96 std::unique_ptr<const geos::geom::PrecisionModel> m_precision_model;
97 std::unique_ptr<geos::geom::GeometryFactory> m_our_geos_factory;
98 geos::geom::GeometryFactory* m_geos_factory;
100 std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
101 std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
102 std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
106 using point_type = std::unique_ptr<geos::geom::Point>;
107 using linestring_type = std::unique_ptr<geos::geom::LineString>;
108 using polygon_type = std::unique_ptr<geos::geom::Polygon>;
109 using multipolygon_type = std::unique_ptr<geos::geom::MultiPolygon>;
110 using ring_type = std::unique_ptr<geos::geom::LinearRing>;
112 explicit GEOSFactoryImpl(
int , geos::geom::GeometryFactory& geos_factory) :
113 m_precision_model(nullptr),
114 m_our_geos_factory(nullptr),
115 m_geos_factory(&geos_factory) {
118 explicit GEOSFactoryImpl(
int srid) :
119 m_precision_model(new geos::geom::PrecisionModel),
120 m_our_geos_factory(new geos::geom::GeometryFactory{m_precision_model.get(), srid}),
121 m_geos_factory(m_our_geos_factory.get()) {
128 return point_type{m_geos_factory->createPoint(geos::geom::Coordinate{xy.
x, xy.
y})};
129 }
catch (
const geos::util::GEOSException& e) {
130 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
136 void linestring_start() {
138 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(
static_cast<std::size_t
>(0), 2));
139 }
catch (
const geos::util::GEOSException& e) {
140 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
146 m_coordinate_sequence->add(geos::geom::Coordinate{xy.
x, xy.
y});
147 }
catch (
const geos::util::GEOSException& e) {
148 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
152 linestring_type linestring_finish(std::size_t ) {
154 return linestring_type{m_geos_factory->createLineString(m_coordinate_sequence.release())};
155 }
catch (
const geos::util::GEOSException& e) {
156 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
162 void multipolygon_start() {
166 void multipolygon_polygon_start() {
170 void multipolygon_polygon_finish() {
172 assert(!m_rings.empty());
173 auto inner_rings =
new std::vector<geos::geom::Geometry*>;
174 std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
177 m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
179 }
catch (
const geos::util::GEOSException& e) {
180 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
184 void multipolygon_outer_ring_start() {
186 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(
static_cast<std::size_t
>(0), 2));
187 }
catch (
const geos::util::GEOSException& e) {
188 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
192 void multipolygon_outer_ring_finish() {
194 m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
195 }
catch (
const geos::util::GEOSException& e) {
196 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
200 void multipolygon_inner_ring_start() {
202 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(
static_cast<std::size_t
>(0), 2));
203 }
catch (
const geos::util::GEOSException& e) {
204 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
208 void multipolygon_inner_ring_finish() {
210 m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
211 }
catch (
const geos::util::GEOSException& e) {
212 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
218 m_coordinate_sequence->add(geos::geom::Coordinate{xy.
x, xy.
y});
219 }
catch (
const geos::util::GEOSException& e) {
220 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
224 multipolygon_type multipolygon_finish() {
226 auto polygons =
new std::vector<geos::geom::Geometry*>;
227 std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
231 return multipolygon_type{m_geos_factory->createMultiPolygon(polygons)};
232 }
catch (
const geos::util::GEOSException& e) {
233 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
242 template <
typename TProjection = IdentityProjection>
243 using GEOSFactory = GeometryFactory<osmium::geom::detail::GEOSFactoryImpl, TProjection>;
OSMIUM_DEPRECATED Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:125
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555
Definition: coordinates.hpp:48
double y
Definition: coordinates.hpp:51
double x
Definition: coordinates.hpp:50