-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathgeom.hpp
More file actions
431 lines (343 loc) · 11.6 KB
/
geom.hpp
File metadata and controls
431 lines (343 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#ifndef OSM2PGSQL_GEOM_HPP
#define OSM2PGSQL_GEOM_HPP
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
/**
* \file
*
* Basic geometry types and functions.
*/
#include "projection.hpp"
#include <osmium/osm/location.hpp>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <initializer_list>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
namespace geom {
class nullgeom_t
{
public:
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
{
return 0;
}
[[nodiscard]] constexpr friend bool operator==(nullgeom_t,
nullgeom_t) noexcept
{
return true;
}
[[nodiscard]] constexpr friend bool operator!=(nullgeom_t,
nullgeom_t) noexcept
{
return false;
}
}; // class nullgeom_t
class point_t
{
public:
point_t() = default;
explicit point_t(osmium::Location location) noexcept
: m_x(location.lon_without_check()), m_y(location.lat_without_check())
{}
constexpr point_t(double x, double y) noexcept : m_x(x), m_y(y) {}
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
{
return 1;
}
[[nodiscard]] constexpr double x() const noexcept { return m_x; }
[[nodiscard]] constexpr double y() const noexcept { return m_y; }
constexpr void set_x(double value) noexcept { m_x = value; }
constexpr void set_y(double value) noexcept { m_y = value; }
[[nodiscard]] constexpr friend bool operator==(point_t a,
point_t b) noexcept
{
return a.x() == b.x() && a.y() == b.y();
}
[[nodiscard]] constexpr friend bool operator!=(point_t a,
point_t b) noexcept
{
return !(a == b);
}
/// Give points an (arbitrary) order.
[[nodiscard]] constexpr friend bool operator<(point_t a, point_t b) noexcept
{
if (a.x() == b.x()) {
return a.y() < b.y();
}
return a.x() < b.x();
}
[[nodiscard]] constexpr friend bool operator>(point_t a, point_t b) noexcept
{
if (a.x() == b.x()) {
return a.y() > b.y();
}
return a.x() > b.x();
}
private:
double m_x = 0.0;
double m_y = 0.0;
}; // class point_t
/**
* This type is used as the basis for linestrings and rings.
*
* Point lists should not contain consecutive duplicate points. You can
* use the remove_duplicates() function to remove them if needed. (OGC validity
* only requires there to be at least two different points in a linestring, but
* we are more strict here to make sure we don't have to handle that anomaly
* later on.)
*/
class point_list_t : public std::vector<point_t>
{
public:
point_list_t() = default;
template <typename Iterator>
point_list_t(Iterator begin, Iterator end)
: std::vector<point_t>(begin, end)
{}
point_list_t(std::initializer_list<point_t> list)
: std::vector<point_t>(list.begin(), list.end())
{}
/// Collapse consecutive identical points into a single point (in-place).
void remove_duplicates();
}; // class point_list_t
class linestring_t : public point_list_t
{
public:
using point_list_t::point_list_t;
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
{
return 1;
}
}; // class linestring_t
class ring_t : public point_list_t
{
public:
using point_list_t::point_list_t;
}; // class ring_t
class polygon_t
{
public:
polygon_t() = default;
explicit polygon_t(ring_t &&ring) : m_outer(std::move(ring)) {}
[[nodiscard]] constexpr static std::size_t num_geometries() noexcept
{
return 1;
}
[[nodiscard]] ring_t const &outer() const noexcept { return m_outer; }
[[nodiscard]] ring_t &outer() noexcept { return m_outer; }
[[nodiscard]] std::vector<ring_t> const &inners() const noexcept
{
return m_inners;
}
[[nodiscard]] std::vector<ring_t> &inners() noexcept { return m_inners; }
void add_inner_ring(ring_t &&ring) { m_inners.push_back(std::move(ring)); }
friend bool operator==(polygon_t const &a, polygon_t const &b) noexcept;
friend bool operator!=(polygon_t const &a, polygon_t const &b) noexcept;
private:
ring_t m_outer;
std::vector<ring_t> m_inners;
}; // class polygon_t
template <typename GEOM>
class multigeometry_t
{
public:
using const_iterator = typename std::vector<GEOM>::const_iterator;
using iterator = typename std::vector<GEOM>::iterator;
using value_type = GEOM;
static constexpr bool FOR_POINT = std::is_same_v<GEOM, point_t>;
[[nodiscard]] std::size_t num_geometries() const noexcept
{
return m_geometry.size();
}
GEOM &
add_geometry(typename std::conditional_t<FOR_POINT, point_t, GEOM &&> geom)
{
m_geometry.push_back(std::forward<GEOM>(geom));
return m_geometry.back();
}
[[nodiscard]] GEOM &add_geometry() { return m_geometry.emplace_back(); }
[[nodiscard]] friend bool operator==(multigeometry_t const &a,
multigeometry_t const &b)
{
return a.m_geometry == b.m_geometry;
}
[[nodiscard]] friend bool operator!=(multigeometry_t const &a,
multigeometry_t const &b)
{
return a.m_geometry != b.m_geometry;
}
iterator begin() noexcept { return m_geometry.begin(); }
iterator end() noexcept { return m_geometry.end(); }
const_iterator begin() const noexcept { return m_geometry.cbegin(); }
const_iterator end() const noexcept { return m_geometry.cend(); }
const_iterator cbegin() const noexcept { return m_geometry.cbegin(); }
const_iterator cend() const noexcept { return m_geometry.cend(); }
GEOM const &operator[](std::size_t n) const noexcept
{
return m_geometry[n];
}
GEOM &operator[](std::size_t n) noexcept
{
return m_geometry[n];
}
void remove_last()
{
assert(!m_geometry.empty());
m_geometry.pop_back();
}
void reserve(std::size_t size) { m_geometry.reserve(size); }
private:
std::vector<GEOM> m_geometry;
}; // class multigeometry_t
using multipoint_t = multigeometry_t<point_t>;
using multilinestring_t = multigeometry_t<linestring_t>;
using multipolygon_t = multigeometry_t<polygon_t>;
class geometry_t;
using collection_t = multigeometry_t<geometry_t>;
/**
* This is a variant type holding any one of the geometry types (including
* nullgeom_t) and a SRID.
*/
class geometry_t
{
public:
constexpr geometry_t() = default;
// point_t is small and trivially copyable, no move needed like for the
// other constructors.
constexpr explicit geometry_t(point_t geom, int srid = PROJ_LATLONG)
: m_geom(geom), m_srid(srid)
{}
constexpr explicit geometry_t(linestring_t &&geom, int srid = PROJ_LATLONG)
: m_geom(std::move(geom)), m_srid(srid)
{}
constexpr explicit geometry_t(polygon_t &&geom, int srid = PROJ_LATLONG)
: m_geom(std::move(geom)), m_srid(srid)
{}
constexpr explicit geometry_t(multipoint_t &&geom, int srid = PROJ_LATLONG)
: m_geom(std::move(geom)), m_srid(srid)
{}
constexpr explicit geometry_t(multilinestring_t &&geom,
int srid = PROJ_LATLONG)
: m_geom(std::move(geom)), m_srid(srid)
{}
constexpr explicit geometry_t(multipolygon_t &&geom,
int srid = PROJ_LATLONG)
: m_geom(std::move(geom)), m_srid(srid)
{}
constexpr explicit geometry_t(collection_t &&geom, int srid = PROJ_LATLONG)
: m_geom(std::move(geom)), m_srid(srid)
{}
[[nodiscard]] constexpr int srid() const noexcept { return m_srid; }
constexpr void set_srid(int srid) noexcept { m_srid = srid; }
[[nodiscard]] constexpr bool is_null() const noexcept
{
return std::holds_alternative<nullgeom_t>(m_geom);
}
[[nodiscard]] constexpr bool is_point() const noexcept
{
return std::holds_alternative<point_t>(m_geom);
}
[[nodiscard]] constexpr bool is_linestring() const noexcept
{
return std::holds_alternative<linestring_t>(m_geom);
}
[[nodiscard]] constexpr bool is_polygon() const noexcept
{
return std::holds_alternative<polygon_t>(m_geom);
}
[[nodiscard]] constexpr bool is_multipoint() const noexcept
{
return std::holds_alternative<multipoint_t>(m_geom);
}
[[nodiscard]] constexpr bool is_multilinestring() const noexcept
{
return std::holds_alternative<multilinestring_t>(m_geom);
}
[[nodiscard]] constexpr bool is_multipolygon() const noexcept
{
return std::holds_alternative<multipolygon_t>(m_geom);
}
[[nodiscard]] constexpr bool is_collection() const noexcept
{
return std::holds_alternative<collection_t>(m_geom);
}
[[nodiscard]] constexpr bool is_multi() const noexcept
{
return is_multipoint() || is_multilinestring() || is_multipolygon() ||
is_collection();
}
template <typename T>
[[nodiscard]] constexpr T const &get() const
{
return std::get<T>(m_geom);
}
template <typename T>
[[nodiscard]] constexpr T &get()
{
return std::get<T>(m_geom);
}
void reset() { m_geom.emplace<nullgeom_t>(); }
template <typename T>
constexpr T &set()
{
return m_geom.emplace<T>();
}
template <typename V>
constexpr auto visit(V &&visitor) const
{
return std::visit(std::forward<V>(visitor), m_geom);
}
// This is non-member function visit(), different than the member
// function above, because we need to move the geometry into the function
// which we can't do for a member function.
template <typename V>
friend auto visit(V &&visitor, geometry_t &&geom)
{
return std::visit(std::forward<V>(visitor), std::move(geom.m_geom));
}
[[nodiscard]] friend bool operator==(geometry_t const &a,
geometry_t const &b)
{
return (a.srid() == b.srid()) && (a.m_geom == b.m_geom);
}
[[nodiscard]] friend bool operator!=(geometry_t const &a,
geometry_t const &b)
{
return !(a == b);
}
private:
std::variant<nullgeom_t, point_t, linestring_t, polygon_t, multipoint_t,
multilinestring_t, multipolygon_t, collection_t>
m_geom = nullgeom_t{};
int m_srid = PROJ_LATLONG;
}; // class geometry_t
inline std::size_t dimension(nullgeom_t const &) noexcept { return 0; }
inline std::size_t dimension(point_t const &) noexcept { return 0; }
inline std::size_t dimension(linestring_t const &) noexcept { return 1; }
inline std::size_t dimension(polygon_t const &) noexcept { return 2; }
inline std::size_t dimension(multipoint_t const &) noexcept { return 0; }
inline std::size_t dimension(multilinestring_t const &) noexcept { return 1; }
inline std::size_t dimension(multipolygon_t const &) noexcept { return 2; }
std::size_t dimension(collection_t const &geom);
/**
* Return the dimension of this geometry. This is:
*
* 0 - for null and point geometries
* 1 - for (multi)linestring geometries
* 2 - for (multi)polygon geometries
*
* For geometry collections this is the largest dimension of all its members.
*/
std::size_t dimension(geometry_t const &geom);
} // namespace geom
#endif // OSM2PGSQL_GEOM_HPP