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
+112 -34
View File
@@ -530,6 +530,15 @@ static void rewrite(serial_feature const &osf, int z, int nextzoom, int maxzoom,
}
}
// What the first pass of simplification (simplifying and rounding a feature)
// leaves for the second pass (cleaning it) to use
struct simplified_feature {
double area = 0; // in world coordinates, before simplification
drawvec strips; // spindle strips to union with the cleaned polygon
drawvec world; // the polygon as simplified, but before rounding
bool gridded = false; // stairstepped onto a grid
};
// This is the parameter block passed to each simplification worker thread
struct simplification_worker_arg {
std::vector<std::shared_ptr<serial_feature>> *features = NULL;
@@ -540,6 +549,14 @@ struct simplification_worker_arg {
node *shared_nodes_map;
size_t nodepos;
std::string const *shared_nodes_bloom;
// 0: simplify, round, and clean each feature.
// 1: only simplify and round, leaving the rest in `simplified`.
// 2: only clean, snapping to the hot pixels of the feature's layer
// in `layer_hot_pixels`, so that shared edges snap consistently.
int pass = 0;
std::vector<simplified_feature> *simplified = NULL;
std::map<long long, hot_pixel_list> const *layer_hot_pixels = NULL;
};
// If a polygon has collapsed away to nothing during polygon cleaning,
@@ -668,52 +685,80 @@ static void *simplification_worker(void *v) {
std::vector<std::shared_ptr<serial_feature>> *features = a->features;
for (size_t i = a->task; i < (*features).size(); i += a->tasks) {
double area = 0;
area = simplify_feature(&*((*features)[i]), *(a->shared_nodes), a->shared_nodes_map, a->nodepos, *(a->shared_nodes_bloom));
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;
simplified_feature local;
simplified_feature &sf = (a->simplified != NULL) ? (*a->simplified)[i] : local;
if (a->pass != 2) {
sf.area = simplify_feature(&*((*features)[i]), *(a->shared_nodes), a->shared_nodes_map, a->nodepos, *(a->shared_nodes_bloom));
// stairstepped onto a grid, which strips would not follow
sf.gridded = additional[A_GRID_LOW_ZOOMS] && z < (*features)[i]->maxzoom;
drawvec geom = (*features)[i]->geometry;
// Before rounding to tile coordinates, find any parts of the polygon
// that are too narrow to survive the rounding, so they can be
// restored as one-pixel-wide strips after cleaning.
// (Not when the geometry has been stairstepped onto a grid, which the strips would not follow.)
if (t == VT_POLYGON && !prevent[P_SPINDLES] && !sf.gridded) {
sf.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)
if (t == VT_POLYGON && !sf.gridded && !prevent[P_SPINDLES]) {
sf.world = geom;
}
to_tile_scale(geom, z, out_detail);
(*features)[i]->geometry = std::move(geom);
if (a->pass == 1) {
continue;
}
}
drawvec geom = (*features)[i]->geometry;
// Before rounding to tile coordinates, find any parts of the polygon
// that are too narrow to survive the rounding, so they can be
// 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] && !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) {
// Scaling may have made the polygon degenerate.
// Give Clipper a chance to try to fix it.
{
drawvec before = geom;
// we can try scaling up because this is now tile scale
coalesce_polygon(geom, true);
geom = union_polygon_strips(geom, strips);
// If there is a set of hot pixels for the whole layer, snap to them
// so that edges shared with other features are snapped identically.
hot_pixel_list const *hot = NULL;
if (a->layer_hot_pixels != NULL) {
auto found = a->layer_hot_pixels->find((*features)[i]->layer);
if (found != a->layer_hot_pixels->end()) {
hot = &found->second;
}
}
bool cleaned = false;
if (hot != NULL && geom.size() < 100000) {
geom = clean_polygon_with_hot_pixels(before, *hot, &cleaned);
if (!cleaned) {
geom = before;
}
}
if (!cleaned) {
// we can try scaling up because this is now tile scale
coalesce_polygon(geom, true);
}
geom = union_polygon_strips(geom, sf.strips, hot);
if (additional[A_DEBUG_POLYGON]) {
check_polygon(geom);
}
if (geom.size() < 3) {
if (area > 0) {
if (sf.area > 0) {
// 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
@@ -724,16 +769,16 @@ static void *simplification_worker(void *v) {
// so that its neighbors close over the space without overlapping.
geom.clear();
bool all_shared = false;
if (world.size() > 0) {
geom = union_polygon_strips(drawvec(), outline_strips(world, z, out_detail, (*features)[i]->simplification,
(long long) (*features)[i]->tx << (32 - z), (long long) (*features)[i]->ty << (32 - z), &all_shared));
if (sf.world.size() > 0) {
geom = union_polygon_strips(drawvec(), outline_strips(sf.world, z, out_detail, (*features)[i]->simplification,
(long long) (*features)[i]->tx << (32 - z), (long long) (*features)[i]->ty << (32 - z), &all_shared));
}
if (geom.size() < 3) {
if (all_shared) {
geom.clear();
} else {
// area is in world coordinates, calculated before scaling down
geom = revive_polygon(before, area, z, out_detail);
geom = revive_polygon(before, sf.area, z, out_detail);
}
}
} else {
@@ -747,6 +792,9 @@ static void *simplification_worker(void *v) {
(*features)[i]->index = i;
}
(*features)[i]->geometry = std::move(geom);
// The intermediate state is no longer needed
sf = simplified_feature();
}
return NULL;
@@ -2517,7 +2565,30 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch
tasks = 1;
}
{
// With --no-simplification-of-shared-nodes, features that share borders are
// meant to tile exactly, so after they have all been simplified and rounded,
// find the hot pixels of all the polygons in each layer, so that each polygon
// can be snapped to them and the borders they share are snapped identically.
bool consistent_snapping = prevent[P_SIMPLIFY_SHARED_NODES];
std::vector<simplified_feature> simplified;
std::map<long long, hot_pixel_list> hot_pixels_by_layer;
if (consistent_snapping) {
simplified.resize(features.size());
}
for (int snap_pass = consistent_snapping ? 1 : 0; snap_pass <= (consistent_snapping ? 2 : 0); snap_pass++) {
if (snap_pass == 2) {
std::map<long long, std::vector<drawvec const *>> layer_polygons;
for (auto const &f : features) {
if (f->t == VT_POLYGON) {
layer_polygons[f->layer].push_back(&f->geometry);
}
}
for (auto const &lp : layer_polygons) {
hot_pixels_by_layer[lp.first] = layer_hot_pixels(lp.second);
}
}
std::vector<pthread_t> pthreads(tasks);
std::vector<simplification_worker_arg> args;
args.resize(tasks);
@@ -2529,6 +2600,13 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch
args[i].shared_nodes_map = shared_nodes_map;
args[i].nodepos = nodepos;
args[i].shared_nodes_bloom = &shared_nodes_bloom;
args[i].pass = snap_pass;
if (consistent_snapping) {
args[i].simplified = &simplified;
}
if (snap_pass == 2) {
args[i].layer_hot_pixels = &hot_pixels_by_layer;
}
if (tasks > 1) {
if (thread_create(&pthreads[i], NULL, simplification_worker, &args[i]) != 0) {