From 2468cdba25a410911ff461dd3594484b1b8fee07 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 21:57:22 +0000 Subject: [PATCH] Only exempt the unshared length of a ring from tiny polygon reduction Long, narrow rings were exempted from tiny polygon reduction so that spindle preservation could keep them as lines. But with -pn, no strips are added along borders shared with other features, so a narrow block whose length is mostly shared border was exempted, then collapsed completely in cleaning, and was then replaced by revive_polygon()'s placeholder rectangle, which overlaps its neighbors. (In Dane County census blocks at -D8 -d8 -pn, this raised the number of revived polygons at z12 from 57 to 92.) Now only the part of the ring's length that is not made of shared edges counts toward the exemption, so such blocks are combined into tiny polygons as before. The shared edges can be matched exactly here, because tiny polygon reduction happens before simplification. reduce_tiny_poly() takes the length function as a callback because clip.cpp is also linked into tools that don't include polygon.cpp. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ --- clip.cpp | 17 ++++++----------- geometry.hpp | 3 ++- polygon.cpp | 25 +++++++++++++++++++++++++ polygon.hpp | 4 ++++ tests/spindle-shared/out/-z7_-pn.json | 11 +---------- tile.cpp | 11 ++++++++++- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/clip.cpp b/clip.cpp index ec4910b8..2fd2150e 100644 --- a/clip.cpp +++ b/clip.cpp @@ -1045,19 +1045,11 @@ static drawvec simplify_lines_basic(drawvec &geom, int z, int detail, double sim return geom; } -// A ring whose perimeter is more than this many times the tiny polygon size is long enough +// A ring that is longer than this many times the tiny polygon size is long enough // to be visible as a line even if its area is too small to be visible as a polygon #define SPINDLE_PERIMETER 8 -static double get_perimeter(drawvec const &geom, size_t i, size_t j) { - double perimeter = 0; - for (size_t k = i; k + 1 < j; k++) { - perimeter += std::hypot((double) geom[k + 1].x - geom[k].x, (double) geom[k + 1].y - geom[k].y); - } - return perimeter; -} - -drawvec reduce_tiny_poly(drawvec const &geom, int z, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, double tiny_polygon_size, bool keep_spindles) { +drawvec reduce_tiny_poly(drawvec const &geom, int z, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, double tiny_polygon_size, std::function const &spindle_length) { drawvec out; const double pixel = (1LL << (32 - detail - z)) * (double) tiny_polygon_size; @@ -1095,8 +1087,11 @@ drawvec reduce_tiny_poly(drawvec const &geom, int z, int detail, bool *still_nee // // But a long, narrow outer ring is not tiny even if its area is small, // so if we are preserving spindles, let it represent itself. + // (The length that counts is the length that spindle preservation + // can actually preserve, which leaves out borders shared with + // other features if --no-simplification-of-shared-nodes is used.) bool tiny = (area > 0 && area <= pixel * pixel); - if (tiny && keep_spindles && get_perimeter(geom, i, j) > SPINDLE_PERIMETER * pixel) { + if (tiny && spindle_length && spindle_length(geom, i, j) > SPINDLE_PERIMETER * pixel) { tiny = false; } diff --git a/geometry.hpp b/geometry.hpp index 82fe0286..be0210c6 100644 --- a/geometry.hpp +++ b/geometry.hpp @@ -2,6 +2,7 @@ #define GEOMETRY_HPP #include +#include #include #include #include @@ -74,7 +75,7 @@ drawvec remove_noop(drawvec geom, int type, int shift); drawvec clip_point(drawvec &geom, int z, long long buffer); drawvec clean_or_clip_poly(drawvec &geom, int z, int buffer, bool clip, bool try_scaling); drawvec close_poly(drawvec &geom); -drawvec reduce_tiny_poly(const drawvec &geom, int z, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, double tiny_polygon_size, bool keep_spindles = false); +drawvec reduce_tiny_poly(const drawvec &geom, int z, int detail, bool *still_needs_simplification, bool *reduced_away, double *accum_area, double tiny_polygon_size, std::function const &spindle_length = nullptr); int clip(long long *x0, long long *y0, long long *x1, long long *y1, long long xmin, long long ymin, long long xmax, long long ymax); drawvec clip_lines(drawvec &geom, int z, long long buffer); drawvec stairstep(drawvec &geom, int z, int detail); diff --git a/polygon.cpp b/polygon.cpp index 29fc673b..b8db92b8 100644 --- a/polygon.cpp +++ b/polygon.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -420,6 +421,30 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simpl return out; } +double unshared_perimeter(drawvec const &geom, size_t i, size_t j, long long ox, long long oy) { + double perimeter = 0; + + for (size_t k = i; k + 1 < j; k++) { + long long ax = geom[k].x + ox, ay = geom[k].y + oy; + long long bx = geom[k + 1].x + ox, by = geom[k + 1].y + oy; + + if (shared_edges_count > 0) { + auto first = std::lower_bound(shared_edges_map, shared_edges_map + shared_edges_count, shared_edge{ax, ay, bx, by}, + [](shared_edge const &a, shared_edge const &b) { + return std::make_tuple(a.ay, a.ax, a.by, a.bx) < std::make_tuple(b.ay, b.ax, b.by, b.bx); + }); + if (first != shared_edges_map + shared_edges_count && + first->ax == ax && first->ay == ay && first->bx == bx && first->by == by) { + continue; + } + } + + perimeter += std::hypot((double) bx - ax, (double) by - ay); + } + + return perimeter; +} + static void add_rings(mapbox::geometry::wagyu::wagyu &wagyu, drawvec const &geom, mapbox::geometry::wagyu::polygon_type type, long long scale) { for (size_t i = 0; i < geom.size(); i++) { if (geom[i].op == VT_MOVETO) { diff --git a/polygon.hpp b/polygon.hpp index 71aae74a..c8cd6e7b 100644 --- a/polygon.hpp +++ b/polygon.hpp @@ -26,6 +26,10 @@ extern size_t shared_edges_count; // coordinate of the tile origin that the geometry is relative to. drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simplification = 1, long long ox = 0, long long oy = 0); +// The length of the ring from geom[i] to geom[j] (world coordinates relative to +// 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); + // 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); diff --git a/tests/spindle-shared/out/-z7_-pn.json b/tests/spindle-shared/out/-z7_-pn.json index dadc94e3..78187cae 100644 --- a/tests/spindle-shared/out/-z7_-pn.json +++ b/tests/spindle-shared/out/-z7_-pn.json @@ -9,19 +9,16 @@ "maxzoom": "7", "minzoom": "0", "name": "tests/spindle-shared/out/-z7_-pn.json.check.mbtiles", +"strategies": "[{\"tiny_polygons\":2},{\"tiny_polygons\":1},{\"tiny_polygons\":1},{\"tiny_polygons\":1},{},{},{},{}]", "type": "overlay", "version": "2" }, "features": [ { "type": "FeatureCollection", "properties": { "zoom": 0, "x": 0, "y": 0 }, "features": [ { "type": "FeatureCollection", "properties": { "layer": "in", "version": 2, "extent": 4096 }, "features": [ -{ "type": "Feature", "properties": { "name": "C" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.316406, 39.842286 ], [ -99.316406, 39.774769 ], [ -100.019531, 39.774769 ], [ -100.019531, 39.842286 ], [ -99.316406, 39.842286 ] ] ] } } -, { "type": "Feature", "properties": { "name": "D" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.316406, 39.909736 ], [ -99.316406, 39.842286 ], [ -100.019531, 39.842286 ], [ -100.019531, 39.909736 ], [ -99.316406, 39.909736 ] ] ] } } , { "type": "Feature", "properties": { "name": "R" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -100.019531, 39.842286 ], [ -99.316406, 39.774769 ], [ -99.316406, 39.707187 ], [ -100.019531, 39.707187 ], [ -100.019531, 39.842286 ] ] ] } } , -{ "type": "Feature", "properties": { "name": "P" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.755859, 40.044438 ], [ -99.667969, 40.044438 ], [ -99.667969, 39.977120 ], [ -99.755859, 39.977120 ], [ -99.755859, 40.044438 ] ] ] } } -, { "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -100.019531, 40.044438 ], [ -99.316406, 39.977120 ], [ -100.019531, 39.977120 ], [ -100.019531, 40.044438 ] ] ] } } , { "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.931641, 40.044438 ], [ -99.492188, 39.977120 ], [ -99.316406, 39.977120 ], [ -99.316406, 39.909736 ], [ -100.019531, 39.909736 ], [ -100.019531, 39.977120 ], [ -99.931641, 40.044438 ] ] ] } } @@ -36,8 +33,6 @@ , { "type": "Feature", "properties": { "name": "R" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -100.019531, 39.774769 ], [ -99.316406, 39.740986 ], [ -99.316406, 39.673370 ], [ -99.448242, 39.707187 ], [ -100.019531, 39.707187 ], [ -100.019531, 39.774769 ] ] ] } } , -{ "type": "Feature", "properties": { "name": "P" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.316406, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.931641, 40.010787 ], [ -99.316406, 40.010787 ] ] ] } } -, { "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.316406, 40.078071 ], [ -99.316406, 40.010787 ], [ -100.019531, 40.010787 ], [ -100.019531, 40.078071 ], [ -99.316406, 40.078071 ] ] ] } } , { "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.931641, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.316406, 40.010787 ], [ -99.316406, 39.943436 ], [ -100.019531, 39.943436 ], [ -100.019531, 40.010787 ], [ -99.931641, 40.010787 ] ] ] } } @@ -52,8 +47,6 @@ , { "type": "Feature", "properties": { "name": "R" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.448242, 39.757880 ], [ -99.294434, 39.724089 ], [ -99.294434, 39.690281 ], [ -99.448242, 39.724089 ], [ -99.558105, 39.690281 ], [ -99.711914, 39.724089 ], [ -99.887695, 39.690281 ], [ -99.997559, 39.724089 ], [ -99.997559, 39.757880 ], [ -99.887695, 39.724089 ], [ -99.711914, 39.757880 ], [ -99.558105, 39.724089 ], [ -99.448242, 39.757880 ] ] ] } } , -{ "type": "Feature", "properties": { "name": "P" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.733887, 40.010787 ], [ -99.711914, 40.010787 ], [ -99.711914, 39.993956 ], [ -99.733887, 39.993956 ], [ -99.733887, 40.010787 ] ] ] } } -, { "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.294434, 40.078071 ], [ -99.294434, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.645996, 40.010787 ], [ -99.755859, 39.977120 ], [ -99.909668, 40.010787 ], [ -99.997559, 39.993956 ], [ -99.997559, 40.078071 ], [ -99.294434, 40.078071 ] ] ] } } , { "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.294434, 40.010787 ], [ -99.294434, 39.926588 ], [ -99.997559, 39.926588 ], [ -99.997559, 39.993956 ], [ -99.931641, 40.010787 ], [ -99.821777, 39.993956 ], [ -99.645996, 40.010787 ], [ -99.492188, 39.977120 ], [ -99.294434, 40.010787 ] ] ] } } @@ -68,8 +61,6 @@ , { "type": "Feature", "properties": { "name": "R" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.448242, 39.757880 ], [ -99.294434, 39.724089 ], [ -99.294434, 39.707187 ], [ -99.448242, 39.740986 ], [ -99.558105, 39.707187 ], [ -99.624023, 39.707187 ], [ -99.722900, 39.740986 ], [ -99.887695, 39.707187 ], [ -99.997559, 39.732538 ], [ -99.997559, 39.749434 ], [ -99.887695, 39.724089 ], [ -99.722900, 39.757880 ], [ -99.624023, 39.724089 ], [ -99.558105, 39.724089 ], [ -99.448242, 39.757880 ] ] ] } } , -{ "type": "Feature", "properties": { "name": "P" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.744873, 39.985538 ], [ -99.755859, 39.985538 ], [ -99.645996, 40.019201 ], [ -99.744873, 39.985538 ] ] ] } } -, { "type": "Feature", "properties": { "name": "A" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.294434, 40.069665 ], [ -99.294434, 40.002372 ], [ -99.316406, 40.019201 ], [ -99.382324, 40.010787 ], [ -99.481201, 39.985538 ], [ -99.645996, 40.019201 ], [ -99.755859, 39.985538 ], [ -99.821777, 39.985538 ], [ -99.920654, 40.019201 ], [ -99.997559, 40.002372 ], [ -99.997559, 40.069665 ], [ -99.294434, 40.069665 ] ] ] } } , { "type": "Feature", "properties": { "name": "B" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -99.316406, 40.019201 ], [ -99.294434, 40.002372 ], [ -99.294434, 39.926588 ], [ -99.997559, 39.926588 ], [ -99.997559, 40.002372 ], [ -99.920654, 40.019201 ], [ -99.810791, 39.985538 ], [ -99.744873, 39.985538 ], [ -99.645996, 40.019201 ], [ -99.481201, 39.985538 ], [ -99.382324, 40.010787 ], [ -99.316406, 40.019201 ] ] ] } } diff --git a/tile.cpp b/tile.cpp index 047d9331..ee301933 100644 --- a/tile.cpp +++ b/tile.cpp @@ -2169,10 +2169,19 @@ long long write_tile(decompressor *geoms, std::atomic *geompos_in, ch if (sf.t == VT_POLYGON) { bool simplified_away_by_reduction = false; + // How much of a ring's length spindle preservation would be able to preserve + std::function spindle_length; + if (!prevent[P_SPINDLES]) { + long long ox = (long long) tx << (32 - z), oy = (long long) ty << (32 - z); + spindle_length = [ox, oy](drawvec const &g, size_t i, size_t j) { + return unshared_perimeter(g, i, j, ox, oy); + }; + } + bool prevent_tiny = prevent[P_TINY_POLYGON_REDUCTION] || (prevent[P_TINY_POLYGON_REDUCTION_AT_MAXZOOM] && z == maxzoom); if (!prevent_tiny && !additional[A_GRID_LOW_ZOOMS]) { - sf.geometry = reduce_tiny_poly(sf.geometry, z, line_detail, &still_need_simplification_after_reduction, &simplified_away_by_reduction, &accum_area, tiny_polygon_size, !prevent[P_SPINDLES]); + sf.geometry = reduce_tiny_poly(sf.geometry, z, line_detail, &still_need_simplification_after_reduction, &simplified_away_by_reduction, &accum_area, tiny_polygon_size, spindle_length); if (simplified_away_by_reduction) { strategy.tiny_polygons++; }