Skip to content

Copied DataArray loses its set CRS #532

Closed

Description

@apiwat-chantawibul

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

Activity

  1. apiwat-chantawibul commented on Jun 2, 2022

    @apiwat-chantawibul
    ContributorAuthor

    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.

  2. apiwat-chantawibul commented on Jun 2, 2022

    @apiwat-chantawibul
    ContributorAuthor

    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.

  3. snowman2 commented on Jun 2, 2022

    @snowman2
    Member

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

  4. added
    questionFurther information is requested
    and removed
    bugSomething isn't working
    on Jun 2, 2022
  5. snowman2 commented on Jun 2, 2022

    @snowman2
    Member

    Related #356

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions