Skip to content

BUG: Raise informative error for 2D spatial coordinates and fix rio.shape (#848) - #940

Open
VolodymyrLinuxovich wants to merge 1 commit into
corteva:masterfrom
VolodymyrLinuxovich:fix-848-2d-coordinate-errors
Open

VolodymyrLinuxovich wants to merge 1 commit into
corteva:masterfrom
VolodymyrLinuxovich:fix-848-2d-coordinate-errors

Conversation

@VolodymyrLinuxovich

Copy link
Copy Markdown

Problem

When a DataArray's x/y spatial coordinates are 2D (a curvilinear grid, like the ERA5 data regridded with xESMF in #848), the bounds, resolution and transform code calls float() on coordinate slices and fails with an unhelpful error:

TypeError: only 0-dimensional arrays can be converted to Python scalars

rio.width/rio.height are 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

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/y coordinates, running rio.shape and then rio.reproject("EPSG:2193"):

master:  shape: (30, 30)
         TypeError: only 0-dimensional arrays can be converted to Python scalars
this PR: shape: (5, 6)
         CoordinateDimensionError: The x coordinate must be 1-dimensional to determine the bounds,
         resolution, or transform; it has 2 dimensions. To reproject data on a curvilinear grid,
         pass the 2D coordinates to 'reproject' using the 'src_geoloc_array' keyword argument.

Tests

  • Six new tests: bounds, resolution, transform(recalc=True) and reproject raise CoordinateDimensionError for a DataArray with 2D coordinates; rio.shape is correct; bounds raises 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.py appear on master.
  • Full suite, locally (Python 3.12, pip wheels: rasterio 1.5.1 / GDAL 3.12.4, xarray 2026.7.0, numpy 2.5.3): the six new tests pass and there are no new failures. In the same environment, master has 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reprojecting xarray dataset: TypeError: only length-1 arrays can be converted to Python scalars

1 participant