BUG: Raise informative error for 2D spatial coordinates and fix rio.shape (#848) - #940
Open
VolodymyrLinuxovich wants to merge 1 commit into
Open
VolodymyrLinuxovich wants to merge 1 commit into
VolodymyrLinuxovich wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
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.