[GH-3118] Rasterize line segments by exact cell traversal instead of fixed-step sampling#3119
Draft
james-willis wants to merge 8 commits into
Draft
[GH-3118] Rasterize line segments by exact cell traversal instead of fixed-step sampling#3119james-willis wants to merge 8 commits into
james-willis wants to merge 8 commits into
Conversation
…ad of fixed-step sampling
…assing assertions
This file contains hidden or 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
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.
Did you read the Contributor Guide?
Is this PR related to a ticket?
[GH-XXX] my subject. Closes Line rasterization uses fixed-step sampling instead of exact cell traversal, dropping boundary pixels (allTouched and LINESTRING) #3118What changes were proposed in this PR?
Rasterizationburned line/ring segments by sampling them at fixed 0.2-pixel steps and flooring each sample to a cell (drawLineBresenham). A cell is only burned if a sample lands inside it, so a cell the segment crosses over a chord shorter than the step could fall between two samples and be missed. This replaced the sampler with exact grid traversal (Amanatides–Woo): the segment is walked from one cell-boundary crossing to the next, so every cell it enters is burned. This fixes bothLINESTRINGrasterization and polygon-boundary selection underallTouched = true(which affectsRS_AsRaster,RS_Clip,RS_ZonalStats/RS_ZonalStatsAll, andRS_SetValues).This PR is stacked on #3113 (the non-square-pixel scanline fix); review that one first.
Small known deviation from GDAL: when a vertex lands exactly on a grid line, the geometry touches a cell only at a corner or edge. This exact traversal burns that cell (a superset consistent with the documented "all pixels touched by the geometry"), whereas GDAL omits it. This only occurs at exact grid-aligned vertices (never on general input) and GDAL's own behaviour there is inconsistent, so it is not pursued here — tracked in #3120.
How was this patch tested?
testAsRasterTouchedPixelsExactTraversal: a polygon boundary and aLINESTRINGthat each clip a pixel over a sub-pixel chord, on a square unit grid; expected matrices from GDAL (rasterio.features.rasterize, all_touched=True). Written first, failed on the sampler.allTouchedfuzz added on [GH-3111] Fix scanline x-intercepts on rasters with non-square pixels #3113 (python/tests/sql/test_rasterize_parity.py) matches GDAL exactly with this fix; its two strict-xfailmarkers (the allTouched polygon corpus and the LineString case) become passing assertions here.RS_AsRaster/RS_Clip/RS_ZonalStats/RS_SetValuesexpectations that had encoded the sampler's missed boundary pixels are updated; each change was verified against GDAL/rasterio.RS_ZonalStatsallTouched count on the edge-case geometry goes 1832 → 1842 (the 10 boundary pixels GDAL also selects). Fullcommonmodule suite passes (1147 tests).Did this PR include necessary documentation updates?