Consistent snapping keeps the borders of polygons in a layer from crossing,
but where rounding turned a narrow polygon between two others inside out
(for example, a Census block thinner than a pixel whose two sides cross
when rounded to tile coordinates), the area it covered is covered by both of
its neighbors, and snapping can't decide which of them it belongs to.
So after cleaning the polygons of each layer, find the ones that overlap:
two polygons that share an edge in the same direction both cover the area
beside it (neighbors that tile exactly traverse their shared border in
opposite directions), and where their borders don't share any edges, a
vertex of one is strictly inside the other. Then remove each overlapping
area from all but the polygon with the lowest sequence number, using the
layer's hot pixels so the result stays consistent with the rest.
For Dane County Census blocks at -D8 -d8 -pt -pn, this reduces the
overlapping pixels at z8, z9, and z10 from 41, 61, and 61 to 7, 8, and 16,
without changing the gaps much or taking noticeably longer, and with no
self-intersecting polygons.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
With consistent snapping, edges are snapped to every hot pixel they touch,
including those whose corners they only touch. But with closed pixels, an
edge passing diagonally through a lattice point touches the corners of the
two pixels beside it as well, and is snapped into a zigzag through all four.
Two edges from different polygons that cross at a pixel corner zigzag
through the same pixels in different orders, and the zigzags cross each
other. (In Dane County tile 10/258/376, this left a quarter-pixel gap and a
quarter-pixel overlap between two blocks on either side of a sliver.)
Hobby's snap rounding, whose guarantee that snapped edges don't cross is
what makes snapping to a common set of hot pixels work, uses half-open
pixels, [x - 0.5, x + 0.5) by [y - 0.5, y + 0.5), consistent with rounding
halves up, which is how wagyu rounds intersection points. So when snapping
to a common set of hot pixels, find the range of pixels an edge passes
through in each row that way, so that a point on a pixel boundary belongs
to only one pixel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
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
A polygon that collapses away entirely is replaced by its outline, but with
--no-simplification-of-shared-nodes that outline overlaps the neighboring
features along every shared border, and those features already cover the
space. So leave the shared borders out of the outline, as the spindle strips
already do, and if every border is shared, leave the polygon out entirely
instead of falling back to a rectangle.
For Dane County Census blocks at -D8 -d8 -pt -pn, this reduces the
overlapping pixels in tile 12/1031/1503 from 141 to 11 and in 11/515/751
from 915 to 38, and the number of revived rectangles at z8 from 6307 to
1414, at the cost of a few more one-pixel gaps (7 to 10 and 14 to 23) where
a block of less than a pixel is left out and its neighbors' borders don't
round to the same line.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
When a polygon collapsed away to nothing during cleaning, revive_polygon()
replaced it with a rectangle with the polygon's area, centered on the
average of its vertices. That looked bad for two reasons. Most polygons that
collapse are much smaller than a pixel, and the rectangle is at least a
pixel, so it was usually far too big: for Dane County Census blocks at
-D8 -d8 -pt -pn, 88% of the 27,206 revived polygons had less than half a
pixel of area, and the median rectangle had 11 times the area of the polygon
it replaced. And the larger polygons that collapse are almost all long and
narrow, like blocks between roads, so a square at their midpoint looked
nothing like them.
Now a polygon that collapses entirely is first replaced by the union of
one-pixel strips along every edge of its simplified outline, which traces
its shape in the right place. Only if that is empty too, because the whole
polygon is within a pixel or so, is it replaced by the rectangle, which is
then a single pixel. This is part of spindle preservation, so with
--no-spindle-preservation the rectangle is still used.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
The previous change, which stopped wagyu from snapping an edge to a hot
pixel whose corner it only touches, made two of upstream wagyu's 3320
geometry fixtures produce spikes. When an intersection lies exactly on a
pixel corner, it is rounded to that pixel, and the edges that intersect
there must be snapped to it; and once an edge has been snapped to one hot
pixel, its new segments may pass through the interior of another hot pixel
whose corner the original edge only touched.
So now the inward rounding of the hot pixel range only applies to an edge
that intersects no other edge (which is recorded while the hot pixels are
built), and never to a hot pixel that is the rounded location of an
intersection (which are now kept in a separate sorted list). Edges that are
part of intersections are snapped exactly as before. With this, upstream
wagyu's unit tests and all 3320 of its geometry fixtures pass, and the
narrow diagonal Census block in Dane County is still preserved.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
Wagyu's snap rounding finds the hot pixels (pixels containing a vertex or
intersection) that each edge passes through, row by row, by rounding the
x range that the edge covers within the row outward. A value exactly
halfway between two integers was rounded outward too, so a pixel whose
corner the edge merely touched counted as one it passed through.
For a 45-degree edge, the x range always ends exactly on pixel corners,
so two parallel diagonal edges one unit apart were each snapped to the
other's endpoints, and a perfectly valid diagonal polygon one unit wide
(for example, a narrow Census block between two others, after rounding to
tile coordinates) was collapsed to nothing by cleaning, leaving a diagonal
gap between the blocks on either side of it.
Round halfway values inward instead, for this purpose only (the rounding
of intersection points is unchanged). On Dane County census blocks at
-D8 -d8 -pn -pt, this removes about 10-20% of the gaps between blocks at
z9 and z10, without introducing any self-intersecting polygons (checked
independently with exact orientation tests at z6-z12). Upstream wagyu's
rounding is unchanged since 2020.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
Features don't necessarily have IDs, and sequence numbers are only unique
within a layer and a reading segment: the flatgeobuf, geobuf, and CSV
readers start every segment's sequence at the same offset and parse in
parallel, so two different features can have the same seq. Record all
three in each vertex record, and only count an edge as shared if it is
part of polygons that differ in any of them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
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
On arm64, clang contracts floating point multiply-adds into FMA
instructions, which changes wagyu's intersection arithmetic enough that
it can fail on some strip unions with "Trying to find intersection of
lines that do not intersect" (seen with ne_10m_admin_0_countries at
-D10 -d10 -zg; reproducible on x86 with -mfma -ffp-contract=fast, where
2 of 4426 unions failed).
The strips are only an improvement on an already-valid cleaned polygon,
so if the union fails at 16x resolution, retry it at tile resolution,
and if that fails too, keep the cleaned polygon without strips. The
re-cleaning of the union after rounding it back to tile resolution also
no longer goes through clean_or_clip_poly(), which exits on failure, but
does the same thing (16x if the result is exact, otherwise 1x) with the
same fallbacks. Output where wagyu doesn't fail is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
A polygon (or part of a polygon) narrower than a tile pixel, like a
1-meter buffer around a long linestring, used to collapse away to nothing
at most zoom levels when it was rounded to tile coordinates, except for
scattered fragments wherever the rounding happened to keep its two sides
apart.
Now, before the geometry is rounded, thin_polygon_strips() looks for edges
that have an oppositely-directed edge less than sqrt(2) tile units away
(the most that rounding can move two boundaries toward each other) and
that is not just the other side of a corner (the two points must also be
far apart along the ring). Candidate pairs are found with a sweep over the
edges' bounding boxes, so polygons without narrow parts cost little more
than sorting their edges. Each narrow edge gets a one-unit-wide strip, one
side of which is the edge as it will be rounded and the other side of which
is offset one unit toward the polygon's interior, so the strips lie within
the polygon wherever it is already thick enough, and they are unioned with
the cleaned polygon. The union is done at 16x resolution, because the
strips from the two sides of a spindle are nearly coincident and wagyu's
own snap rounding would otherwise collapse them in places.
Long, narrow polygons whose perimeter is more than 8 times the tiny polygon
size are also no longer combined into tiny-polygon squares, so a spindle
stays visible down to the zoom level where it is shorter than that.
--no-spindle-preservation restores the old behavior.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ