corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
517 stars 82 forks source link

write_crs() shifts extent by half a cell #624

Closed dkczk closed 1 year ago

dkczk commented 1 year ago
import xarray as xr
import rioxarray as rio

res = 0.00008
dec = abs(decimal.Decimal(str(res)).as_tuple().exponent)

lon = list(np.arange(12, 15, res))
lon = [round(x, dec) for x in lon]
lat = list(np.arange(51, 54, res))
lat = [round(x, dec) for x in lat]

dummy = xr.DataArray(
        data=1,
        dims=["x", "y"],
        coords={"x": lon, "y": lat},
        name="dummy"
        )

dummy.rio.write_crs("epsg:4326", inplace=True)

Problem description

I want to create a dummy raster with an extent of 3x3 degrees and a specific resolution which I can later use for the reproject_match() function. The chosen resolution of 0.00008 degrees results in a raster with 37500 rows and cols. After writing the CRS using rio.write_crs() the extent is shifted by half a cell and obviously I don't want that. dummy.rio.bounds() gives (11.99996, 50.99996, 14.99996, 53.99996). I've already checked, if the problem is the DataArray with dummy_x = dummy.coords["x"].values. But there the coordinates are as expected and wanted.

Expected Output

dummy.rio.bounds() should return (12.0, 51.0, 15.0, 54.0).

Environment Information

python (3.10.8) rioxarray (0.13.3) xarray (2022.12.0)

Installation method

conda

Conda environment information (if you installed with conda):

Conda Version: 22.11.1 libgdal 3.6.1 hf2b5f72_1 conda-forge rasterio 1.3.4 py310hfc14bbd_0 conda-forge rioxarray 0.13.3 pyhd8ed1ab_0 conda-forge xarray 2022.12.0 pyhd8ed1ab_0 conda-forge

snowman2 commented 1 year ago

The coordinates of the grid cell represent the centroid of the grid cell. To get the boundary of the raster, you have to take into account the resolution of the grid cell and add/subtract from the bounding centroid coordinates.

dkczk commented 1 year ago

Ahhh obvious. Thank you!