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> 68 # define THROW std::throw_with_nested 76 geometry_error(
std::string(
"geometry creation failed in GEOS library: ") + message) {
85 class GEOSFactoryImpl {
87 std::unique_ptr<const geos::geom::PrecisionModel> m_precision_model;
88 std::unique_ptr<geos::geom::GeometryFactory> m_our_geos_factory;
89 geos::geom::GeometryFactory* m_geos_factory;
91 std::unique_ptr<geos::geom::CoordinateSequence> m_coordinate_sequence;
92 std::vector<std::unique_ptr<geos::geom::LinearRing>> m_rings;
93 std::vector<std::unique_ptr<geos::geom::Polygon>> m_polygons;
97 using point_type = std::unique_ptr<geos::geom::Point>;
98 using linestring_type = std::unique_ptr<geos::geom::LineString>;
99 using polygon_type = std::unique_ptr<geos::geom::Polygon>;
100 using multipolygon_type = std::unique_ptr<geos::geom::MultiPolygon>;
101 using ring_type = std::unique_ptr<geos::geom::LinearRing>;
103 explicit GEOSFactoryImpl(
int , geos::geom::GeometryFactory& geos_factory) :
104 m_precision_model(
nullptr),
105 m_our_geos_factory(
nullptr),
106 m_geos_factory(&geos_factory) {
114 m_precision_model(
new geos::geom::PrecisionModel),
115 m_our_geos_factory(
new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
116 m_geos_factory(m_our_geos_factory.get()) {
119 explicit GEOSFactoryImpl(
int srid) :
120 m_precision_model(
new geos::geom::PrecisionModel),
121 m_our_geos_factory(
new geos::geom::GeometryFactory(m_precision_model.get(), srid)),
122 m_geos_factory(m_our_geos_factory.get()) {
129 return point_type(m_geos_factory->createPoint(geos::geom::Coordinate(xy.
x, xy.
y)));
130 }
catch (geos::util::GEOSException& e) {
137 void linestring_start() {
139 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
140 }
catch (geos::util::GEOSException& e) {
147 m_coordinate_sequence->add(geos::geom::Coordinate(xy.
x, xy.
y));
148 }
catch (geos::util::GEOSException& e) {
153 linestring_type linestring_finish(
size_t ) {
155 return linestring_type(m_geos_factory->createLineString(m_coordinate_sequence.release()));
156 }
catch (geos::util::GEOSException& e) {
163 void multipolygon_start() {
167 void multipolygon_polygon_start() {
171 void multipolygon_polygon_finish() {
173 assert(!m_rings.empty());
174 auto inner_rings =
new std::vector<geos::geom::Geometry*>;
175 std::transform(std::next(m_rings.begin(), 1), m_rings.end(), std::back_inserter(*inner_rings), [](std::unique_ptr<geos::geom::LinearRing>& r) {
178 m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
180 }
catch (geos::util::GEOSException& e) {
185 void multipolygon_outer_ring_start() {
187 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
188 }
catch (geos::util::GEOSException& e) {
193 void multipolygon_outer_ring_finish() {
195 m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
196 }
catch (geos::util::GEOSException& e) {
201 void multipolygon_inner_ring_start() {
203 m_coordinate_sequence.reset(m_geos_factory->getCoordinateSequenceFactory()->create(static_cast<size_t>(0), 2));
204 }
catch (geos::util::GEOSException& e) {
209 void multipolygon_inner_ring_finish() {
211 m_rings.emplace_back(m_geos_factory->createLinearRing(m_coordinate_sequence.release()));
212 }
catch (geos::util::GEOSException& e) {
219 m_coordinate_sequence->add(geos::geom::Coordinate(xy.
x, xy.
y));
220 }
catch (geos::util::GEOSException& e) {
225 multipolygon_type multipolygon_finish() {
227 auto polygons =
new std::vector<geos::geom::Geometry*>;
228 std::transform(m_polygons.begin(), m_polygons.end(), std::back_inserter(*polygons), [](std::unique_ptr<geos::geom::Polygon>& p) {
232 return multipolygon_type(m_geos_factory->createMultiPolygon(polygons));
233 }
catch (geos::util::GEOSException& e) {
242 template <
typename TProjection = IdentityProjection>
251 #endif // OSMIUM_GEOM_GEOS_HPP double y
Definition: coordinates.hpp:49
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
Definition: factory.hpp:146
#define THROW
Definition: geos.hpp:68
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:75
Definition: coordinates.hpp:46
double x
Definition: coordinates.hpp:48