From a2c4972aba1cbe91180035d8a6038fc168a0509f Mon Sep 17 00:00:00 2001 From: Kirill Kouzoubov Date: Thu, 27 Jun 2024 18:47:05 +1000 Subject: [PATCH] Expose odc.{crs_coord,grid_mapping} - access crs coordinate variable/name --- odc/geo/_xr_interop.py | 12 ++++++++++++ tests/test_xr_interop.py | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/odc/geo/_xr_interop.py b/odc/geo/_xr_interop.py index cd2518f..6404d7b 100644 --- a/odc/geo/_xr_interop.py +++ b/odc/geo/_xr_interop.py @@ -953,6 +953,18 @@ def map_bounds(self) -> Tuple[Tuple[float, float], Tuple[float, float]]: return gbox.map_bounds() + @property + def crs_coord(self) -> xarray.DataArray | None: + """Return CRS coordinate DataArray.""" + return self._state.crs_coord + + @property + def grid_mapping(self) -> str | None: + """Return name of the grid mapping coordinate.""" + if c := self.crs_coord: + return str(c.name) + return None + mask = _wrap_op(mask) crop = _wrap_op(crop) diff --git a/tests/test_xr_interop.py b/tests/test_xr_interop.py index bec8693..50d4914 100644 --- a/tests/test_xr_interop.py +++ b/tests/test_xr_interop.py @@ -228,6 +228,9 @@ def test_odc_extension(xx_epsg4326: xr.DataArray, geobox_epsg4326: GeoBox): assert xx.odc.map_bounds() == gbox.map_bounds() assert xx.odc.output_geobox("utm").crs.epsg is not None assert xx.odc.aspect == gbox.aspect + assert xx.odc.grid_mapping == xx.encoding["grid_mapping"] + assert xx.odc.crs_coord == xx.spatial_ref + assert xx.odc.crs_coord.attrs == xx.spatial_ref.attrs # this drops encoding/attributes, but crs/geobox should remain the same _xx = xx * 10.0 @@ -299,6 +302,8 @@ def test_odc_extension_ds(xx_epsg4326: xr.DataArray, geobox_epsg4326: GeoBox): def test_assign_crs(xx_epsg4326: xr.DataArray): xx = purge_crs_info(xx_epsg4326) assert xx.odc.crs is None + assert xx.odc.grid_mapping is None + assert xx.odc.crs_coord is None yy = xx.odc.assign_crs("epsg:4326") assert xx.odc.uncached.crs is None assert yy.odc.crs == "epsg:4326"