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
+23 -3
View File
@@ -674,6 +674,8 @@ static void *simplification_worker(void *v) {
signed char t = (*features)[i]->t;
int z = (*features)[i]->z;
int out_detail = (*features)[i]->extra_detail;
// stairstepped onto a grid, which strips would not follow
bool gridded = additional[A_GRID_LOW_ZOOMS] && z < (*features)[i]->maxzoom;
drawvec geom = (*features)[i]->geometry;
@@ -682,11 +684,19 @@ static void *simplification_worker(void *v) {
// restored as one-pixel-wide strips after cleaning.
drawvec strips;
// (Not when the geometry has been stairstepped onto a grid, which the strips would not follow.)
if (t == VT_POLYGON && !prevent[P_SPINDLES] && !(additional[A_GRID_LOW_ZOOMS] && z < (*features)[i]->maxzoom)) {
if (t == VT_POLYGON && !prevent[P_SPINDLES] && !gridded) {
strips = thin_polygon_strips(geom, z, out_detail, (*features)[i]->simplification,
(long long) (*features)[i]->tx << (32 - z), (long long) (*features)[i]->ty << (32 - z));
}
// The polygon as simplified but before rounding, in case it collapses
// entirely and needs to be replaced by something shaped like it
// (which is part of spindle preservation, so not if that is turned off)
drawvec world;
if (t == VT_POLYGON && !gridded && !prevent[P_SPINDLES]) {
world = geom;
}
to_tile_scale(geom, z, out_detail);
if (t == VT_POLYGON) {
@@ -704,8 +714,18 @@ static void *simplification_worker(void *v) {
if (geom.size() < 3) {
if (area > 0) {
// area is in world coordinates, calculated before scaling down
geom = revive_polygon(before, area, z, out_detail);
// Replace the polygon with a line one pixel wide along its outline,
// which keeps its shape and location. Only if that too is empty,
// because the whole polygon is within a pixel or so, fall back to
// a rectangle with the polygon's area (but at least a pixel).
geom.clear();
if (world.size() > 0) {
geom = union_polygon_strips(drawvec(), outline_strips(world, z, out_detail));
}
if (geom.size() < 3) {
// area is in world coordinates, calculated before scaling down
geom = revive_polygon(before, area, z, out_detail);
}
} else {
geom.clear();
}