-
Notifications
You must be signed in to change notification settings - Fork 84
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
Inconsistent behaviour of clip_box #617
Comments
The the |
len(da1_clipped.y),len(da2_clipped.y)
This is true: xr.testing.assert_equal(da1_clipped.y[1:], da2_clipped.y) |
When clipping rasters, you cannot be guaranteed that everything will mach up exactly in your example here. Floating point issues and other things can have an impact on the behavior of the clip. If you want to join them together, I recommend using merge: from rioxarray.merge import merge_arrays
da_merged = merge_arrays([da1_clipped, da2_clipped]) |
I'm very surprised. I was using da1_clipped = da1.sel(x=slice(bounds[0], bounds[2]), y=slice(bounds[3], bounds[1]))
da2_clipped = da2.sel(x=slice(bounds[0], bounds[2]), y=slice(bounds[3], bounds[1]))
xr.testing.assert_equal(da1_clipped.y, da2_clipped.y) # no exception raised Out of curiosity, why are the results of |
The implementation is different. clip_box uses rasterio to determine the window to select: rioxarray/rioxarray/raster_array.py Line 808 in ca31364
numeric precision differences when calculating the transform can cause there to be a slight offset. |
One part I forgot to mention is that the example you showed only considers the centroid of the grid cell whereas the rasterio version considers the entire grid cell. |
I can see how that's one difference, but wouldn't that impact the "left" and "right" rasters ( |
Not necessarily. Depending on how close the boundary is to the edge of the grid cells, floating point precision differences could make it include grid cells on one side versus the other. Also, the boundary may be slightly shifted compared to the raster. If the boundary or raster is shifted, there can be differences in the resulting raster. |
If you want to be sure everything matches up exactly on both sides, one option is to merge the rasters before clipping. |
I dug into the code and here are the rasterio Windows used for clipping: da1:
da2:
Note that And that is why there is an additional row for one and not the other. It all comes down to floating point precision issues. |
Thanks for looking into this, any reason why |
" it uses math.floor to select the index of the row to ensure any intersecting cells are added." |
Problem description
I am clipping two rasters which are side by side along the x-axis (so they share the same y-axis coordinates) with the same box polygon, so expect the outputs to also share the y-axis coordinates, but they don't.
Expected Output
I expect the outputs to also share the y-axis coordinates.
Environment Information
Installation method
pypi
The text was updated successfully, but these errors were encountered: