Repository navigation
BUG: Raise informative error for 2D spatial coordinates and fix rio.shape (#848) - #940
Conversation
rmz-oz
left a comment
There was a problem hiding this comment.
fwiw the rio.shape fix also changes what reproject(..., src_geoloc_array=...) returns, which is the route the new error message points to. with a 5x6 array and 2D x/y, master gives rio.shape == (30, 30) and reproject("EPSG:2193", src_geoloc_array=(xx, yy)) comes back 42x23, while rasterio.warp.calculate_default_transform(..., 6, 5, src_geoloc_array=(xx, yy)) gives 8x4. on this branch rioxarray matches rasterio. that's probably also behind the 317 TiB allocation in #848 with a ~2000x2000 grid, so a test for the geoloc path might be worth adding (the new reproject test only checks the error).
|
@VolodymyrLinuxovich mind rebasing your MR? |
a102d30 to
a794e22
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #940 +/- ##
==========================================
- Coverage 95.90% 95.72% -0.19%
==========================================
Files 12 18 +6
Lines 1735 2106 +371
==========================================
+ Hits 1664 2016 +352
- Misses 71 90 +19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Thanks, did it |
|
Thanks @VolodymyrLinuxovich 👍 |
docs/history.rstfor all changes anddocs/rioxarray.rstfor new API. The new exception is picked up by the existingautomodule:: rioxarray.exceptions.Problem
When a DataArray's
x/yspatial coordinates are 2D (a curvilinear grid, like the ERA5 data regridded with xESMF in #848), the bounds, resolution and transform code callsfloat()on coordinate slices and fails with an unhelpful error:rio.width/rio.heightare also wrong in that case, because they used the coordinate's.size, which is the size of the whole 2D array rather than the dimension length.Change
_check_dimensions, used bybounds,resolution,transformandreproject, now raises a newCoordinateDimensionError(aRioXarrayError) when the x or y coordinate isn't 1-dimensional. The message explains the requirement and points toreproject(..., src_geoloc_array=...)for curvilinear grids, as suggested in reprojecting xarray dataset: TypeError: only length-1 arrays can be converted to Python scalars #848.rio.width/rio.heightuseself._obj.sizes[dim], sorio.shapeis correct with 2D coordinates.This doesn't add automatic reprojection of curvilinear grids. It replaces a confusing failure with an actionable one and fixes
shape.Before / after
A 5×6 array with 2D
x/ycoordinates, runningrio.shapeand thenrio.reproject("EPSG:2193"):Tests
bounds,resolution,transform(recalc=True)andreprojectraiseCoordinateDimensionErrorfor a DataArray with 2D coordinates;rio.shapeis correct;boundsraises for a Dataset too.pre-commit(black, isort, flake8, blacken-docs) passes.pylint rioxarray/rates the changed modules 10.00/10.mypy rioxarray/reports no new errors: the same 3 pre-existing errors in_spatial_utils.pyappear onmaster.masterhas the identical set of 91 failing tests. They come from the wheel's GDAL lacking the netCDF/HDF drivers and from numpy 2.5 behavior, not from this change, and CI's conda environment isn't affected.