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
+11
View File
@@ -22,6 +22,17 @@
part with open space on its far side, like a coastal strip, is still kept.
Since strips are only needed for edges that are already known to be narrow,
the shared edges are only looked up for those, not for every vertex.
* When a polygon collapses away to nothing entirely during cleaning, replace it
with a line one pixel wide along its outline, so it keeps its shape and
location, instead of a rectangle with the polygon's area centered on the
average of its vertices. For long, narrow polygons, like Census blocks
between roads, the rectangle looked nothing like the polygon. Most polygons
that collapse are much smaller than a pixel, so the rectangle was also
usually far too big: for Dane County Census blocks at -pt, the median
rectangle had 11 times the area of the polygon it replaced. Polygons that
are within about a pixel in every direction still get the rectangle, which
is then one pixel. With --no-spindle-preservation, the rectangle is still
used.
* Fix Wagyu's snap rounding so that an edge that intersects no other edge
is no longer snapped to a hot pixel whose corner it only touches, unless
that hot pixel is the rounded location of an intersection. Previously, two
+1 -1
View File
@@ -513,7 +513,7 @@ the same layer, enclose them in an `all` expression so they will all be evaluate
* `-pt` or `--no-tiny-polygon-reduction`: Don't combine the area of very small polygons into small squares that represent their combined area.
* `-pT` or `--no-tiny-polygon-reduction-at-maximum-zoom`: Combine the area of very small polygons into small squares that represent their combined area only at zoom levels below the maximum.
* `--tiny-polygon-size=`_size_: Use the specified _size_ for tiny polygons instead of the default 2. Anything above 6 or so will lead to visible artifacts with the default tile detail.
* `--no-spindle-preservation`: Don't preserve the narrow parts of polygons. Normally, parts of polygons that are too narrow to survive being rounded to tile coordinates (like a buffer of a few meters around a long road or river) are kept as continuous lines of polygon one pixel wide, and long, narrow polygons are kept as polygons rather than being combined into tiny-polygon squares. With this option, they are allowed to collapse away, which leaves them as scattered fragments wherever the rounding happens to keep their two sides apart. With `--no-simplification-of-shared-nodes`, narrow parts are not preserved where that would make them overlap a neighboring feature that shares a border with them, like a panhandle between two other political units, so that features that tile exactly continue to do so.
* `--no-spindle-preservation`: Don't preserve the narrow parts of polygons. Normally, parts of polygons that are too narrow to survive being rounded to tile coordinates (like a buffer of a few meters around a long road or river) are kept as continuous lines of polygon one pixel wide, and long, narrow polygons are kept as polygons rather than being combined into tiny-polygon squares. A polygon that still collapses away entirely is replaced by a line one pixel wide along its outline, rather than by a rectangle with its area. With this option, they are allowed to collapse away, which leaves them as scattered fragments wherever the rounding happens to keep their two sides apart. With `--no-simplification-of-shared-nodes`, narrow parts are not preserved where that would make them overlap a neighboring feature that shares a border with them, like a panhandle between two other political units, so that features that tile exactly continue to do so.
* `-av` or `--visvalingam`: Use Visvalingam's simplification algorithm rather than Douglas-Peucker's.
### Attempts to improve shared polygon boundaries
+1 -1
View File
@@ -550,7 +550,7 @@ the line or polygon within one tile unit of its proper location. You can probabl
.IP \(bu 2
\fB--tiny-polygon-size=\fR\fIsize\fP: Use the specified \fIsize\fP for tiny polygons instead of the default 2. Anything above 6 or so will lead to visible artifacts with the default tile detail.
.IP \(bu 2
\fB--no-spindle-preservation\fR: Don't preserve the narrow parts of polygons. Normally, parts of polygons that are too narrow to survive being rounded to tile coordinates (like a buffer of a few meters around a long road or river) are kept as continuous lines of polygon one pixel wide, and long, narrow polygons are kept as polygons rather than being combined into tiny-polygon squares. With this option, they are allowed to collapse away, which leaves them as scattered fragments wherever the rounding happens to keep their two sides apart. With \fB--no-simplification-of-shared-nodes\fR, narrow parts are not preserved where that would make them overlap a neighboring feature that shares a border with them, like a panhandle between two other political units, so that features that tile exactly continue to do so.
\fB--no-spindle-preservation\fR: Don't preserve the narrow parts of polygons. Normally, parts of polygons that are too narrow to survive being rounded to tile coordinates (like a buffer of a few meters around a long road or river) are kept as continuous lines of polygon one pixel wide, and long, narrow polygons are kept as polygons rather than being combined into tiny-polygon squares. A polygon that still collapses away entirely is replaced by a line one pixel wide along its outline, rather than by a rectangle with its area. With this option, they are allowed to collapse away, which leaves them as scattered fragments wherever the rounding happens to keep their two sides apart. With \fB--no-simplification-of-shared-nodes\fR, narrow parts are not preserved where that would make them overlap a neighboring feature that shares a border with them, like a panhandle between two other political units, so that features that tile exactly continue to do so.
.IP \(bu 2
\fB-av\fR or \fB--visvalingam\fR: Use Visvalingam's simplification algorithm rather than Douglas-Peucker's.
.SS Attempts to improve shared polygon boundaries
+31
View File
@@ -421,6 +421,37 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl
return out;
}
drawvec outline_strips(drawvec const &geom, int z, int detail) {
drawvec out;
int shift = 32 - detail - z;
if (shift <= 0) {
return out;
}
for (size_t i = 0; i < geom.size(); i++) {
if (geom[i].op == VT_MOVETO) {
size_t j;
for (j = i + 1; j < geom.size(); j++) {
if (geom[j].op != VT_LINETO) {
break;
}
}
if (j - i >= 3) {
for (size_t k = i; k < j; k++) {
size_t n = (k + 1 < j) ? k + 1 : i; // close the ring if it isn't already
add_strip(out, geom[k].x, geom[k].y, geom[n].x, geom[n].y, shift);
}
}
i = j - 1;
}
}
return out;
}
double unshared_perimeter(drawvec const &geom, size_t i, size_t j, long long ox, long long oy) {
double perimeter = 0;
+5
View File
@@ -30,6 +30,11 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl
// a tile origin at (ox, oy)) that is not made of edges shared with other features
double unshared_perimeter(drawvec const &geom, size_t i, size_t j, long long ox, long long oy);
// Strips (in tile coordinates) one pixel wide along every edge of a polygon
// (in world coordinates), which together trace its outline. This is used to
// stand in for a polygon that has collapsed away to nothing entirely.
drawvec outline_strips(drawvec const &geom, int z, int detail);
// Union the strips from thin_polygon_strips() with a cleaned polygon
// (both in tile coordinates)
drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips);
@@ -173,7 +173,7 @@
,
{ "type": "FeatureCollection", "properties": { "zoom": 7, "x": 8, "y": 66 }, "features": [
{ "type": "FeatureCollection", "properties": { "layer": "in", "version": 2, "extent": 128 }, "features": [
{ "type": "Feature", "properties": { "adm1_code": "KIR+99?", "OBJECTID_1": 3643, "diss_me": 10097, "adm1_cod_1": "KIR+99?", "iso_3166_2": "KI-", "iso_a2": "KI", "adm0_sr": 5, "code_hasc": "-99", "note": "KIR-99 (Kiribati minor island)", "provnum_ne": 0, "gadm_level": 0, "check_me": 0, "scalerank": 11, "datarank": 11, "area_sqkm": 0, "sameascity": -99, "labelrank": 20, "featurecla": "Admin-1 minor island", "name_len": 0, "mapcolor9": 6, "mapcolor13": 12, "woe_id": -99, "latitude": -4.689669, "longitude": -174.511, "sov_a3": "KIR", "adm0_a3": "KIR", "adm0_label": 7, "admin": "Kiribati", "geonunit": "Kiribati", "gu_a3": "KIR", "gn_id": 0, "gns_id": 0, "gn_level": 0, "gn_a1_code": "KI.", "gns_level": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -155.939941, -5.594118 ], [ -155.895996, -5.594118 ], [ -155.895996, -5.637853 ], [ -155.939941, -5.637853 ], [ -155.939941, -5.594118 ] ] ] } }
{ "type": "Feature", "properties": { "adm1_code": "KIR+99?", "OBJECTID_1": 3643, "diss_me": 10097, "adm1_cod_1": "KIR+99?", "iso_3166_2": "KI-", "iso_a2": "KI", "adm0_sr": 5, "code_hasc": "-99", "note": "KIR-99 (Kiribati minor island)", "provnum_ne": 0, "gadm_level": 0, "check_me": 0, "scalerank": 11, "datarank": 11, "area_sqkm": 0, "sameascity": -99, "labelrank": 20, "featurecla": "Admin-1 minor island", "name_len": 0, "mapcolor9": 6, "mapcolor13": 12, "woe_id": -99, "latitude": -4.689669, "longitude": -174.511, "sov_a3": "KIR", "adm0_a3": "KIR", "adm0_label": 7, "admin": "Kiribati", "geonunit": "Kiribati", "gu_a3": "KIR", "gn_id": 0, "gns_id": 0, "gn_level": 0, "gn_a1_code": "KI.", "gns_level": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -155.852051, -5.594118 ], [ -155.852051, -5.637853 ], [ -155.939941, -5.637853 ], [ -155.939941, -5.594118 ], [ -155.852051, -5.594118 ] ] ] } }
] }
] }
,
+2 -2
View File
@@ -28,7 +28,7 @@
,
{ "type": "FeatureCollection", "properties": { "zoom": 1, "x": 0, "y": 0 }, "features": [
{ "type": "FeatureCollection", "properties": { "layer": "in", "version": 2, "extent": 4096 }, "features": [
{ "type": "Feature", "properties": { "adm1_code": "KIR+99?", "OBJECTID_1": 3643, "diss_me": 10097, "adm1_cod_1": "KIR+99?", "iso_3166_2": "KI-", "iso_a2": "KI", "adm0_sr": 5, "code_hasc": "-99", "note": "KIR-99 (Kiribati minor island)", "provnum_ne": 0, "gadm_level": 0, "check_me": 0, "scalerank": 11, "datarank": 11, "area_sqkm": 0, "sameascity": -99, "labelrank": 20, "featurecla": "Admin-1 minor island", "name_len": 0, "mapcolor9": 6, "mapcolor13": 12, "woe_id": -99, "latitude": -4.689669, "longitude": -174.511, "sov_a3": "KIR", "adm0_a3": "KIR", "adm0_label": 7, "admin": "Kiribati", "geonunit": "Kiribati", "gu_a3": "KIR", "gn_id": 0, "gns_id": 0, "gn_level": 0, "gn_a1_code": "KI.", "gns_level": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -171.079102, -3.118576 ], [ -171.035156, -3.118576 ], [ -171.035156, -3.162456 ], [ -171.079102, -3.162456 ], [ -171.079102, -3.118576 ] ] ] } }
{ "type": "Feature", "properties": { "adm1_code": "KIR+99?", "OBJECTID_1": 3643, "diss_me": 10097, "adm1_cod_1": "KIR+99?", "iso_3166_2": "KI-", "iso_a2": "KI", "adm0_sr": 5, "code_hasc": "-99", "note": "KIR-99 (Kiribati minor island)", "provnum_ne": 0, "gadm_level": 0, "check_me": 0, "scalerank": 11, "datarank": 11, "area_sqkm": 0, "sameascity": -99, "labelrank": 20, "featurecla": "Admin-1 minor island", "name_len": 0, "mapcolor9": 6, "mapcolor13": 12, "woe_id": -99, "latitude": -4.689669, "longitude": -174.511, "sov_a3": "KIR", "adm0_a3": "KIR", "adm0_label": 7, "admin": "Kiribati", "geonunit": "Kiribati", "gu_a3": "KIR", "gn_id": 0, "gns_id": 0, "gn_level": 0, "gn_a1_code": "KI.", "gns_level": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -171.035156, -3.118576 ], [ -171.035156, -3.162456 ], [ -171.123047, -3.162456 ], [ -171.123047, -3.118576 ], [ -171.035156, -3.118576 ] ] ] } }
] }
] }
,
@@ -208,7 +208,7 @@
,
{ "type": "FeatureCollection", "properties": { "zoom": 7, "x": 8, "y": 66 }, "features": [
{ "type": "FeatureCollection", "properties": { "layer": "in", "version": 2, "extent": 128 }, "features": [
{ "type": "Feature", "properties": { "adm1_code": "KIR+99?", "OBJECTID_1": 3643, "diss_me": 10097, "adm1_cod_1": "KIR+99?", "iso_3166_2": "KI-", "iso_a2": "KI", "adm0_sr": 5, "code_hasc": "-99", "note": "KIR-99 (Kiribati minor island)", "provnum_ne": 0, "gadm_level": 0, "check_me": 0, "scalerank": 11, "datarank": 11, "area_sqkm": 0, "sameascity": -99, "labelrank": 20, "featurecla": "Admin-1 minor island", "name_len": 0, "mapcolor9": 6, "mapcolor13": 12, "woe_id": -99, "latitude": -4.689669, "longitude": -174.511, "sov_a3": "KIR", "adm0_a3": "KIR", "adm0_label": 7, "admin": "Kiribati", "geonunit": "Kiribati", "gu_a3": "KIR", "gn_id": 0, "gns_id": 0, "gn_level": 0, "gn_a1_code": "KI.", "gns_level": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -155.939941, -5.594118 ], [ -155.895996, -5.594118 ], [ -155.895996, -5.637853 ], [ -155.939941, -5.637853 ], [ -155.939941, -5.594118 ] ] ] } }
{ "type": "Feature", "properties": { "adm1_code": "KIR+99?", "OBJECTID_1": 3643, "diss_me": 10097, "adm1_cod_1": "KIR+99?", "iso_3166_2": "KI-", "iso_a2": "KI", "adm0_sr": 5, "code_hasc": "-99", "note": "KIR-99 (Kiribati minor island)", "provnum_ne": 0, "gadm_level": 0, "check_me": 0, "scalerank": 11, "datarank": 11, "area_sqkm": 0, "sameascity": -99, "labelrank": 20, "featurecla": "Admin-1 minor island", "name_len": 0, "mapcolor9": 6, "mapcolor13": 12, "woe_id": -99, "latitude": -4.689669, "longitude": -174.511, "sov_a3": "KIR", "adm0_a3": "KIR", "adm0_label": 7, "admin": "Kiribati", "geonunit": "Kiribati", "gu_a3": "KIR", "gn_id": 0, "gns_id": 0, "gn_level": 0, "gn_a1_code": "KI.", "gns_level": 0 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -155.852051, -5.594118 ], [ -155.852051, -5.637853 ], [ -155.939941, -5.637853 ], [ -155.939941, -5.594118 ], [ -155.852051, -5.594118 ] ] ] } }
] }
] }
,
@@ -532,7 +532,7 @@
,
{ "type": "Feature", "properties": { "name": "Uganda" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 34.497070, 3.513421 ], [ 34.584961, 3.074695 ], [ 35.024414, 1.889306 ], [ 34.672852, 1.186439 ], [ 33.881836, 0.131836 ], [ 33.881836, 0.000000 ], [ 33.925781, -0.966751 ], [ 30.761719, -1.010690 ], [ 29.838867, -1.450040 ], [ 29.575195, -1.362176 ], [ 29.575195, -0.571280 ], [ 29.838867, -0.219726 ], [ 29.838867, 0.000000 ], [ 29.882812, 0.615223 ], [ 30.102539, 1.054628 ], [ 30.454102, 1.581830 ], [ 30.849609, 1.845384 ], [ 31.157227, 2.196727 ], [ 30.761719, 2.328460 ], [ 30.849609, 3.513421 ], [ 34.497070, 3.513421 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "S. Sudan" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 30.805664, 3.513421 ], [ 30.849609, 3.513421 ], [ 30.849609, 3.469557 ], [ 30.805664, 3.469557 ], [ 30.805664, 3.513421 ] ] ] } }
{ "type": "Feature", "properties": { "name": "S. Sudan" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 30.849609, 3.557283 ], [ 30.849609, 3.469557 ], [ 30.805664, 3.469557 ], [ 30.805664, 3.557283 ], [ 30.849609, 3.557283 ] ] ] } }
,
{ "type": "Feature", "properties": { "name": "Ethiopia" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 38.891602, 3.557283 ], [ 39.243164, 3.513421 ], [ 39.638672, 3.513421 ], [ 39.638672, 3.469557 ], [ 39.594727, 3.469557 ], [ 39.550781, 3.425692 ], [ 39.243164, 3.469557 ], [ 38.891602, 3.469557 ], [ 38.891602, 3.557283 ] ] ] } }
,
+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();
}
+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);
}