Replace polygons that collapse entirely with their outlines, not rectangles

When a polygon collapsed away to nothing during cleaning, revive_polygon()
replaced it with a rectangle with the polygon's area, centered on the
average of its vertices. That looked bad for two reasons. Most polygons that
collapse are much smaller than a pixel, and the rectangle is at least a
pixel, so it was usually far too big: for Dane County Census blocks at
-D8 -d8 -pt -pn, 88% of the 27,206 revived polygons had less than half a
pixel of area, and the median rectangle had 11 times the area of the polygon
it replaced. And the larger polygons that collapse are almost all long and
narrow, like blocks between roads, so a square at their midpoint looked
nothing like them.

Now a polygon that collapses entirely is first replaced by the union of
one-pixel strips along every edge of its simplified outline, which traces
its shape in the right place. Only if that is empty too, because the whole
polygon is within a pixel or so, is it replaced by the rectangle, which is
then a single pixel. This is part of spindle preservation, so with
--no-spindle-preservation the rectangle is still used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
Claude
2026-11-30 22:41:12 +00:00
parent 5b35e5dacf
commit 34e1f56bdd
10 changed files with 106 additions and 9 deletions
+30
View File
@@ -663,3 +663,33 @@ TEST_CASE("Polygon cleaning keeps a diagonal polygon one pixel wide", "[wagyu]")
drawvec out = clean_or_clip_poly(geom, 0, 0, false, false);
REQUIRE(get_mp_area(out) == 7);
}
TEST_CASE("A polygon that collapses entirely can be replaced by its outline", "[spindle]") {
const int z = 0, detail = 12, shift = 32 - detail - z;
// An L-shaped polygon 0.2 pixels wide, with arms 1000 and 500 pixels long
drawvec geom = world_ring({{100, 100}, {1100, 100}, {1100, 600}, {1099.8, 600}, {1099.8, 100.2}, {100, 100.2}}, shift);
drawvec scaled = geom;
to_tile_scale(scaled, z, detail);
REQUIRE(clean_or_clip_poly(scaled, 0, 0, false, true).size() == 0);
drawvec outline = union_polygon_strips(drawvec(), outline_strips(geom, z, detail));
double area = get_mp_area(outline);
// About a pixel wide along both arms, rather than a square
REQUIRE(area >= 1500);
REQUIRE(area <= 2 * 1500 + 10);
long long minx = LLONG_MAX, maxx = LLONG_MIN, miny = LLONG_MAX, maxy = LLONG_MIN;
for (auto const &d : outline) {
minx = std::min(minx, (long long) d.x);
maxx = std::max(maxx, (long long) d.x);
miny = std::min(miny, (long long) d.y);
maxy = std::max(maxy, (long long) d.y);
}
REQUIRE(minx >= 99);
REQUIRE(maxx <= 1101);
REQUIRE(miny >= 99);
REQUIRE(maxy <= 601);
}