Repository navigation
Return the data pixel for point queries on pixel corners shared with nodata - #321
DresdenGman wants to merge 1 commit into
Conversation
Points falling exactly on a pixel edge or corner are equidistant from every pixel sharing the boundary. When the selected pixel was nodata, point_query returned None even if another touching pixel had data. For interpolate='nearest', check the other pixels that touch the point when the selected pixel is nodata and the point lies exactly on a pixel boundary. For bilinear's nodata fallback, examine the 2x2 cells in order of distance from the point instead of a single fixed cell. Fixes perrygeo#294
perrygeo
left a comment
There was a problem hiding this comment.
Thanks @DresdenGman
I'd like to remove the changes to bilinear since they are separate and change behavior.
The NN corner case is a good idea in theory, I think we can improve the implementation. See comments.
| val = arr[i, j] | ||
| if val is not masked: | ||
| return val.item() | ||
| return None |
There was a problem hiding this comment.
This is a potential bug, or at least a significant behavior change. Consider a 2x2 raster region with a single nodata cell. Any point query directly on that cell would previously return None. This is correct - you can't interpolate null data. The only other valid response would be an exception. Any numeric response to this scenario, especially if its the value of a cell I didn't query, is a bug (I should have a test case to assert this!)
Performance wise, this could be a deep regression in some cases. It's a shame to do all that work to select one of three options. I'd prefer a fast heuristic ('start upper left, go clockwise").
Finally, this code path really doesn't have anything to do with the pixel corners issue. It handles generic nodata encounters, where the behavior is already correct and well established.
So TLDR, I'd like to remove this section from the PR. Along with associated tests. We could pick this up in a separate PR if you'd like.
| if val is masked: | ||
| # The point may fall exactly on a pixel edge or | ||
| # corner, making it equidistant from every pixel | ||
| # that shares the boundary. If the selected pixel | ||
| # has no data, check the other pixels that touch | ||
| # the point before giving up. | ||
| fcol, frow = ~rast.affine * (x, y) | ||
| rows = [r, r - 1] if frow == r else [r] | ||
| cols = [c, c - 1] if fcol == c else [c] | ||
| if len(rows) * len(cols) > 1: | ||
| if not boundless: | ||
| rows = [i for i in rows if 0 <= i < rast.shape[0]] | ||
| cols = [j for j in cols if 0 <= j < rast.shape[1]] | ||
| r0, c0 = min(rows), min(cols) | ||
| window = ( | ||
| (r0, max(rows) + 1), | ||
| (c0, max(cols) + 1), | ||
| ) | ||
| src_array = rast.read( | ||
| window=window, masked=True, boundless=boundless | ||
| ).array | ||
| for i in rows: | ||
| for j in cols: | ||
| val = src_array[i - r0, j - c0] | ||
| if val is not masked: | ||
| break | ||
| if val is not masked: | ||
| break |
There was a problem hiding this comment.
While this code may do the job, it's not optimal and does lots of calculation plus an extra round trip.
Since there is no correct answer (its effectively random choice) can we use a heuristic instead - start with the upper left neighbor and move clockwise.
For performance optimization, to avoid an extra round trip, we could always read src_array as a 3x3 window (which should be exactly as fast as reading a 1x1 due to GDAL's block design).
Something like:
# read with 3x3 window always
val = src_array[1, 1]
if val is masked:
... # use simple heuristic to pick a different cell
Fixes #294.
Point queries on a pixel corner (or shared edge) are equidistant from every pixel touching the point, but the two interpolation modes picked one of them blindly:
nearestfloored its way to a single pixel andbilinear's masked fallback used round() on a single cell. If that pixel happened to be nodata, the query returned None even when another pixel sharing the corner had data, and the two modes disagreed with each other. On the example from the issue,nearestreturned[None, None, 1, None]andbilinear[None, 1, None, None].The fix only changes the case where the selected pixel has no data:
If every touching pixel is nodata the result stays None, and points not on a boundary are unaffected. The issue example now returns
[1, 1, 1, 1]under both modes.Tests: two new tests in
tests/test_point.py— one for the corner/edge regression (fails before, passes after), one guarding that all-nodata neighborhoods still return None. Full suite: 117 passed / 5 skipped before, 119 passed / 5 skipped after, no regressions; ruff check and format clean.