With -pn, don't add spindle strips that would overlap neighboring features

A spindle strip extends one pixel past the far side of the narrow part it
preserves, so a panhandle between two neighboring features (or any narrow
part whose far side borders another feature) would overlap its neighbors
by up to a pixel, which is unfortunate for datasets that are meant to tile
exactly.

With --no-simplification-of-shared-nodes, the vertex pass already sorts every
polygon vertex with its neighbors to find the shared nodes. Each vertex record
now also carries the feature it came from, and the same pass writes out each
edge that is part of two or more different polygon features, as a sorted
table of half-edges. When a narrow edge's strip would extend past an edge
that is in that table, the strip is left out, so the panhandle collapses as
it used to and the features still tile exactly. A narrow part with open space
on its far side, like a coastal strip whose other side is a shared border,
still gets the strip along its shared side, which extends away from the
neighbor.

The table is only searched for edges that have already been found to be
narrow, not for every vertex, and since simplification may have removed
vertices along a shared border, an edge counts as shared if a shared edge
leaves either of its endpoints in its direction, within the simplification
tolerance. On a synthetic tiling (tests/spindle-shared), spindle
preservation with -pn adds no overlapping pixels at all compared to
--no-spindle-preservation, while without -pn the same data overlaps by
several hundred pixels per zoom level. On ne_10m_admin_0_countries -z8 -pn,
the time difference is within noise.

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 21:31:47 +00:00
parent 1963076817
commit 4352b1498f
14 changed files with 546 additions and 21 deletions
+77 -7
View File
@@ -47,16 +47,21 @@ static const double MIN_ALONG_RING = 4 * THIN_DISTANCE;
// for being close to a nearby edge
static const double SAMPLE_SPACING = 1.0;
shared_edge *shared_edges_map = NULL;
size_t shared_edges_count = 0;
namespace {
struct fedge {
double x1, y1, x2, y2; // unrounded tile coordinates
long long wx1, wy1, wx2, wy2; // world coordinates
long long wx1, wy1, wx2, wy2; // world coordinates, relative to the tile origin
size_t ring; // which ring the edge is part of
double arc; // distance along the ring to the start of the edge
double len; // length of the edge
double minx, miny, maxx, maxy;
bool thin = false;
bool thin = false; // within THIN_DISTANCE of some other edge
bool crosses_shared = false; // and that other edge is shared with another feature
int shared = -1; // is this edge shared with another feature? -1 if not yet known
};
double point_segment_distance_sq(double px, double py, fedge const &e, double &t) {
@@ -204,9 +209,58 @@ void add_strip(drawvec &out, long long x1, long long y1, long long x2, long long
}
}
// Is there an edge shared with another feature that starts at (ax, ay)
// and runs in the direction of (bx, by)? After simplification, the other end
// of the shared edge may no longer be in the geometry, but it must lie within
// the simplification tolerance of this edge.
bool shared_toward(long long ax, long long ay, long long bx, long long by, double tolerance) {
if (shared_edges_count == 0) {
return false;
}
auto first = std::lower_bound(shared_edges_map, shared_edges_map + shared_edges_count, std::make_pair(ay, ax),
[](shared_edge const &se, std::pair<long long, long long> const &p) {
return se.ay < p.first || (se.ay == p.first && se.ax < p.second);
});
double dx = bx - ax;
double dy = by - ay;
double len = std::hypot(dx, dy);
if (len == 0) {
return false;
}
for (auto it = first; it != shared_edges_map + shared_edges_count && it->ay == ay && it->ax == ax; ++it) {
double qx = it->bx - ax;
double qy = it->by - ay;
// how far along this edge, and how far off to the side of it, is the other end?
double along = (qx * dx + qy * dy) / len;
double off = std::fabs(qx * dy - qy * dx) / len;
if (along > 0 && off <= tolerance) {
return true;
}
}
return false;
}
// Is this edge (or what is left of it after simplification) shared with another feature?
bool is_shared(fedge &e, double tolerance, long long ox, long long oy) {
if (shared_edges_count == 0) {
return false;
}
if (e.shared < 0) {
e.shared = shared_toward(e.wx1 + ox, e.wy1 + oy, e.wx2 + ox, e.wy2 + oy, tolerance) ||
shared_toward(e.wx2 + ox, e.wy2 + oy, e.wx1 + ox, e.wy1 + oy, tolerance);
}
return e.shared;
}
} // namespace
drawvec thin_polygon_strips(drawvec const &geom, int z, int detail) {
drawvec thin_polygon_strips(drawvec const &geom, int z, int detail, double simplification, long long ox, long long oy) {
drawvec out;
int shift = 32 - detail - z;
@@ -277,6 +331,9 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail) {
return edges[a].miny < edges[b].miny;
});
// How far a simplified shared border may have strayed from the original shared edges
const double tolerance = (1 + std::max(simplification, 0.0)) * scale;
std::vector<size_t> active;
const double thin_sq = THIN_DISTANCE * THIN_DISTANCE;
bool any = false;
@@ -295,7 +352,9 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail) {
}
active[keep++] = fi;
if (e.thin && f.thin) {
// Once both edges are known to be thin (and, if there are shared
// edges, to need no strip anyway), there is nothing more to learn.
if (e.thin && f.thin && (shared_edges_count == 0 || (e.crosses_shared && f.crosses_shared))) {
continue;
}
if (f.maxx + THIN_DISTANCE < e.minx || e.maxx + THIN_DISTANCE < f.minx) {
@@ -324,13 +383,21 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail) {
continue;
}
if (!e.thin && close_along(e, f, ring_perimeters)) {
// A strip along `e` would extend past `f`, so if `f` is an edge
// shared with another feature, the strip would overlap that feature.
if ((!e.thin || (shared_edges_count > 0 && !e.crosses_shared)) && close_along(e, f, ring_perimeters)) {
e.thin = true;
any = true;
if (is_shared(f, tolerance, ox, oy)) {
e.crosses_shared = true;
}
}
if (!f.thin && close_along(f, e, ring_perimeters)) {
if ((!f.thin || (shared_edges_count > 0 && !f.crosses_shared)) && close_along(f, e, ring_perimeters)) {
f.thin = true;
any = true;
if (is_shared(e, tolerance, ox, oy)) {
f.crosses_shared = true;
}
}
}
active.resize(keep);
@@ -341,8 +408,11 @@ drawvec thin_polygon_strips(drawvec const &geom, int z, int detail) {
return out;
}
// A narrow part whose far side is an edge shared with another feature,
// like a panhandle between two neighboring political units, is left to
// collapse, so that the features still tile exactly instead of overlapping.
for (auto const &e : edges) {
if (e.thin) {
if (e.thin && !e.crosses_shared) {
add_strip(out, e.wx1, e.wy1, e.wx2, e.wy2, shift);
}
}