Skip to content

Commit 6d5c5ad

Browse files
committed
Issue #699 fixup item_assets handling for pystac<1.12
Support for STAC 1.1 item_assets through `Collection.item_assets` is only available since 1.12. Below that DIY dict handling is necessary.
1 parent 19e7978 commit 6d5c5ad

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

openeo/metadata.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ def _band_from_eo_bands_metadata(self, band: Union[dict, pystac.extensions.eo.Ba
768768

769769
def _band_from_common_bands_metadata(self, data: dict) -> Band:
770770
"""Construct band from metadata dict in STAC 1.1 + eo v2 style metadata"""
771+
# TODO: also support pystac wrapper when available (pystac v2?)
771772
return Band(
772773
name=data["name"],
773774
common_name=data.get("eo:common_name"),
@@ -819,6 +820,13 @@ def bands_from_stac_collection(
819820
return self._bands_from_item_assets(collection.item_assets)
820821
elif _PYSTAC_1_9_EXTENSION_INTERFACE and collection.ext.has("item_assets") and collection.ext.item_assets:
821822
return self._bands_from_item_assets(collection.ext.item_assets)
823+
elif "item_assets" in collection.extra_fields:
824+
# Workaround for lack of support for STAC 1.1 core item_assets with pystac < 1.12
825+
item_assets = {
826+
k: pystac.extensions.item_assets.AssetDefinition(properties=v, owner=collection)
827+
for k, v in collection.extra_fields["item_assets"].items()
828+
}
829+
return self._bands_from_item_assets(item_assets)
822830
# If no band metadata so far: traverse items in collection
823831
elif consult_items:
824832
bands = _BandList.merge(
@@ -894,6 +902,8 @@ def _bands_from_item_asset_definition(
894902
if _PYSTAC_1_9_EXTENSION_INTERFACE and asset.owner and not asset.ext.has("eo"):
895903
self._warn_undeclared_metadata(field="eo:bands", ext="eo")
896904
return _BandList(self._band_from_eo_bands_metadata(b) for b in asset.properties["eo:bands"])
905+
else:
906+
self._warn(f"_bands_from_item_asset_definition: unsupported {type(asset)=}")
897907
return _BandList([])
898908

899909
def _bands_from_item_assets(

tests/test_metadata.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,9 +1736,12 @@ def test_bands_from_stac_asset(self, data, expected):
17361736
["Deriving band listing from unordered `item_assets`"],
17371737
),
17381738
(
1739-
# STAC 1.0, with "eo" extension is used for band metadata, but not declared
1739+
# STAC 1.0, with "eo" extension used for band metadata, but not declared
17401740
StacDummyBuilder.collection(
17411741
stac_version="1.0.0",
1742+
stac_extensions=[
1743+
"https://stac-extensions.github.io/item-assets/v1.0.0/schema.json",
1744+
],
17421745
item_assets={
17431746
"asset1": {"eo:bands": [{"name": "B03"}, {"name": "B02"}]},
17441747
"asset2": {"eo:bands": [{"name": "B04"}]},
@@ -1763,7 +1766,7 @@ def test_bands_from_stac_asset(self, data, expected):
17631766
["Deriving band listing from unordered `item_assets`"],
17641767
),
17651768
(
1766-
# STAC 1.1 Collection with "eo" extension
1769+
# STAC 1.1 Collection with "eo" extension based band metadata
17671770
StacDummyBuilder.collection(
17681771
stac_version="1.1.0",
17691772
stac_extensions=[
@@ -1784,7 +1787,9 @@ def test_bands_from_stac_collection_with_item_assets(
17841787
):
17851788
collection = pystac.Collection.from_dict(stac_data)
17861789
assert _StacMetadataParser().bands_from_stac_collection(collection).band_names() == expected_bands
1787-
assert caplog.messages == expected_warnings
1790+
1791+
if _PYSTAC_1_9_EXTENSION_INTERFACE:
1792+
assert caplog.messages == expected_warnings
17881793

17891794
@pytest.mark.parametrize(
17901795
["path", "expected"],

0 commit comments

Comments
 (0)