Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
geos.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_GEOM_GEOS_HPP
2#define OSMIUM_GEOM_GEOS_HPP
3
4/*
5
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
9
10 Boost Software License - Version 1.0 - August 17th, 2003
11
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33
34*/
35
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))
38
39#define OSMIUM_WITH_GEOS
40
55#include <osmium/geom/coordinates.hpp>
56#include <osmium/geom/factory.hpp>
57
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>
68
69#include <algorithm>
70#include <cassert>
71#include <cstddef>
72#include <exception>
73#include <iterator>
74#include <memory>
75#include <string>
76#include <utility>
77#include <vector>
78
79 namespace osmium {
80
81 struct geos_geometry_error : public geometry_error {
82
83 explicit geos_geometry_error(const char* message) :
84 geometry_error(std::string{"geometry creation failed in GEOS library: "} + message) {
85 }
86
87 }; // struct geos_geometry_error
88
89 namespace geom {
90
91 namespace detail {
92
94 class GEOSFactoryImpl {
95
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;
99
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;
103
104 public:
105
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>;
111
112 explicit GEOSFactoryImpl(int /* srid */, geos::geom::GeometryFactory& geos_factory) :
113 m_precision_model(nullptr),
114 m_our_geos_factory(nullptr),
115 m_geos_factory(&geos_factory) {
116 }
117
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()) {
122 }
123
124 /* Point */
125
126 point_type make_point(const osmium::geom::Coordinates& xy) const {
127 try {
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()));
131 }
132 }
133
134 /* LineString */
135
136 void linestring_start() {
137 try {
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()));
141 }
142 }
143
144 void linestring_add_location(const osmium::geom::Coordinates& xy) {
145 try {
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()));
149 }
150 }
151
152 linestring_type linestring_finish(std::size_t /* num_points */) {
153 try {
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()));
157 }
158 }
159
160 /* MultiPolygon */
161
162 void multipolygon_start() {
163 m_polygons.clear();
164 }
165
166 void multipolygon_polygon_start() {
167 m_rings.clear();
168 }
169
170 void multipolygon_polygon_finish() {
171 try {
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) {
175 return r.release();
176 });
177 m_polygons.emplace_back(m_geos_factory->createPolygon(m_rings[0].release(), inner_rings));
178 m_rings.clear();
179 } catch (const geos::util::GEOSException& e) {
180 std::throw_with_nested(osmium::geos_geometry_error(e.what()));
181 }
182 }
183
184 void multipolygon_outer_ring_start() {
185 try {
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()));
189 }
190 }
191
192 void multipolygon_outer_ring_finish() {
193 try {
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()));
197 }
198 }
199
200 void multipolygon_inner_ring_start() {
201 try {
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()));
205 }
206 }
207
208 void multipolygon_inner_ring_finish() {
209 try {
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()));
213 }
214 }
215
216 void multipolygon_add_location(const osmium::geom::Coordinates& xy) {
217 try {
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()));
221 }
222 }
223
224 multipolygon_type multipolygon_finish() {
225 try {
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) {
228 return p.release();
229 });
230 m_polygons.clear();
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()));
234 }
235 }
236
237 }; // class GEOSFactoryImpl
238
239 } // namespace detail
240
242 template <typename TProjection = IdentityProjection>
243 using GEOSFactory = GeometryFactory<osmium::geom::detail::GEOSFactoryImpl, TProjection>;
244
245 } // namespace geom
246
247} // namespace osmium
248
249#endif
250
251#endif // OSMIUM_GEOM_GEOS_HPP
coordinates.hpp
factory.hpp
detail
Definition attr.hpp:342
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555
osmium::geom::Coordinates
Definition coordinates.hpp:48
osmium::geom::Coordinates::y
double y
Definition coordinates.hpp:51
osmium::geom::Coordinates::x
double x
Definition coordinates.hpp:50