nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: opendatacube/odc-geo
Pull request #167 (2024-06-28 06:39:46)

Expose odc.{crs_coord,grid_mapping}
- access crs coordinate variable/name


Patch:
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 @@ class ODCExtension:
 
         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)
 


Test patch:
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"