-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathexpire-tiles.cpp
More file actions
345 lines (293 loc) · 11.5 KB
/
expire-tiles.cpp
File metadata and controls
345 lines (293 loc) · 11.5 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
/**
* 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.
*/
#include "expire-tiles.hpp"
#include "format.hpp"
#include "geom-functions.hpp"
#include "options.hpp"
#include "projection.hpp"
#include "reprojection.hpp"
#include "tile.hpp"
#include "wkb.hpp"
#include <algorithm>
#include <cerrno>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <vector>
expire_tiles_t::expire_tiles_t(uint32_t max_zoom,
std::shared_ptr<reprojection_t> projection,
std::size_t max_tiles_geometry)
: m_projection(std::move(projection)), m_max_tiles_geometry(max_tiles_geometry),
m_maxzoom(max_zoom), m_map_width(static_cast<int>(1U << m_maxzoom))
{
}
void expire_tiles_t::expire_tile(uint32_t x, uint32_t y)
{
if (m_dirty_tiles.size() > m_max_tiles_geometry) {
return;
}
tile_t const new_tile{m_maxzoom, x, y};
m_dirty_tiles.insert(new_tile.quadkey());
}
void expire_tiles_t::commit_tiles(expire_output_t *expire_output)
{
if (!expire_output || m_dirty_tiles.empty()) {
return;
}
expire_output->add_tiles(m_dirty_tiles);
m_dirty_tiles.clear();
}
uint32_t expire_tiles_t::normalise_tile_x_coord(int x) const
{
x %= m_map_width;
if (x < 0) {
x = (m_map_width - x) + 1;
}
return static_cast<uint32_t>(x);
}
geom::point_t expire_tiles_t::coords_to_tile(geom::point_t const &point)
{
auto const c = m_projection->target_to_tile(point);
return {m_map_width * (0.5 + c.x() / tile_t::EARTH_CIRCUMFERENCE),
m_map_width * (0.5 - c.y() / tile_t::EARTH_CIRCUMFERENCE)};
}
void expire_tiles_t::from_point_list(geom::point_list_t const &list,
expire_config_t const &expire_config)
{
for_each_segment(list, [&](geom::point_t const &a, geom::point_t const &b) {
from_line_segment(a, b, expire_config);
});
}
void expire_tiles_t::from_geometry(geom::point_t const &geom,
expire_config_t const &expire_config)
{
auto const tilec = coords_to_tile(geom);
auto const ymin =
std::max(0U, static_cast<uint32_t>(tilec.y() - expire_config.buffer));
auto const ymax =
std::min(m_map_width - 1U,
static_cast<uint32_t>(tilec.y() + expire_config.buffer));
for (int x = static_cast<int>(tilec.x() - expire_config.buffer);
x <= static_cast<int>(tilec.x() + expire_config.buffer); ++x) {
uint32_t const norm_x = normalise_tile_x_coord(x);
for (uint32_t y = ymin; y <= ymax; ++y) {
expire_tile(norm_x, y);
}
}
}
void expire_tiles_t::from_geometry(geom::linestring_t const &geom,
expire_config_t const &expire_config)
{
from_point_list(geom, expire_config);
}
void expire_tiles_t::from_polygon_boundary(geom::polygon_t const &geom,
expire_config_t const &expire_config)
{
from_point_list(geom.outer(), expire_config);
for (auto const &inner : geom.inners()) {
from_point_list(inner, expire_config);
}
}
namespace {
template <typename TGEOM>
expire_mode decide_expire_mode(TGEOM const &geom,
expire_config_t const &expire_config,
geom::box_t *box)
{
if (expire_config.mode != expire_mode::hybrid) {
return expire_config.mode;
}
*box = geom::envelope(geom);
if (box->width() > expire_config.full_area_limit ||
box->height() > expire_config.full_area_limit) {
return expire_mode::boundary_only;
}
return expire_mode::full_area;
}
} // anonymous namespace
void expire_tiles_t::build_tile_list(std::vector<uint32_t> *tile_x_list,
geom::ring_t const &ring, double tile_y)
{
assert(!ring.empty());
for (std::size_t i = 1; i < ring.size(); ++i) {
auto const t1 = coords_to_tile(ring[i]);
auto const t2 = coords_to_tile(ring[i - 1]);
if ((t1.y() < tile_y && t2.y() >= tile_y) ||
(t2.y() < tile_y && t1.y() >= tile_y)) {
auto const pos =
(tile_y - t1.y()) / (t2.y() - t1.y()) * (t2.x() - t1.x());
tile_x_list->push_back(static_cast<uint32_t>(std::clamp(
t1.x() + pos, 0.0, static_cast<double>(m_map_width - 1))));
}
}
}
void expire_tiles_t::from_polygon_area(geom::polygon_t const &geom,
geom::box_t box)
{
if (!box.valid()) {
box = geom::envelope(geom);
}
// This uses a variation on a simple polygon fill algorithm, for instance
// described on https://alienryderflex.com/polygon_fill/ . For each row
// of tiles we find the intersections with the area boundary and "fill" in
// the tiles between them. Note that we don't need to take particular care
// about the boundary, because we simply use the algorithm we use for
// expiry along a line to do that, which will also take care of the buffer.
// Coordinates are numbered from bottom to top, tiles are numbered from top
// to bottom, so "min" and "max" are switched here.
auto const max_tile_y = static_cast<std::uint32_t>(
m_map_width * (0.5 - box.min().y() / tile_t::EARTH_CIRCUMFERENCE));
auto const min_tile_y = static_cast<std::uint32_t>(
m_map_width * (0.5 - box.max().y() / tile_t::EARTH_CIRCUMFERENCE));
std::vector<uint32_t> tile_x_list;
// Loop through the tile rows from top to bottom
for (std::uint32_t tile_y = min_tile_y; tile_y < max_tile_y; ++tile_y) {
// Build a list of tiles crossed by the area boundary
tile_x_list.clear();
build_tile_list(&tile_x_list, geom.outer(),
static_cast<double>(tile_y));
for (auto const &ring : geom.inners()) {
build_tile_list(&tile_x_list, ring, static_cast<double>(tile_y));
}
// Sort list of tiles from left to right
std::sort(tile_x_list.begin(), tile_x_list.end());
// Add the tiles between entering and leaving the area to expire list
assert(tile_x_list.size() % 2 == 0);
for (std::size_t i = 0; i < tile_x_list.size(); i += 2) {
if (tile_x_list[i] >= static_cast<uint32_t>(m_map_width - 1)) {
break;
}
if (tile_x_list[i + 1] > 0) {
for (std::uint32_t tile_x = tile_x_list[i];
tile_x < tile_x_list[i + 1]; ++tile_x) {
expire_tile(tile_x, tile_y);
}
}
}
}
}
void expire_tiles_t::from_geometry(geom::polygon_t const &geom,
expire_config_t const &expire_config)
{
geom::box_t box;
auto const mode = decide_expire_mode(geom, expire_config, &box);
from_polygon_boundary(geom, expire_config);
// Only need to expire area if in full_area mode. If there is only a
// single tile expired the whole polygon is inside that tile and we
// don't need to do the polygon expire.
if (mode == expire_mode::full_area && m_dirty_tiles.size() > 1) {
from_polygon_area(geom, box);
}
}
void expire_tiles_t::from_geometry(geom::multipolygon_t const &geom,
expire_config_t const &expire_config)
{
geom::box_t box;
auto const mode = decide_expire_mode(geom, expire_config, &box);
for (auto const &sgeom : geom) {
from_polygon_boundary(sgeom, expire_config);
}
// Only need to expire area if in full_area mode. If there is only a
// single tile expired the whole polygon is inside that tile and we
// don't need to do the polygon expire.
if (mode == expire_mode::full_area && m_dirty_tiles.size() > 1) {
for (auto const &sgeom : geom) {
from_polygon_area(sgeom, geom::box_t{});
}
}
}
// False positive: Apparently clang-tidy can not see through the visit()
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
void expire_tiles_t::from_geometry(geom::geometry_t const &geom,
expire_config_t const &expire_config)
{
if (!enabled()) {
return;
}
geom.visit([&](auto const &g) { from_geometry(g, expire_config); });
}
void expire_tiles_t::from_geometry_if_3857(geom::geometry_t const &geom,
expire_config_t const &expire_config)
{
if (geom.srid() == PROJ_SPHERE_MERC) {
from_geometry(geom, expire_config);
}
}
/*
* Expire tiles that a line crosses
*/
void expire_tiles_t::from_line_segment(geom::point_t const &a,
geom::point_t const &b,
expire_config_t const &expire_config)
{
auto tilec_a = coords_to_tile(a);
auto tilec_b = coords_to_tile(b);
if (tilec_a.x() > tilec_b.x()) {
/* We always want the line to go from left to right - swap the ends if it doesn't */
std::swap(tilec_a, tilec_b);
}
double const x_len = tilec_b.x() - tilec_a.x();
if (x_len > m_map_width / 2) { // NOLINT(bugprone-integer-division)
/* If the line is wider than half the map, assume it
crosses the international date line.
These coordinates get normalised again later */
tilec_a.set_x(tilec_a.x() + m_map_width);
std::swap(tilec_a, tilec_b);
}
double const y_len = tilec_b.y() - tilec_a.y();
double const hyp_len =
std::sqrt(x_len * x_len + y_len * y_len); /* Pythagoras */
double const x_step = x_len / hyp_len;
double const y_step = y_len / hyp_len;
for (int i = 0; i <= hyp_len / 0.4; ++i) {
double const step = i * 0.4;
double const next_step = std::min(hyp_len, (i + 1) * 0.4);
double const x1 = tilec_a.x() + (step * x_step);
double y1 = tilec_a.y() + (step * y_step);
double const x2 = tilec_a.x() + (next_step * x_step);
double y2 = tilec_a.y() + (next_step * y_step);
/* The line (x1,y1),(x2,y2) is up to 1 tile width long
x1 will always be <= x2
We could be smart and figure out the exact tiles intersected,
but for simplicity, treat the coordinates as a bounding box
and expire everything within that box. */
if (y1 > y2) {
std::swap(y1, y2);
}
for (int x = static_cast<int>(x1 - expire_config.buffer);
x <= static_cast<int>(x2 + expire_config.buffer); ++x) {
uint32_t const norm_x = normalise_tile_x_coord(x);
for (int y = static_cast<int>(y1 - expire_config.buffer);
y <= static_cast<int>(y2 + expire_config.buffer); ++y) {
if (y >= 0) {
expire_tile(norm_x, static_cast<uint32_t>(y));
}
}
}
}
}
quadkey_list_t expire_tiles_t::get_tiles()
{
quadkey_list_t tiles;
tiles.reserve(m_dirty_tiles.size());
tiles.assign(m_dirty_tiles.cbegin(), m_dirty_tiles.cend());
std::sort(tiles.begin(), tiles.end());
m_dirty_tiles.clear();
return tiles;
}
int expire_from_result(expire_tiles_t *expire, pg_result_t const &result,
expire_config_t const &expire_config)
{
auto const num_tuples = result.num_tuples();
for (int i = 0; i < num_tuples; ++i) {
expire->from_geometry(ewkb_to_geom(result.get(i, 0)), expire_config);
}
return num_tuples;
}