forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeom.cpp
More file actions
295 lines (252 loc) · 8.83 KB
/
Copy pathgeom.cpp
File metadata and controls
295 lines (252 loc) · 8.83 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
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2021 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "geom.hpp"
#include "osmtypes.hpp"
#include <osmium/osm/way.hpp>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <tuple>
namespace geom {
double distance(osmium::geom::Coordinates p1,
osmium::geom::Coordinates p2) noexcept
{
double const dx = p1.x - p2.x;
double const dy = p1.y - p2.y;
return std::sqrt(dx * dx + dy * dy);
}
osmium::geom::Coordinates interpolate(osmium::geom::Coordinates p1,
osmium::geom::Coordinates p2,
double frac) noexcept
{
return osmium::geom::Coordinates{frac * (p1.x - p2.x) + p2.x,
frac * (p1.y - p2.y) + p2.y};
}
linestring_t::linestring_t(
std::initializer_list<osmium::geom::Coordinates> coords)
{
std::copy(coords.begin(), coords.end(), std::back_inserter(m_coordinates));
}
linestring_t::linestring_t(osmium::NodeRefList const &nodes,
reprojection const &proj)
{
osmium::Location last{};
for (auto const &node : nodes) {
auto const loc = node.location();
if (loc.valid() && loc != last) {
add_point(proj.reproject(loc));
last = loc;
}
}
if (size() <= 1) {
m_coordinates.clear();
}
}
void split_linestring(linestring_t const &line, double split_at,
std::vector<linestring_t> *out)
{
double dist = 0;
osmium::geom::Coordinates prev_pt{};
out->emplace_back();
for (auto const &this_pt : line) {
if (prev_pt.valid()) {
double const delta = distance(prev_pt, this_pt);
// figure out if the addition of this point would take the total
// length of the line in `segment` over the `split_at` distance.
if (dist + delta > split_at) {
auto const splits =
(size_t)std::floor((dist + delta) / split_at);
// use the splitting distance to split the current segment up
// into as many parts as necessary to keep each part below
// the `split_at` distance.
osmium::geom::Coordinates ipoint;
for (size_t j = 0; j < splits; ++j) {
double const frac =
((double)(j + 1) * split_at - dist) / delta;
ipoint = interpolate(this_pt, prev_pt, frac);
if (frac != 0.0) {
out->back().add_point(ipoint);
}
// start a new segment
out->emplace_back();
out->back().add_point(ipoint);
}
// reset the distance based on the final splitting point for
// the next iteration.
if (this_pt == ipoint) {
dist = 0;
prev_pt = this_pt;
continue;
}
dist = distance(this_pt, ipoint);
} else {
dist += delta;
}
}
out->back().add_point(this_pt);
prev_pt = this_pt;
}
if (out->back().size() <= 1) {
out->pop_back();
}
}
void make_line(linestring_t &&line, double split_at,
std::vector<linestring_t> *out)
{
assert(out);
if (line.empty()) {
return;
}
if (split_at > 0.0) {
split_linestring(line, split_at, out);
} else {
out->emplace_back(std::move(line));
}
}
void make_multiline(osmium::memory::Buffer const &ways, double split_at,
reprojection const &proj, std::vector<linestring_t> *out)
{
// make a list of all endpoints
struct endpoint_t {
osmid_t id;
std::size_t n;
bool is_front;
endpoint_t(osmid_t ref, std::size_t size, bool front) noexcept
: id(ref), n(size), is_front(front)
{}
bool operator==(endpoint_t const &rhs) const noexcept
{
return id == rhs.id;
}
};
std::vector<endpoint_t> endpoints;
// and a list of way connections
enum lmt : size_t
{
NOCONN = -1UL
};
struct connection_t {
std::size_t left = NOCONN;
osmium::Way const *way;
std::size_t right = NOCONN;
explicit connection_t(osmium::Way const *w) noexcept : way(w) {}
};
std::vector<connection_t> conns;
// initialise the two lists
for (auto const &w : ways.select<osmium::Way>()) {
if (w.nodes().size() > 1) {
endpoints.emplace_back(w.nodes().front().ref(), conns.size(), true);
endpoints.emplace_back(w.nodes().back().ref(), conns.size(), false);
conns.emplace_back(&w);
}
}
// sort by node id
std::sort(endpoints.begin(), endpoints.end(), [
](endpoint_t const &a, endpoint_t const &b) noexcept {
return std::tuple<osmid_t, std::size_t, bool>(a.id, a.n, a.is_front) <
std::tuple<osmid_t, std::size_t, bool>(b.id, b.n, b.is_front);
});
// now fill the connection list based on the sorted list
for (auto it = std::adjacent_find(endpoints.cbegin(), endpoints.cend());
it != endpoints.cend();
it = std::adjacent_find(it + 2, endpoints.cend())) {
auto const previd = it->n;
auto const ptid = std::next(it)->n;
if (it->is_front) {
conns[previd].left = ptid;
} else {
conns[previd].right = ptid;
}
if (std::next(it)->is_front) {
conns[ptid].left = previd;
} else {
conns[ptid].right = previd;
}
}
// First find all open ends and use them as starting points to assemble
// linestrings. Mark ways as "done" as we go.
std::size_t done_ways = 0;
std::size_t const todo_ways = conns.size();
for (std::size_t i = 0; i < todo_ways; ++i) {
if (!conns[i].way ||
(conns[i].left != NOCONN && conns[i].right != NOCONN)) {
continue; // way already done or not the beginning of a segment
}
linestring_t linestring;
{
std::size_t prev = NOCONN;
std::size_t cur = i;
do {
auto &conn = conns[cur];
assert(conn.way);
auto const &nl = conn.way->nodes();
bool const forward = conn.left == prev;
prev = cur;
// add way nodes
if (forward) {
add_nodes_to_linestring(linestring, proj, nl.cbegin(),
nl.cend());
cur = conn.right;
} else {
add_nodes_to_linestring(linestring, proj, nl.crbegin(),
nl.crend());
cur = conn.left;
}
// mark way as done
conns[prev].way = nullptr;
++done_ways;
} while (cur != NOCONN);
}
// found a line end, create the wkbs
make_line(std::move(linestring), split_at, out);
}
// If all ways have been "done", i.e. are part of a linestring now, we
// are finished.
if (done_ways >= todo_ways) {
return;
}
// oh dear, there must be circular ways without an end
// need to do the same shebang again
for (size_t i = 0; i < todo_ways; ++i) {
if (!conns[i].way) {
continue; // way already done
}
linestring_t linestring;
{
size_t prev = conns[i].left;
size_t cur = i;
do {
auto &conn = conns[cur];
assert(conn.way);
auto const &nl = conn.way->nodes();
bool const forward =
(conn.left == prev &&
(!conns[conn.left].way ||
conns[conn.left].way->nodes().back() == nl.front()));
prev = cur;
if (forward) {
// add way forwards
add_nodes_to_linestring(linestring, proj, nl.cbegin(),
nl.cend());
cur = conn.right;
} else {
// add way backwards
add_nodes_to_linestring(linestring, proj, nl.crbegin(),
nl.crend());
cur = conn.left;
}
// mark way as done
conns[prev].way = nullptr;
} while (cur != i);
}
// found a line end, create the wkbs
make_line(std::move(linestring), split_at, out);
}
}
} // namespace geom