Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging CRSs #4

Open
benbovy opened this issue Dec 5, 2024 · 0 comments
Open

Merging CRSs #4

benbovy opened this issue Dec 5, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@benbovy
Copy link
Owner

benbovy commented Dec 5, 2024

A useful feature (regardless of the single vs. multi CRS model discussed in #2) would be to merge all the CRSs found in a dataset or dataarray.

Let's take the example below where two datasets differ only by the name of the CRS-indexed coordinate:

>>> ds = xr.Dataset(coords={"lat": [1, 2, 3], "lon": [1, 2, 3]})
>>> ds_wgs84 = ds.proj.assign_crs(crs_wgs84="epsg:4326")
>>> ds_spatial_ref = ds.proj.assign_crs(spatial_ref="epsg:4326")
>>> ds_wgs84
<xarray.Dataset> Size: 56B
Dimensions:    (lat: 3, lon: 3)
Coordinates:
  * lat        (lat) int64 24B 1 2 3
  * lon        (lon) int64 24B 1 2 3
  * crs_wgs84  int64 8B 0
Data variables:
    *empty*
Indexes:
    crs_wgs84  CRSIndex (crs=EPSG:4326)
>>> ds_spatial_ref
<xarray.Dataset> Size: 56B
Dimensions:      (lat: 3, lon: 3)
Coordinates:
  * lat          (lat) int64 24B 1 2 3
  * lon          (lon) int64 24B 1 2 3
  * spatial_ref  int64 8B 0
Data variables:
    *empty*
Indexes:
    spatial_ref  CRSIndex (crs=EPSG:4326)

Merging these two datasets with xarray.merge() will keep both CRS coordinates:

>>> merged = xr.merge([ds_wgs84, ds_spatial_ref])
>>> merged
<xarray.Dataset> Size: 64B
Dimensions:      (lat: 3, lon: 3)
Coordinates:
  * lat          (lat) int64 24B 1 2 3
  * lon          (lon) int64 24B 1 2 3
  * crs_wgs84    int64 8B 0
  * spatial_ref  int64 8B 0
Data variables:
    *empty*
Indexes:
    crs_wgs84    CRSIndex (crs=EPSG:4326)
    spatial_ref  CRSIndex (crs=EPSG:4326)

Getting the unique CRS from the merged dataset won't work despite all CRS are the same:

>>> merged.proj.crs
ValueError: found more than one coordinate with a CRSIndex in Dataset or DataArray

For convenience we could provide a proj.merge_crs() method such that:

>>> merged = merged.proj.merge_crs("spatial_ref")
>>> merged
<xarray.Dataset> Size: 56B
Dimensions:      (lat: 3, lon: 3)
Coordinates:
  * lat          (lat) int64 24B 1 2 3
  * lon          (lon) int64 24B 1 2 3
  * spatial_ref  int64 8B 0
Data variables:
    *empty*
Indexes:
    spatial_ref  CRSIndex (crs=EPSG:4326)
>>> merged.proj.crs
<Geographic 2D CRS: EPSG:4326>
...

We could first restrict .proj.merge_crs("coord_name") to checking that all CRS indexes found in the dataset are equal and if yes merge all CRS coordinates into one (with the name given as argument). Later on we could expand the functionality with optional arguments (e.g., trigger re-projection).

@benbovy benbovy added the enhancement New feature or request label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant