With -pn, snap-round each layer's polygons consistently with each other

Wagyu snaps each edge to every "hot pixel" (vertex or rounded intersection)
that it passes through. Each polygon was cleaned on its own, with only its
own hot pixels, so a border shared by two polygons could be bent through a
hot pixel in one of them and not in the other, leaving a shard gap or overlap
between them even though --no-simplification-of-shared-nodes had simplified
and rounded the border identically in both. (In Dane County tile 11/515/751,
rounding moved one of a block's own vertices across its border with a sliver,
and cleaning bent the block's copy of the border through a hot pixel that the
sliver's copy didn't have.)

Wagyu now has hot_pixels(), which returns the hot pixels of the rings added
to it, and add_hot_pixels(), which gives execute() more hot pixels to snap
to. When extra hot pixels are given, edges are snapped to every hot pixel
they touch, including corner touches, so that whether a shared edge is
snapped to a hot pixel can't depend on which polygon it is in.

With -pn, the simplification worker now runs in two passes. The first
simplifies and rounds every feature in the tile. Then the hot pixels of all
the polygons in each layer are found together, which also finds crossings
between features that no single feature would see. The second pass cleans
each polygon with the layer's hot pixels near it as well as its own, so
the borders it shares are snapped identically in each polygon. This is
snap rounding of the whole layer's arrangement at once, so the result is
still free of crossings.

For Dane County Census blocks at -D8 -d8 -pt -pn, tiles 12/1031/1503 and
11/515/751 now have no gaps between blocks (from 10 and 23 pixels), and the
gaps and overlaps at z9 and z10 go from 55/113 and 155/175 to 13/56 and 23/59
pixels, with no self-intersecting polygons. Natural Earth 10m countries at
-z8 -pn take about 7% longer.

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-12-01 00:09:18 +00:00
parent 5e1240b341
commit 162cb1c1b0
12 changed files with 366 additions and 99 deletions
+73 -3
View File
@@ -523,7 +523,33 @@ static void add_rings(mapbox::geometry::wagyu::wagyu<long long> &wagyu, drawvec
// at `scale` times tile resolution, and round the result back to tile resolution.
// Returns false, rather than exiting, if wagyu fails, since the callers have
// a valid polygon to fall back on.
static bool wagyu_union(drawvec const &subject, drawvec const *clip, long long scale, drawvec &out, bool require_exact) {
// The hot pixels from `hot` (sorted by y, then x) that are within a pixel of
// the bounding box of `geom`, which are the only ones its edges could touch
static std::vector<mapbox::geometry::point<long long>> hot_pixels_near(drawvec const &geom, hot_pixel_list const &hot) {
std::vector<mapbox::geometry::point<long long>> out;
if (geom.size() == 0 || hot.size() == 0) {
return out;
}
long long minx = LLONG_MAX, miny = LLONG_MAX, maxx = LLONG_MIN, maxy = LLONG_MIN;
for (auto const &d : geom) {
minx = std::min(minx, (long long) d.x);
miny = std::min(miny, (long long) d.y);
maxx = std::max(maxx, (long long) d.x);
maxy = std::max(maxy, (long long) d.y);
}
for (long long y = miny - 1; y <= maxy + 1; y++) {
auto it = std::lower_bound(hot.begin(), hot.end(), std::make_pair(y, minx - 1));
for (; it != hot.end() && it->first == y && it->second <= maxx + 1; ++it) {
out.emplace_back(it->second, it->first);
}
}
return out;
}
static bool wagyu_union(drawvec const &subject, drawvec const *clip, long long scale, drawvec &out, bool require_exact, hot_pixel_list const *hot = NULL) {
mapbox::geometry::multi_polygon<long long> result;
try {
@@ -532,6 +558,16 @@ static bool wagyu_union(drawvec const &subject, drawvec const *clip, long long s
if (clip != NULL) {
add_rings(wagyu, *clip, mapbox::geometry::wagyu::polygon_type_clip, scale);
}
if (hot != NULL && scale == 1) {
drawvec both = subject;
if (clip != NULL) {
both.insert(both.end(), clip->begin(), clip->end());
}
std::vector<mapbox::geometry::point<long long>> near = hot_pixels_near(both, *hot);
if (near.size() > 0) {
wagyu.add_hot_pixels(near);
}
}
wagyu.execute(mapbox::geometry::wagyu::clip_type_union, result, mapbox::geometry::wagyu::fill_type_positive, mapbox::geometry::wagyu::fill_type_non_zero);
} catch (std::runtime_error &e) {
return false;
@@ -555,7 +591,7 @@ static bool wagyu_union(drawvec const &subject, drawvec const *clip, long long s
return true;
}
drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips) {
drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips, hot_pixel_list const *hot) {
if (strips.size() == 0) {
return geom;
}
@@ -586,8 +622,14 @@ drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips) {
// Clean the rounded union at tile resolution, the same way
// clean_or_clip_poly() would, but without exiting on failure.
// (Or, if there is a set of hot pixels to snap to consistently
// with other features, at tile resolution with those.)
drawvec out;
if (wagyu_union(unioned, NULL, 16, out, true)) {
if (hot != NULL) {
if (wagyu_union(unioned, NULL, 1, out, false, hot)) {
return out;
}
} else if (wagyu_union(unioned, NULL, 16, out, true)) {
return out;
}
if (wagyu_union(unioned, NULL, 1, out, false)) {
@@ -598,3 +640,31 @@ drawvec union_polygon_strips(drawvec const &geom, drawvec const &strips) {
}
return geom;
}
hot_pixel_list layer_hot_pixels(std::vector<drawvec const *> const &geoms) {
hot_pixel_list out;
try {
mapbox::geometry::wagyu::wagyu<long long> wagyu;
for (auto const *g : geoms) {
add_rings(wagyu, *g, mapbox::geometry::wagyu::polygon_type_subject, 1);
}
for (auto const &p : wagyu.hot_pixels()) {
out.emplace_back(p.y, p.x);
}
} catch (std::runtime_error &e) {
// Without a consistent set of hot pixels, each feature is
// just snapped to its own, as it would otherwise be.
out.clear();
}
std::sort(out.begin(), out.end());
out.erase(std::unique(out.begin(), out.end()), out.end());
return out;
}
drawvec clean_polygon_with_hot_pixels(drawvec const &geom, hot_pixel_list const &hot, bool *ok) {
drawvec out;
*ok = wagyu_union(geom, NULL, 1, out, false, &hot);
return out;
}