-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Wagyu with a new polygon cleaner #164
Draft
e-n-f
wants to merge
51
commits into
main
Choose a base branch
from
clean-polygons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e-n-f
changed the title
Another attempt to fix the spindle polygons
Replace Wagyu with a new polygon cleaner
Nov 14, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR replaces Tippecanoe's use of Wagyu to clean polygons with a new polygon cleaning implementation in
polygon.cpp
.Like Wagyu, it is intended to take whatever mess of self-contradictory polygon geometry it is given and produce a new polygon or multipolygon that meets the OGC standards for "simple" and "valid" polygon geometries.
The steps of cleaning are:
Scaling
Scales the geometry from its source resolution to the output tile resolution, and attempts to correct winding errors introduced during scaling by jittering the coordinates of the central vertex of ears whose winding differs from the winding of the same ear in the source geometry.
Wagyu does not have the opportunity to correct windings here because it starts from pre-scaled geometry.
Decomposition
The scaled rings are decomposed into a set of directed edges. The information about which edges originally came from the same ring is discarded.
Spike and spindle correction
Any pairs of edges that have the same endpoints but opposite orientations are discarded. These may be "spikes" (narrow peninsulas or inlets) from a single ring, "spindles" where narrow polygon rings have collapsed to a line during scaling, or borders where an outer ring and an inner ring meet and should be merged into a single crescent ring.
As a special case, if a spike or spindle is more than 5 pixels long, it is assumed to have visual significance. Rather than being discarded, each of the two edges is split into two and bowed out slightly at the center, so the pair will display as a long, narrow diamond shape rather than being lost. (This unfortunately may introduce new self-intersections or new shard overlaps with adjacent features, or even new spindles or spikes. So any altered edges are reinspected after snap rounding until the set of edges stabilizes.)
Wagyu eliminates matched pairs of edges, but later in the process, and does not have the special case to try to convert them back into viable polygon rings.
Snap rounding
Pairs of edges from the scaled geometry that might intersect are inspected to determine whether they do intersect. If they do (and the intersection is not at one of the endpoints of the edge), the edge is split into two new edges, one from each existing endpoint to the intersection points.
The edges that have been altered are reinspected for spikes and spindles and intersections, and are re-split if necessary until the set of edges stabilizes so that edges intersect only at their endpoints.
Wagyu snap-rounds any two edges that enter the same pixel rather than only those that edges actually intersect, which provides more opportunities for accidentally introducing shard gaps. MapLibre rendering does not appear to actually require this higher level of snap-rounding.
Ring reassembly
From the surviving and modified set of edges, Tippecanoe reconstructs polygon rings. It starts from an arbitrary edge and walks the connections from that point until it finds a point that it has already visited, which may not be the starting point of the walk. When there are multiple paths leading out from a point, it follows the sharpest available left turn, which (in the Y-down tile coordinate space), which should always yield the largest outer ring or the smallest inner ring that can be reached from the starting point. After a ring has been found, it is removed from the set of edges, and more rings are sought until all the edges are consumed.
Wagyu instead builds its rings from the top down, both sides at once, following Vatti's algorithm.
Ring hierarchy determination
At this point, Wagyu does its checks for rings that cross and for spikes that need to be removed, which can cause rings to be split into two or for parts of rings to be reassigned to other rings. Tippecanoe has already removed the spikes, and it should not have been possible for the ring reconstruction to have created any rings that cross.
Both Tippecanoe and Wagyu now inspect the rings to determine which rings are children of which other rings. In the Tippecanoe case, this is done by choosing an interior point for each ring, starting from the largest, in one of the ring's ears, and using
pnpoly
to determine what other ring, if any, that point appears in, starting from the smallest (and therefore most inner) ring that has already been placed in the hierarchy.The final stage is to remove any inner rings that somehow exist at the top level of the hierarchy, since there is nothing for them to be cut away from. As required by the vector tile spec, each outer ring is followed by the inner rings that cut away parts of it.