1 #ifndef OSMIUM_GEOM_GEOS_HPP
2 #define OSMIUM_GEOM_GEOS_HPP
49 #include <geos/geom/Coordinate.h>
50 #include <geos/geom/CoordinateSequence.h>
51 #include <geos/geom/CoordinateSequenceFactory.h>
52 #include <geos/geom/GeometryFactory.h>
53 #include <geos/geom/LinearRing.h>
54 #include <geos/geom/MultiPolygon.h>
55 #include <geos/geom/Point.h>
56 #include <geos/geom/Polygon.h>
57 #include <geos/geom/PrecisionModel.h>
58 #include <geos/util/GEOSException.h>
67 # define THROW std::throw_with_nested
75 geometry_error(
std::string(
"geometry creation failed in GEOS library: ") + message) {
84 class GEOSFactoryImpl {
86 std::unique_ptr<const geos::geom::PrecisionModel> m_precision_model;
87 std::unique_ptr<geos::geom::GeometryFactory> m_our_geos_factory;
88 geos::geom::GeometryFactory* m_geos_factory;
90 std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
91 std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
92 std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
96 using point_type = std::unique_ptr<geos::geom::Point>;
97 using linestring_type = std::unique_ptr<geos::geom::LineString>;
98 using polygon_type = std::unique_ptr<geos::geom::Polygon>;
99 using multipolygon_type = std::unique_ptr<geos::geom::MultiPolygon>;
100 using ring_type = std::unique_ptr<geos::geom::LinearRing>;
102 explicit GEOSFactoryImpl(geos::geom::GeometryFactory& geos_factory) :
103 m_precision_model(nullptr),
104 m_our_geos_factory(nullptr),
105 m_geos_factory(&geos_factory) {
108 explicit GEOSFactoryImpl(
int srid = -1) :
109 m_precision_model(new geos::geom::PrecisionModel),
110 m_our_geos_factory(new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
111 m_geos_factory(m_our_geos_factory.get()) {
118 return point_type(m_geos_factory->createPoint(geos::geom::Coordinate(xy.
x, xy.
y)));
119 }
catch (geos::util::GEOSException& e) {
126 void linestring_start() {
128 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
129 }
catch (geos::util::GEOSException& e) {
136 m_coordinate_sequence->add(geos::geom::Coordinate(xy.
x, xy.
y));
137 }
catch (geos::util::GEOSException& e) {
142 linestring_type linestring_finish(
size_t ) {
144 return linestring_type(m_geos_factory->createLineString(m_coordinate_sequence.release()));
145 }
catch (geos::util::GEOSException& e) {
152 void multipolygon_start() {
156 void multipolygon_polygon_start() {
160 void multipolygon_polygon_finish() {
162 assert(!m_rings.empty());
163 auto inner_rings =
new std::vector<geos::geom::Geometry*>;
164 std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
167 m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
169 }
catch (geos::util::GEOSException& e) {
174 void multipolygon_outer_ring_start() {
176 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
177 }
catch (geos::util::GEOSException& e) {
182 void multipolygon_outer_ring_finish() {
184 m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
185 }
catch (geos::util::GEOSException& e) {
190 void multipolygon_inner_ring_start() {
192 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
193 }
catch (geos::util::GEOSException& e) {
198 void multipolygon_inner_ring_finish() {
200 m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
201 }
catch (geos::util::GEOSException& e) {
208 m_coordinate_sequence->add(geos::geom::Coordinate(xy.
x, xy.
y));
209 }
catch (geos::util::GEOSException& e) {
214 multipolygon_type multipolygon_finish() {
216 auto polygons =
new std::vector<geos::geom::Geometry*>;
217 std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
221 return multipolygon_type(m_geos_factory->createMultiPolygon(polygons));
222 }
catch (geos::util::GEOSException& e) {
231 template <
typename TProjection = IdentityProjection>
240 #endif // OSMIUM_GEOM_GEOS_HPP
double y
Definition: coordinates.hpp:49
Definition: factory.hpp:146
#define THROW
Definition: geos.hpp:67
Definition: reader_iterator.hpp:39
Coordinates transform(const CRS &src, const CRS &dest, Coordinates c)
Definition: projection.hpp:109
Definition: factory.hpp:57
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
geos_geometry_error(const char *message)
Definition: geos.hpp:74
Definition: coordinates.hpp:46
double x
Definition: coordinates.hpp:48