Skip to content

Commit

Permalink
Expose odc.{crs_coord,grid_mapping}
Browse files Browse the repository at this point in the history
- access crs coordinate variable/name
  • Loading branch information
Kirill888 committed Jun 27, 2024
1 parent 47b143a commit a2c4972
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions odc/geo/_xr_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions tests/test_xr_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a2c4972

Please sign in to comment.