rioxarray icon indicating copy to clipboard operation
rioxarray copied to clipboard

Xarray concat does not take into account different CRS data

Open gcaria opened this issue 3 years ago • 3 comments

import rasterio as rxr

da1 = xr.DataArray([[0,1], [1,0]], coords=dict(y=[1,0], x=[0,1]))
da1['time'] = 0

da2 = da1.copy()
da2['time'] = 1

da1 = da1.rio.write_crs(32610)
da2 = da2.rio.write_crs(32611)

xr.concat([da1, da2], dim='time').rio.crs # returns 32610

This snippet shows that when concatenating two DataArrays with different CRS, the result gets the CRS of the first DataArray. It seems this is due to the fact that the true CRS information is given as attributes of thespatial_ref coordinate, whose numerical value is the same for both CRSs and does not actually mean anything.

Is this a feature or a bug?

gcaria avatar Aug 30 '22 08:08 gcaria

xarray is unaware of the spatial information during it's operations. You have to manage it yourself in the current state.

This is something that requires more changes to xarray to have either an error or automagically work.

For more information:

  • https://github.com/pydata/xarray/issues/2288#issuecomment-1205793138
  • https://github.com/pydata/xarray/issues/3620

snowman2 avatar Aug 30 '22 11:08 snowman2

For now, I recommend using rio.reproject_match

snowman2 avatar Aug 30 '22 11:08 snowman2

Related #588

snowman2 avatar Nov 07 '22 21:11 snowman2