0

I'm trying to create a dummy raster which I can later use as target for rioxarrays reproject_match() function. My area of interest (AOI) is exactly 3x3 degrees and my desired resolution is 0.00008. So the resolution fits exactly into the AOI with 37500 rows and columns. I did:

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)

When I check the boundaries with:

dummy.rio.bounds()

I get (11.99996, 50.99996, 14.99996, 53.99996) when the extent should be (12.0, 51.0, 15.0, 54.0). Why is that and how to solve this issue?

Corbjn
  • 276
  • 1
  • 10
  • Please update your example to include the `dst_res` variable. Also, can you observe the stated problem in the `dummy` xarray object? Or is `rioxarray` required to reproduce? – jhamman Jan 04 '23 at 23:41
  • @jhamman Updated `dst_res` to `res`. `rioxarray` is needed to write the CRS definition to the DataArray. I noticed that this shift is apparently caused by the `rio.write_crs()` function. I checked the coordinates of the DataArray with: `dummy_x = dummy.coords["x"].values`. But after I write the CRS, the coordinates are shifted. – Corbjn Jan 05 '23 at 10:04

1 Answers1

0

When setting the coordinates for the cell values you have to keep in mind, that those coordinates are the centroids of each cell. So the middle part should've been:

# ...
lon = list(np.arange((12 + res), (15 + res), res))
lat = list(np.arange((51 + res), (54 + res), res))
# ...
Corbjn
  • 276
  • 1
  • 10
  • 1
    That is correct. If you look at the relevant code in rioxarray, you'll see that the bounds are adjusted by `resolution / 2`: https://github.com/corteva/rioxarray/blob/b323754273affce6806abdc096d964ae61a9af64/rioxarray/rioxarray.py#L1034-L1038 – jhamman Jan 05 '23 at 17:26