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

Compatibility fix for passing odc-geo GeoBoxes to dc.load(like=...) #1551

Merged
merged 12 commits into from
Mar 26, 2024
7 changes: 7 additions & 0 deletions datacube/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,16 @@ def output_geobox(like=None, output_crs=None, resolution=None, align=None,
assert output_crs is None, "'like' and 'output_crs' are not supported together"
assert resolution is None, "'like' and 'resolution' are not supported together"
assert align is None, "'like' and 'align' are not supported together"

# If user passes a GeoBox, return as-is
if isinstance(like, GeoBox):
return like

# For `odc-geo` GeoBox compatibility: use "compat" attribute if
# it exists to convert to a Datacube-style GeoBox
if isinstance(compat := getattr(like, "compat", None), GeoBox):
return compat

return like.geobox

if load_hints:
Expand Down
1 change: 1 addition & 0 deletions docs/about/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ v1.8.next
- Update github-Dockerhub credential-passing mechanism. (:pull:`1528`)
- Tweak ``list_products`` logic for getting crs and resolution values (:pull:`1535`)
- Add new ODC Cheatsheet reference doc to Data Access & Analysis documentation page (:pull:`1543`)
- Compatibility fix to allow users to supply ``odc.geo``-style GeoBoxes to ``dc.load(like=...)`` (:pull:`1551`)
- Fix broken codecov github action. (:pull:`1554`)
- Throw error if ``time`` dimension is provided as an int or float to Query construction
instead of assuming it to be seconds since epoch (:pull:`1561`)
Expand Down
45 changes: 45 additions & 0 deletions integration_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import numpy
import pytest
import rasterio
from affine import Affine

from datacube.api.query import query_group_by
from datacube.api.core import Datacube
from datacube.utils.geometry import GeoBox

from integration_tests.utils import prepare_test_ingestion_configuration

Expand Down Expand Up @@ -116,6 +118,7 @@ def test_end_to_end(clirunner, index, testdata_dir, ingest_configs, datacube_env
check_open_with_dc(index)
check_open_with_grid_workflow(index)
check_load_via_dss(index)
check_odcgeo_geobox_load(index)


def check_open_with_dc(index):
Expand Down Expand Up @@ -317,3 +320,45 @@ def check_legacy_open(index):
xx_lazy = dc.load_data(sources, gbox, mm, dask_chunks={'time': 1})
assert xx_lazy['blue'].data.dask
assert xx_lazy.blue[0, :, :].equals(xx.blue[0, :, :])


def check_odcgeo_geobox_load(index):
"""
Test that users can use `dc.load(like=...)` by passing an
`odc-geo`-style GeoBox.
"""
dc = Datacube(index=index)

# Create mock odc-geo GeoBox
class ODC_geo_geobox:
compat = GeoBox(
500, 500, Affine(0.002, 0.0, 149.0, 0.0, -0.002, -35.0), "EPSG:4326"
)
coords = compat.coords

odc_geo_geobox = ODC_geo_geobox()

# Load data using .compat method
ds_compat = dc.load(
product="ls5_nbar_albers",
measurements=["blue"],
like=odc_geo_geobox.compat,
)
assert "blue" in ds_compat.data_vars

# Load data using odc-geo GeoBox directly
ds_odcgeo = dc.load(
product="ls5_nbar_albers",
measurements=["blue"],
like=odc_geo_geobox,
)
assert "blue" in ds_odcgeo.data_vars

# Like behaves differently when time is specified; make sure this works too
ds_odcgeo_time = dc.load(
product="ls5_nbar_albers",
measurements=["blue"],
like=odc_geo_geobox,
time="1992-03-23T23:14:25.500000",
)
assert "blue" in ds_odcgeo_time.data_vars
1 change: 1 addition & 0 deletions wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ geo
geobase
GeoBox
geobox
GeoBoxes
GeoboxTiles
GEOGCS
GeoJSON
Expand Down
Loading