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
+91
View File
@@ -56,6 +56,7 @@
#include "geocsv.hpp"
#include "geometry.hpp"
#include "serial.hpp"
#include "polygon.hpp"
#include "options.hpp"
#include "usage.hpp"
#include "mvt.hpp"
@@ -1230,6 +1231,13 @@ int vertexcmp(const void *void1, const void *void2) {
return 1;
}
if (v1->feature < v2->feature) {
return -1;
}
if (v1->feature > v2->feature) {
return 1;
}
return 0;
}
@@ -2043,9 +2051,77 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
rewind(vertex_out);
// Also find the edges that are shared between different polygon features,
// so that spindle preservation can avoid adding strips along them
// that would overlap the neighboring features.
std::string edgename = std::string(tmpdir) + "/edge.XXXXXX";
int edgefd = mkstemp((char *) edgename.c_str());
if (edgefd < 0) {
perror(("mkstemp edgefile " + edgename).c_str());
exit(EXIT_OPEN);
}
unlink(edgename.c_str());
FILE *edge_out = fdopen(edgefd, "w+b");
if (edge_out == NULL) {
perror(edgename.c_str());
exit(EXIT_OPEN);
}
std::atomic<long long> edgepos(0);
// All the vertices with the same central point, which are adjacent after sorting
std::vector<vertex> group;
// The polygon neighbors of the central point, and which feature each is from
std::vector<std::pair<draw, long long>> neighbors;
auto finish_group = [&]() {
// A neighbor that is connected to the central point by more than one
// polygon feature is the other end of a shared edge
neighbors.clear();
for (auto const &g : group) {
if (g.feature >= 0) {
neighbors.emplace_back(g.p1, g.feature);
if (g.p2 != g.p1) {
neighbors.emplace_back(g.p2, g.feature);
}
}
}
std::sort(neighbors.begin(), neighbors.end(), [](std::pair<draw, long long> const &a, std::pair<draw, long long> const &b) {
if (a.first < b.first) {
return true;
} else if (a.first == b.first) {
return a.second < b.second;
}
return false;
});
for (size_t k = 0; k < neighbors.size(); k++) {
size_t l;
bool multiple = false;
for (l = k + 1; l < neighbors.size() && neighbors[l].first == neighbors[k].first; l++) {
if (neighbors[l].second != neighbors[k].second) {
multiple = true;
}
}
if (multiple) {
shared_edge se;
se.ax = group[0].mid.x * (1LL << geometry_scale);
se.ay = group[0].mid.y * (1LL << geometry_scale);
se.bx = neighbors[k].first.x * (1LL << geometry_scale);
se.by = neighbors[k].first.y * (1LL << geometry_scale);
fwrite_check((char *) &se, sizeof(shared_edge), 1, edge_out, &edgepos, "shared edges");
}
k = l - 1;
}
group.clear();
};
vertex prev(draw(VT_MOVETO, 0, 0), draw(VT_MOVETO, 0, 0), draw(VT_MOVETO, 0, 0));
vertex v(draw(VT_MOVETO, 0, 0), draw(VT_MOVETO, 0, 0), draw(VT_MOVETO, 0, 0));
while (fread((void *) &v, sizeof(vertex), 1, vertex_out)) {
if (group.size() > 0 && !(v.mid == group[0].mid)) {
finish_group();
}
group.push_back(v);
if (v.mid == prev.mid && (v.p1 != prev.p1 || v.p2 != prev.p2)) {
long long x = v.mid.x * (1LL << geometry_scale);
long long y = v.mid.y * (1LL << geometry_scale);
@@ -2063,8 +2139,23 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
}
prev = v;
}
finish_group();
fclose(vertex_out);
fflush(edge_out);
if (edgepos > 0) {
// Already sorted, because the vertices were sorted by their central point
// and each group's neighbors were sorted before being written
shared_edges_map = (shared_edge *) mmap(NULL, edgepos, PROT_READ, MAP_PRIVATE, edgefd, 0);
if (shared_edges_map == (shared_edge *) MAP_FAILED) {
perror("mmap shared edges");
exit(EXIT_MEMORY);
}
shared_edges_count = edgepos / sizeof(shared_edge);
}
// The mapping keeps the file alive after the FILE is closed
fclose(edge_out);
}
if (!quiet) {