Skip to content

Commit

Permalink
Convert legacy GeoBoxes to odc.geo GeoBoxes in the core API.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacemanPaul committed Apr 9, 2024
1 parent a8ec892 commit 28b7748
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion datacube/api/core.py
Expand Up @@ -1019,8 +1019,12 @@ def output_geobox(like: GeoBox | xarray.Dataset | xarray.DataArray | None = None
assert resolution is None, "'like' and 'resolution' are not supported together"
assert align is None, "'like' and 'align' are not supported together"
if isinstance(like, GeoBox):
# Is already a GeoBox
return like

if like.__class__.__name__ == "GeoBox" and like.__class__.__module__ == 'datacube.utils.geometry._base':
# Is a legacy GeoBox: convert to odc.geo.geobox.GeoBox (check class without importing deprecated class)
return GeoBox(shape=(like.height, like.width), affine=like.affine, crs=like.crs)
# Is an Xarray object
return like.odc.geobox

if load_hints:
Expand Down
30 changes: 29 additions & 1 deletion tests/api/test_core.py
Expand Up @@ -12,7 +12,7 @@
import pytest

from datacube.api.query import GroupBy
from datacube.api.core import _calculate_chunk_sizes
from datacube.api.core import _calculate_chunk_sizes, output_geobox
from datacube import Datacube
from datacube.testutils.geom import AlbersGS
from datacube.testutils import mk_sample_dataset
Expand Down Expand Up @@ -122,3 +122,31 @@ def test_index_validation():
dc = Datacube(index=index, config=["/a/path", "/a/nother/path"], env="prod", app="this_is_me", raw_config="{}")
estr = str(e.value)
assert "config,raw_config,app,env" in estr


def test_output_geobox():
from odc.geo.geobox import GeoBox as ODCGeoGeoBox, CRS as ODCGeoCRS
from datacube.utils.geometry import GeoBox as LegacyGeoGeoBox, CRS as LegacyCRS
from odc.geo.xr import xr_zeros

odc_gbox = ODCGeoGeoBox.from_bbox(
(-2_000_000, -5_000_000, 2_250_000, -1_000_000),
"epsg:3577",
resolution=1000
)
legacy_gbox = LegacyGeoGeoBox(width=odc_gbox.width, height=odc_gbox.height,
affine=odc_gbox.affine,
crs=LegacyCRS(str(odc_gbox.crs)))

assert legacy_gbox != odc_gbox

xra = xr_zeros(odc_gbox, chunks=(1000, 1000))

assert xra.odc.geobox == odc_gbox

gbox = output_geobox(like=xra)
assert gbox == odc_gbox

gbox = output_geobox(like=legacy_gbox)
assert gbox == odc_gbox
assert isinstance(gbox.crs, ODCGeoCRS)

0 comments on commit 28b7748

Please sign in to comment.