-
Notifications
You must be signed in to change notification settings - Fork 522
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
Coordinate contains check #2901
base: main
Are you sure you want to change the base?
Conversation
@groutr how about a different numerical approach? If we apply the inverse of the raster's transform to the x, y coords we get column and row indices, right? Then we can compare these to the raster's width and height. It would account for rotation, too, unless I'm mistaken. |
@sgillies Using the transform is definitely a more precise method. In my use case, I wasn't dealing with rotated rasters and an approximate check was more than good enough. I was attempting to stay consistent with the other functions in coords.py by not requiring a transform. I guess the question to decide is do we want an approximate check using just the bounds, or a precise check using the transform. I don't have a strong opinion either way. The goal is to avoid unnecessary reads. |
@groutr I'm seeing more and more rotated rasters at my new day job. Umbra's open data, for example, is generally rotated.
I think it would be good if new API features were rotation correct. |
I can make the check use transforms. Should it live in the transform.py module or stay in coords.py? Also, I'm trying to dig into why rowcol transforms, especially for scalar coordinates seem slow and if there is a way to speed it up. On my system
EDIT: I'm aware that for a contains check, I can just use the transform directly to avoid all the overhead of the rowcol function. I'm curious where the overhead is coming from though. |
In looking at this some more, I realized that I think a good solution here might be an additional method, |
@groutr no, let's not extend TransformMethodsMixin. |
@sgillies Are you thinking something more like this? def contains_coord(transform, height, width, coords):
"""Test if coord is contained in bounds.
Coordinate and bounds are assumed to be relative to the same CRS.
Parameters
----------
bounds: BoundingBox
coord: Sequence
xy coordinate
Returns
-------
boolean
``True`` if coord is contained in bounding box
``False`` otherwise
"""
x, y = coords
rows, cols = rowcol(transform, x, y)
rows = np.asarray(rows)
cols = np.asarray(cols)
return (rows >= 0) & (rows < height) & (cols >= 0) & (cols < width) |
Implements a useful utility function for checking of a coordinate is contained within the bounding box of a raster. The use case that prompted this was testing to see which rasters covered a specific location(s). Testing this before reading seemed a much simpler route than checking to see if the read pixel was a nodata value.
It's a small utility function designed to make life a little more simple and the coord module seemed like the perfect place for this to live.