Skip to content

Commit 8c1623c

Browse files
authored
Merge pull request #2451 from joto/reorg-expire
Reorganize expire code
2 parents a599cde + 94bb380 commit 8c1623c

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/expire-tiles.cpp

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,43 @@ void expire_tiles_t::from_polygon_boundary(geom::polygon_t const &geom,
114114
}
115115
}
116116

117+
namespace {
118+
119+
template <typename TGEOM>
120+
expire_mode decide_expire_mode(TGEOM const &geom,
121+
expire_config_t const &expire_config,
122+
geom::box_t *box)
123+
{
124+
if (expire_config.mode != expire_mode::hybrid) {
125+
return expire_config.mode;
126+
}
127+
128+
*box = geom::envelope(geom);
129+
if (box->width() > expire_config.full_area_limit ||
130+
box->height() > expire_config.full_area_limit) {
131+
return expire_mode::boundary_only;
132+
}
133+
134+
return expire_mode::full_area;
135+
}
136+
137+
} // anonymous namespace
138+
117139
void expire_tiles_t::from_geometry(geom::polygon_t const &geom,
118140
expire_config_t const &expire_config)
119141
{
120-
if (expire_config.mode == expire_mode::boundary_only) {
142+
geom::box_t box;
143+
auto const mode = decide_expire_mode(geom, expire_config, &box);
144+
145+
if (mode == expire_mode::boundary_only) {
121146
from_polygon_boundary(geom, expire_config);
122147
return;
123148
}
124149

125-
geom::box_t const box = geom::envelope(geom);
126-
if (from_bbox(box, expire_config)) {
127-
/* Bounding box too big - just expire tiles on the boundary */
128-
from_polygon_boundary(geom, expire_config);
150+
if (!box.valid()) {
151+
box = geom::envelope(geom);
129152
}
153+
from_bbox(box, expire_config);
130154
}
131155

132156
void expire_tiles_t::from_polygon_boundary(geom::multipolygon_t const &geom,
@@ -140,16 +164,18 @@ void expire_tiles_t::from_polygon_boundary(geom::multipolygon_t const &geom,
140164
void expire_tiles_t::from_geometry(geom::multipolygon_t const &geom,
141165
expire_config_t const &expire_config)
142166
{
143-
if (expire_config.mode == expire_mode::boundary_only) {
167+
geom::box_t box;
168+
auto const mode = decide_expire_mode(geom, expire_config, &box);
169+
170+
if (mode == expire_mode::boundary_only) {
144171
from_polygon_boundary(geom, expire_config);
145172
return;
146173
}
147174

148-
geom::box_t const box = geom::envelope(geom);
149-
if (from_bbox(box, expire_config)) {
150-
/* Bounding box too big - just expire tiles on the boundary */
151-
from_polygon_boundary(geom, expire_config);
175+
if (!box.valid()) {
176+
box = geom::envelope(geom);
152177
}
178+
from_bbox(box, expire_config);
153179
}
154180

155181
// False positive: Apparently clang-tidy can not see through the visit()
@@ -238,15 +264,6 @@ void expire_tiles_t::from_line_segment(geom::point_t const &a,
238264
int expire_tiles_t::from_bbox(geom::box_t const &box,
239265
expire_config_t const &expire_config)
240266
{
241-
double const width = box.width();
242-
double const height = box.height();
243-
244-
if (expire_config.mode == expire_mode::hybrid &&
245-
(width > expire_config.full_area_limit ||
246-
height > expire_config.full_area_limit)) {
247-
return -1;
248-
}
249-
250267
/* Convert the box's Mercator coordinates into tile coordinates */
251268
auto const tmp_min = coords_to_tile({box.min_x(), box.max_y()});
252269
int const min_tile_x =

src/geom-box.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class box_t
7272
return !(a == b);
7373
}
7474

75+
bool valid() const noexcept
76+
{
77+
return m_min_x != std::numeric_limits<double>::max();
78+
}
79+
7580
private:
7681
double m_min_x = std::numeric_limits<double>::max();
7782
double m_min_y = std::numeric_limits<double>::max();

tests/test-geom-box.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TEST_CASE("box_t getter/setter", "[NoDB]")
1616
{
1717
geom::box_t box{1.0, 2.0, 3.0, 4.0};
1818

19+
REQUIRE(box.valid());
1920
REQUIRE(box.min_x() == Approx(1.0));
2021
REQUIRE(box.max_x() == Approx(3.0));
2122
REQUIRE(box.min_y() == Approx(2.0));
@@ -57,7 +58,9 @@ TEST_CASE("Extend box_t with points", "[NoDB]")
5758
{
5859
geom::box_t box;
5960

61+
REQUIRE_FALSE(box.valid());
6062
box.extend(geom::point_t{1.0, 2.0});
63+
REQUIRE(box.valid());
6164

6265
REQUIRE(box.min_x() == Approx(1.0));
6366
REQUIRE(box.max_x() == Approx(1.0));

0 commit comments

Comments
 (0)