Skip to content

Commit bdd36a4

Browse files
committed
Refactor expire code: Some cleanups
* Use m_ name prefix for members * Make expire a real class * Simplify code * ...
1 parent 9cde168 commit bdd36a4

3 files changed

Lines changed: 88 additions & 103 deletions

File tree

src/expire-tiles.cpp

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ void expire_tiles::output_and_destroy(char const *filename, uint32_t minzoom)
7070

7171
expire_tiles::expire_tiles(uint32_t max, double bbox,
7272
const std::shared_ptr<reprojection> &proj)
73-
: max_bbox(bbox), maxzoom(max), projection(proj)
73+
: m_max_bbox(bbox), m_maxzoom(max), m_projection(proj)
7474
{
75-
map_width = 1U << maxzoom;
76-
tile_width = EARTH_CIRCUMFERENCE / map_width;
75+
m_map_width = 1U << m_maxzoom;
76+
m_tile_width = EARTH_CIRCUMFERENCE / m_map_width;
7777
}
7878

7979
void expire_tiles::expire_tile(uint32_t x, uint32_t y)
8080
{
8181
// Only try to insert to tile into the set if the last inserted tile
8282
// is different from this tile.
83-
tile_t const new_tile{maxzoom, x, y};
83+
tile_t const new_tile{m_maxzoom, x, y};
8484
if (!m_prev_tile.valid() || m_prev_tile != new_tile) {
8585
m_dirty_tiles.insert(new_tile.quadkey());
8686
m_prev_tile = new_tile;
@@ -89,26 +89,26 @@ void expire_tiles::expire_tile(uint32_t x, uint32_t y)
8989

9090
uint32_t expire_tiles::normalise_tile_x_coord(int x) const
9191
{
92-
x %= map_width;
92+
x %= m_map_width;
9393
if (x < 0) {
94-
x = (map_width - x) + 1;
94+
x = (m_map_width - x) + 1;
9595
}
9696
return static_cast<uint32_t>(x);
9797
}
9898

99-
void expire_tiles::coords_to_tile(double lon, double lat, double *tilex,
99+
void expire_tiles::coords_to_tile(geom::point_t const &point, double *tilex,
100100
double *tiley)
101101
{
102-
auto const c = projection->target_to_tile(geom::point_t{lon, lat});
102+
auto const c = m_projection->target_to_tile(point);
103103

104-
*tilex = map_width * (0.5 + c.x() / EARTH_CIRCUMFERENCE);
105-
*tiley = map_width * (0.5 - c.y() / EARTH_CIRCUMFERENCE);
104+
*tilex = m_map_width * (0.5 + c.x() / EARTH_CIRCUMFERENCE);
105+
*tiley = m_map_width * (0.5 - c.y() / EARTH_CIRCUMFERENCE);
106106
}
107107

108108
void expire_tiles::from_point_list(geom::point_list_t const &list)
109109
{
110-
for_each_segment(list, [&](geom::point_t a, geom::point_t b) {
111-
from_line(a.x(), a.y(), b.x(), b.y());
110+
for_each_segment(list, [&](geom::point_t const &a, geom::point_t const &b) {
111+
from_line(a, b);
112112
});
113113
}
114114

@@ -154,16 +154,15 @@ void expire_tiles::from_geometry(geom::geometry_t const &geom, osmid_t osm_id)
154154
/*
155155
* Expire tiles that a line crosses
156156
*/
157-
void expire_tiles::from_line(double lon_a, double lat_a, double lon_b,
158-
double lat_b)
157+
void expire_tiles::from_line(geom::point_t const &a, geom::point_t const &b)
159158
{
160159
double tile_x_a = NAN;
161160
double tile_y_a = NAN;
162161
double tile_x_b = NAN;
163162
double tile_y_b = NAN;
164163

165-
coords_to_tile(lon_a, lat_a, &tile_x_a, &tile_y_a);
166-
coords_to_tile(lon_b, lat_b, &tile_x_b, &tile_y_b);
164+
coords_to_tile(a, &tile_x_a, &tile_y_a);
165+
coords_to_tile(b, &tile_x_b, &tile_y_b);
167166

168167
if (tile_x_a > tile_x_b) {
169168
/* We always want the line to go from left to right - swap the ends if it doesn't */
@@ -176,11 +175,11 @@ void expire_tiles::from_line(double lon_a, double lat_a, double lon_b,
176175
}
177176

178177
double const x_len = tile_x_b - tile_x_a;
179-
if (x_len > map_width / 2) {
178+
if (x_len > m_map_width / 2) {
180179
/* If the line is wider than half the map, assume it
181180
crosses the international date line.
182181
These coordinates get normalised again later */
183-
tile_x_a += map_width;
182+
tile_x_a += m_map_width;
184183
double temp = tile_x_b;
185184
tile_x_b = tile_x_a;
186185
tile_x_a = temp;
@@ -232,43 +231,32 @@ void expire_tiles::from_line(double lon_a, double lat_a, double lon_b,
232231
*/
233232
int expire_tiles::from_bbox(geom::box_t const &box)
234233
{
235-
double const min_lon = box.min_x();
236-
double const min_lat = box.min_y();
237-
double const max_lon = box.max_x();
238-
double const max_lat = box.max_y();
239-
240-
return from_bbox(min_lon, min_lat, max_lon, max_lat);
241-
}
242-
243-
int expire_tiles::from_bbox(double min_lon, double min_lat, double max_lon,
244-
double max_lat)
245-
{
246-
if (maxzoom == 0) {
234+
if (!enabled()) {
247235
return 0;
248236
}
249237

250-
double const width = max_lon - min_lon;
251-
double const height = max_lat - min_lat;
238+
double const width = box.width();
239+
double const height = box.height();
252240
if (width > HALF_EARTH_CIRCUMFERENCE + 1) {
253241
/* Over half the planet's width within the bounding box - assume the
254242
box crosses the international date line and split it into two boxes */
255243
int ret =
256-
from_bbox(-HALF_EARTH_CIRCUMFERENCE, min_lat, min_lon, max_lat);
257-
ret += from_bbox(max_lon, min_lat, HALF_EARTH_CIRCUMFERENCE, max_lat);
244+
from_bbox({-HALF_EARTH_CIRCUMFERENCE, box.min_y(), box.min_x(), box.max_y()});
245+
ret += from_bbox({box.max_x(), box.min_y(), HALF_EARTH_CIRCUMFERENCE, box.max_y()});
258246
return ret;
259247
}
260248

261-
if (width > max_bbox || height > max_bbox) {
249+
if (width > m_max_bbox || height > m_max_bbox) {
262250
return -1;
263251
}
264252

265253
/* Convert the box's Mercator coordinates into tile coordinates */
266254
double tmp_x = NAN;
267255
double tmp_y = NAN;
268-
coords_to_tile(min_lon, max_lat, &tmp_x, &tmp_y);
256+
coords_to_tile({box.min_x(), box.max_y()}, &tmp_x, &tmp_y);
269257
int min_tile_x = tmp_x - TILE_EXPIRY_LEEWAY;
270258
int min_tile_y = tmp_y - TILE_EXPIRY_LEEWAY;
271-
coords_to_tile(max_lon, min_lat, &tmp_x, &tmp_y);
259+
coords_to_tile({box.max_x(), box.min_y()}, &tmp_x, &tmp_y);
272260
int max_tile_x = tmp_x + TILE_EXPIRY_LEEWAY;
273261
int max_tile_y = tmp_y + TILE_EXPIRY_LEEWAY;
274262
if (min_tile_x < 0) {
@@ -277,11 +265,11 @@ int expire_tiles::from_bbox(double min_lon, double min_lat, double max_lon,
277265
if (min_tile_y < 0) {
278266
min_tile_y = 0;
279267
}
280-
if (max_tile_x > map_width) {
281-
max_tile_x = map_width;
268+
if (max_tile_x > m_map_width) {
269+
max_tile_x = m_map_width;
282270
}
283-
if (max_tile_y > map_width) {
284-
max_tile_y = map_width;
271+
if (max_tile_y > m_map_width) {
272+
max_tile_y = m_map_width;
285273
}
286274
for (int iterator_x = min_tile_x; iterator_x <= max_tile_x; ++iterator_x) {
287275
uint32_t const norm_x = normalise_tile_x_coord(iterator_x);
@@ -295,34 +283,31 @@ int expire_tiles::from_bbox(double min_lon, double min_lat, double max_lon,
295283

296284
int expire_tiles::from_result(pg_result_t const &result, osmid_t osm_id)
297285
{
298-
//bail if we dont care about expiry
299-
if (maxzoom == 0) {
286+
if (!enabled()) {
300287
return -1;
301288
}
302289

303-
//dirty the stuff
304290
auto const num_tuples = result.num_tuples();
305291
for (int i = 0; i < num_tuples; ++i) {
306292
char const *const wkb = result.get_value(i, 0);
307293
from_geometry(ewkb_to_geom(decode_hex(wkb)), osm_id);
308294
}
309295

310-
//return how many rows were affected
311296
return num_tuples;
312297
}
313298

314299
void expire_tiles::merge_and_destroy(expire_tiles &other)
315300
{
316-
if (map_width != other.map_width) {
301+
if (m_map_width != other.m_map_width) {
317302
throw std::runtime_error{"Unable to merge tile expiry sets when "
318303
"map_width does not match: {} != {}."_format(
319-
map_width, other.map_width)};
304+
m_map_width, other.m_map_width)};
320305
}
321306

322-
if (tile_width != other.tile_width) {
307+
if (m_tile_width != other.m_tile_width) {
323308
throw std::runtime_error{"Unable to merge tile expiry sets when "
324309
"tile_width does not match: {} != {}."_format(
325-
tile_width, other.tile_width)};
310+
m_tile_width, other.m_tile_width)};
326311
}
327312

328313
if (m_dirty_tiles.empty()) {

src/expire-tiles.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,17 @@ class tile_output_t
4242
void output_dirty_tile(tile_t const &tile);
4343
};
4444

45-
struct expire_tiles
45+
class expire_tiles
4646
{
47+
public:
4748
expire_tiles(uint32_t maxzoom, double maxbbox,
4849
const std::shared_ptr<reprojection> &projection);
4950

50-
bool enabled() const noexcept { return maxzoom != 0; }
51+
bool enabled() const noexcept { return m_maxzoom != 0; }
5152

5253
void from_geometry(geom::geometry_t const &geom, osmid_t osm_id);
5354

5455
int from_bbox(geom::box_t const &box);
55-
int from_bbox(double min_lon, double min_lat, double max_lon,
56-
double max_lat);
5756

5857
/**
5958
* Expire tiles based on an osm id.
@@ -90,7 +89,7 @@ struct expire_tiles
9089
template <class TILE_WRITER>
9190
void output_and_destroy(TILE_WRITER &output_writer, uint32_t minzoom)
9291
{
93-
assert(minzoom <= maxzoom);
92+
assert(minzoom <= m_maxzoom);
9493
// build a sorted vector of all expired tiles
9594
std::vector<uint64_t> tiles_maxzoom(m_dirty_tiles.begin(),
9695
m_dirty_tiles.end());
@@ -101,10 +100,10 @@ struct expire_tiles
101100
*
102101
* last_quadkey is initialized with a value which is not expected to exist
103102
* (larger than largest possible quadkey). */
104-
uint64_t last_quadkey = 1ULL << (2 * maxzoom);
103+
uint64_t last_quadkey = 1ULL << (2 * m_maxzoom);
105104
std::size_t count = 0;
106105
for (auto const quadkey : tiles_maxzoom) {
107-
for (uint32_t dz = 0; dz <= maxzoom - minzoom; ++dz) {
106+
for (uint32_t dz = 0; dz <= m_maxzoom - minzoom; ++dz) {
108107
// scale down to the current zoom level
109108
uint64_t qt_current = quadkey >> (dz * 2);
110109
/* If dz > 0, there are propably multiple elements whose quadkey
@@ -115,7 +114,7 @@ struct expire_tiles
115114
continue;
116115
}
117116
auto const tile =
118-
tile_t::from_quadkey(qt_current, maxzoom - dz);
117+
tile_t::from_quadkey(qt_current, m_maxzoom - dz);
119118
output_writer.output_dirty_tile(tile);
120119
++count;
121120
}
@@ -135,7 +134,8 @@ struct expire_tiles
135134
/**
136135
* Converts from target coordinates to tile coordinates.
137136
*/
138-
void coords_to_tile(double lon, double lat, double *tilex, double *tiley);
137+
void coords_to_tile(geom::point_t const &point, double *tilex,
138+
double *tiley);
139139

140140
/**
141141
* Expire a single tile.
@@ -145,15 +145,15 @@ struct expire_tiles
145145
*/
146146
void expire_tile(uint32_t x, uint32_t y);
147147
uint32_t normalise_tile_x_coord(int x) const;
148-
void from_line(double lon_a, double lat_a, double lon_b, double lat_b);
148+
void from_line(geom::point_t const &a, geom::point_t const &b);
149149

150150
void from_point_list(geom::point_list_t const &list);
151151

152-
double tile_width;
153-
double max_bbox;
154-
int map_width;
155-
uint32_t maxzoom;
156-
std::shared_ptr<reprojection> projection;
152+
double m_tile_width;
153+
double m_max_bbox;
154+
int m_map_width;
155+
uint32_t m_maxzoom;
156+
std::shared_ptr<reprojection> m_projection;
157157

158158
/// The tile which has been added last to the unordered set.
159159
tile_t m_prev_tile;

0 commit comments

Comments
 (0)