Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoconvert legacy geoboxes #1574

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion datacube/api/core.py
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 deletions docs/about/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ v1.9.next

- Standardize resampling input supported to `odc.geo.warp.Resampling` (:pull:`1571`)
- Refine default behaviour for config engine to support easier migration from 1.8 (:pull:`1573`)
- Convert legacy GeoBoxes to odc.geo GeoBoxes in the core API (:pull:`1574`)


v1.9.0-rc3 (27th March 2024)
Expand Down Expand Up @@ -47,6 +48,7 @@ v1.9.0-rc1 (27th March 2024)
- Index driver API type hint cleanup. (:pull:`1541`)
- Deprecate multiple locations. (:pull:`1546`)
- Deprecate search_eager and search_summaries and add `archived` arg to all dataset search/count methods. (:pull:`1550`)
- Compatibility fix - dc.load can take odc.geo GeoBox (:pull:`1551`)
- Migrate away from deprecated Python pkg_resources module (:pull:`1558`)
- Add ``custom_offsets`` and ``order_by`` arguments to search_retunrning() - order_by still unimplemented. (:pull:`1557`)
- Fix and enhance typehints, automated static type checking with mypy. (:pull:`1562`)
Expand Down
30 changes: 29 additions & 1 deletion tests/api/test_core.py
Original file line number Diff line number Diff line change
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)
Loading