corteva / rioxarray

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

Copied DataArray loses its set CRS #532

Closed apiwat-chantawibul closed 2 years ago

apiwat-chantawibul commented 2 years ago

Code Sample

import xarray as xr
import rioxarray as rxr

da = xr.DataArray(
    [[1,2],[3,4]],
    dims = ('y','x'),
    coords = dict(
        y = [0,1],
        x = [0,1],
    ),
)
da.rio.set_crs('EPSG:4326')
print('CRS before copy:', da.rio.crs)
da_ = da.copy()
print('CRS after copy:', da_.rio.crs)

Problem description

Currently it outputs:

CRS before copy: EPSG:4326
CRS after copy: None

Meaning CRS information is lost by .copy()

Expected Output

CRS before copy: EPSG:4326
CRS after copy: EPSG:4326

Environment Information

Tested on

rioxarray 0.10.3
python 3.10.4
apiwat-chantawibul commented 2 years ago

Viewing in another way, maybe the problem has to do with how .set_crs() sets an internal variable instead of something like .attrs which would have been copied automatically.

apiwat-chantawibul commented 2 years ago

Okay, I just found rio.write_crs() that explains a lot. So rio.set_crs() has to be followed by rio.write_crs() to make the CRS change portable. And then there is also rio.write_coordinate_system() for some reason. All this seems like a great trap for new users to me. Right now I'm just keeping this issue open in case anyone want to discuss why it has to be this way and how to possibly improve it.

snowman2 commented 2 years ago

I recommend reading the Getting Started: Introductory Information references for rioxarray. The Coordinare Reference System Management section is relevant for this issue.

snowman2 commented 2 years ago

Related #356