Skip to content
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

reproject_match raises MissingSpatialDimensionError with spatial dims set #768

Closed
DahnJ opened this issue Apr 16, 2024 · 1 comment · Fixed by #769
Closed

reproject_match raises MissingSpatialDimensionError with spatial dims set #768

DahnJ opened this issue Apr 16, 2024 · 1 comment · Fixed by #769
Labels
bug Something isn't working
Milestone

Comments

@DahnJ
Copy link
Contributor

DahnJ commented Apr 16, 2024

Code Sample, a copy-pastable example if possible

import numpy as np
import xarray as xr
import rioxarray

lat = [0.1, 0.15, 0.2]
lon = [0.1, 0.15, 0.2]
data = np.arange(1, 10).reshape(3, 3)
ds = xr.Dataset(
        data_vars={
            "foo": (["lat", "lon"], data),
            "bar": (["lat", "lon"], data),
        },
        coords={"lat": lat, "lon": lon},
    )

ds = ds.rio.set_spatial_dims(x_dim="lon", y_dim="lat")
ds = ds.rio.write_crs(4326)

ds = ds.rio.reproject_match(ds)

Thanks @bocalml for the MRE.

Problem description

This results in MissingSpatialDimensionError: x dimension (lon) not found.

This is likely related to the renaming mentioned in #751 but I haven't been able to make sense of this fully and the result is surprising to me.

Full traceback
---------------------------------------------------------------------------
MissingSpatialDimensionError              Traceback (most recent call last)
Cell In[31], line 1
----> 1 ds = ds.rio.reproject_match(ds)

File ~/miniconda3/envs/myenv/lib/python3.12/site-packages/rioxarray/raster_dataset.py:191, in RasterDataset.reproject_match(self, match_data_array, resampling, **reproject_kwargs)
    189             raise
    190         resampled_dataset[var] = self._obj[var].copy()
--> 191 return resampled_dataset.rio.set_spatial_dims(
    192     x_dim=self.x_dim, y_dim=self.y_dim, inplace=True
    193 )

File ~/miniconda3/envs/myenv/lib/python3.12/site-packages/rioxarray/rioxarray.py:853, in XRasterBase.set_spatial_dims(self, x_dim, y_dim, inplace)
    851     data_obj.rio._x_dim = x_dim
    852 else:
--> 853     raise MissingSpatialDimensionError(
    854         fx dimension ({x_dim}) not found.{_get_data_var_message(data_obj)}”
    855     )
    856 if y_dim in data_obj.dims:
    857     data_obj.rio._y_dim = y_dim

MissingSpatialDimensionError: x dimension (lon) not found.

Expected Output

The dataset itself with no error.

Environment Information

rioxarray (0.15.3) deps:
  rasterio: 1.3.9
    xarray: 2024.3.0
      GDAL: 3.8.4
      GEOS: 3.12.1
      PROJ: 9.3.1
 PROJ DATA: [/Users/danieljahn/miniconda3/envs/myenv/share/proj](https://file+.vscode-resource.vscode-cdn.net/Users/danieljahn/miniconda3/envs/myenv/share/proj)
 GDAL DATA: [/Users/danieljahn/miniconda3/envs/myenv/share/gdal](https://file+.vscode-resource.vscode-cdn.net/Users/danieljahn/miniconda3/envs/myenv/share/gdal)

Other python deps:
     scipy: 1.12.0
    pyproj: 3.6.1

System:
    python: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ]
executable: [/Users/danieljahn/miniconda3/envs/myenv/bin/python](https://file+.vscode-resource.vscode-cdn.net/Users/danieljahn/miniconda3/envs/myenv/bin/python)
   machine: macOS-14.2.1-arm64-arm-64bit

Installation method

Conda

Conda environment information (if you installed with conda):


Environment (conda list):
$ conda list | grep -E "rasterio|xarray|gdal"
cf-xarray                 0.9.0                    pypi_0    pypi
gdal                      3.8.4           py312h56161e1_3    conda-forge
libgdal                   3.8.4                hd76467a_3    conda-forge
rasterio                  1.3.9           py312hd404879_2    conda-forge
rioxarray                 0.15.3             pyhd8ed1ab_0    conda-forge
xarray                    2024.3.0           pyhd8ed1ab_0    conda-forge

Details about conda and system ( conda info ):
$ conda info

     active environment : myenv
    active env location : /Users/danieljahn/miniconda3/envs/myenv
            shell level : 4
       user config file : /Users/danieljahn/.condarc
 populated config files : /Users/danieljahn/.condarc
          conda version : 23.3.1
    conda-build version : 3.23.3
         python version : 3.11.0.final.0
       virtual packages : __archspec=1=arm64
                          __osx=14.2.1=0
                          __unix=0=0
       base environment : /Users/danieljahn/miniconda3  (writable)
      conda av data dir : /Users/danieljahn/miniconda3/etc/conda
  conda av metadata url : None
           channel URLs : 
                          https://conda.anaconda.org/t/<TOKEN>/conda-forge/osx-arm64
                          https://conda.anaconda.org/t/<TOKEN>/conda-forge/noarch
          package cache : /Users/danieljahn/miniconda3/pkgs
                          /Users/danieljahn/.conda/pkgs
       envs directories : /Users/danieljahn/miniconda3/envs
                          /Users/danieljahn/.conda/envs
               platform : osx-arm64
             user-agent : conda/23.3.1 requests/2.31.0 CPython/3.11.0 Darwin/23.2.0 OSX/14.2.1
                UID:GID : 501:20
             netrc file : /Users/danieljahn/.netrc
           offline mode : False
@DahnJ DahnJ added the bug Something isn't working label Apr 16, 2024
@snowman2
Copy link
Member

Bug confirmed - thank you for your report!

Related #372

Even though this doesn't fix the issue, I recommend changing your code like as shown in the crs management example as it will help your spatial dimension persist better and follow the CF conventions for metadata:

import numpy
import xarray
import rioxarray

lat = [0.1, 0.15, 0.2]
lon = [0.1, 0.15, 0.2]
data = numpy.arange(1, 10).reshape(3, 3)
ds = xarray.Dataset(
        data_vars={
            "foo": (["lat", "lon"], data),
            "bar": (["lat", "lon"], data),
        },
        coords={"lat": lat, "lon": lon},
    )

ds = ds.rio.write_crs(
    4326, inplace=True
).rio.set_spatial_dims(
    x_dim="lon", y_dim="lat", inplace=True
).rio.write_coordinate_system(inplace=True)
ds = ds.rio.reproject_match(ds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants