Skip to content

Create auxiliary (index) coordinates with a different CRS #31

Open
@benbovy

Description

@benbovy

It would be nice to have a simple and common way of assigning auxiliary coordinates, sharing the same dimension(s) but with an alternative CRS, to a dataset / dataarray that already has spatial coordinates with a CRS-aware index. It would be even nicer if the new auxiliary coordinate(s) have an index too. Such feature would fit well in XProj IMO.


Example with a raster data cube (rasterix):

>>> ds_raster

<xarray.Dataset>; Size: 120B
Dimensions:      (y: 4, x: 2)
Coordinates:
    spatial_ref  int64 8B 0
  * x            (x) float64 16B ...
  * y            (y) float64 32B ...
Data variables:
    foo          (y, x) float64 64B 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
Indexes:
  ┌ x        RasterIndexy


>>> ds_raster.proj.create_aux_coords(("lon", "lat"), from=("x", "y"), crs=4326)

<xarray.Dataset>; Size: 168B
Dimensions:      (y: 4, x: 2)
Coordinates:
    spatial_ref  int64 8B 0
  * x            (x) float64 16B ...
  * y            (y) float64 32B ...
  * lon          (y, x) float64 16B ...
  * lat          (y, x) float64 32B ...
Data variables:
    foo          (y, x) float64 64B 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
Indexes:
  ┌ x        RasterIndexylon      RasterIndexlat

Example with a vector data cube (xvec):

>>> ds_vec

<xarray.Dataset (county: 3085)> Size: 35kB
Coordinates:
  * county   (county) object 25kB POLYGON ((...
Data variables:
    foo      (county) float64 10kB ...
Indexes:
    county   GeometryIndex (crs=EPSG:2263)


>>> ds_vec.proj.create_aux_coords("county2", from="county", crs=4326)

<xarray.Dataset (county: 3085)> Size: 60kB
Coordinates:
  * county   (county) object 25kB POLYGON ((...
  * county2  (county) object 25kB POLYGON ((...
Data variables:
    foo      (county) float64 10kB ...
Indexes:
    county   GeometryIndex (crs=EPSG:2263)
    county2  GeometryIndex (crs=EPSG:4326)

For the implementation we could rely on xproj.ProjIndexMixin._proj_to_crs(), which returns a new Index instance from an existing CRS-aware index.

The API in the examples above is just a suggestion. It would be nice to also create a new spatial reference, scalar coordinate (with a CRSIndex) alongside the new auxiliary coordinates, perhaps with something like this?

ds.proj.create_aux_coords(..., spatial_ref="spatial_ref2")

@dcherian @scottyhq @martinfleis - Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions