Skip to content

rio.set_spatial_dims doesn't work when writing to tif #372

@raybellwaves

Description

@raybellwaves

Code Sample, a copy-pastable example if possible

import xarray as xr
import rioxarray

ds = xr.tutorial.open_dataset("air_temperature").isel(time=0)
ds = ds.reindex(lat=ds["lat"][::-1])
ds = ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180))
ds = ds.rio.set_spatial_dims('lon', 'lat')
ds = ds.rio.write_crs("epsg:4326")
ds.rio.to_raster("file.tif")

Problem description

Gives DimensionError: x dimension not found. 'rio.set_spatial_dims()' or using 'rename()' to change the dimension name to 'x' can address this.

AFAICT I am using set_spatial_dims correctly in the code above.

Full Traceback:

---------------------------------------------------------------------------
DimensionError                            Traceback (most recent call last)
<ipython-input-66-54315585cbb3> in <module>
----> 1 ds.rio.to_raster("file.tif")

~/opt/miniconda3/envs/main/lib/python3.9/site-packages/rioxarray/raster_dataset.py in to_raster(self, raster_path, driver, dtype, tags, windowed, recalc_transform, lock, compute, **profile_kwargs)
    408             data_array.rio.write_crs(self.crs, inplace=True)
    409         # write it to a raster
--> 410         return data_array.rio.to_raster(
    411             raster_path=raster_path,
    412             driver=driver,

~/opt/miniconda3/envs/main/lib/python3.9/site-packages/rioxarray/raster_array.py in to_raster(self, raster_path, driver, dtype, tags, windowed, recalc_transform, lock, compute, **profile_kwargs)
    936             driver=driver,
    937             height=int(self.height),
--> 938             width=int(self.width),
    939             count=int(self.count),
    940             dtype=dtype,

~/opt/miniconda3/envs/main/lib/python3.9/site-packages/rioxarray/rioxarray.py in width(self)
    743         if self._width is not None:
    744             return self._width
--> 745         self._width = self._obj[self.x_dim].size
    746         return self._width
    747

~/opt/miniconda3/envs/main/lib/python3.9/site-packages/rioxarray/rioxarray.py in x_dim(self)
    721         if self._x_dim is not None:
    722             return self._x_dim
--> 723         raise DimensionError(
    724             "x dimension not found. 'rio.set_spatial_dims()' or "
    725             "using 'rename()' to change the dimension name to 'x' can address this."

DimensionError: x dimension not found. 'rio.set_spatial_dims()' or using 'rename()' to change the dimension name to 'x' can address this.

rio.set_spatial_dims() mentioned here: https://github.com/corteva/rioxarray/blob/master/rioxarray/rioxarray.py#L723

Expected Output

Write tif file.

Behaves same as

ds = xr.tutorial.open_dataset("air_temperature").isel(time=0)
ds = ds.rename({"lat": "y", "lon": "x"})
ds = ds.reindex(y=ds["y"][::-1])
ds = ds.assign_coords(x=(((ds.x + 180) % 360) - 180))
ds = ds.rio.write_crs("epsg:4326")
ds.rio.to_raster("file.tif")

Environment Information

rioxarray (0.4.3) deps:
  rasterio: 1.2.6
    xarray: 0.18.2
      GDAL: 3.2.1

Other python deps:
     scipy: 1.6.3
    pyproj: 3.1.0

System:
    python: 3.9.5 | packaged by conda-forge | (default, Jun 19 2021, 00:27:35)  [Clang 11.1.0 ]
executable: /Users/ray/opt/miniconda3/envs/main/bin/python
   machine: macOS-11.4-x86_64-i386-64bit

Installation method

NA

Conda environment information (if you installed with conda):

NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions