Skip to content

Commit 612821e

Browse files
committed
Issue #699/#780 more warnings about undeclared eo extension
1 parent 6d5c5ad commit 612821e

File tree

3 files changed

+85
-18
lines changed

3 files changed

+85
-18
lines changed

openeo/metadata.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ def get_temporal_dimension(self, stac_obj: pystac.STACObject) -> Union[TemporalD
750750
return TemporalDimension(name=name, extent=extent)
751751

752752
def _band_from_eo_bands_metadata(self, band: Union[dict, pystac.extensions.eo.Band]) -> Band:
753-
"""Construct band from metadata dict in eo v1.1 style"""
753+
"""Construct band from metadata in eo v1.1 style"""
754754
if isinstance(band, pystac.extensions.eo.Band):
755755
return Band(
756756
name=band.name,
@@ -776,6 +776,9 @@ def _band_from_common_bands_metadata(self, data: dict) -> Band:
776776
)
777777

778778
def bands_from_stac_object(self, obj: Union[pystac.STACObject, pystac.Asset]) -> _BandList:
779+
"""
780+
Extract band metadata from a STAC object (Collection, Catalog, Item or Asset).
781+
"""
779782
# Note: first check for Collection, as it is a subclass of Catalog
780783
if isinstance(obj, pystac.Collection):
781784
return self.bands_from_stac_collection(collection=obj)
@@ -791,8 +794,9 @@ def bands_from_stac_object(self, obj: Union[pystac.STACObject, pystac.Asset]) ->
791794
def bands_from_stac_catalog(self, catalog: pystac.Catalog) -> _BandList:
792795
# TODO: "eo:bands" vs "bands" priority based on STAC and EO extension version information
793796
summaries = catalog.extra_fields.get("summaries", {})
794-
self._warn(f"bands_from_stac_catalog with {summaries.keys()=} (which is non-standard)")
795797
if "eo:bands" in summaries:
798+
if _PYSTAC_1_9_EXTENSION_INTERFACE and not catalog.ext.has("eo"):
799+
self._warn_undeclared_metadata(field="eo:bands", ext="eo")
796800
return _BandList(self._band_from_eo_bands_metadata(b) for b in summaries["eo:bands"])
797801
elif "bands" in summaries:
798802
return _BandList(self._band_from_common_bands_metadata(b) for b in summaries["bands"])
@@ -808,6 +812,8 @@ def bands_from_stac_collection(
808812
self._log(f"bands_from_stac_collection with {collection.summaries.lists.keys()=}")
809813
# Look for band metadata in collection summaries
810814
if "eo:bands" in collection.summaries.lists:
815+
if _PYSTAC_1_9_EXTENSION_INTERFACE and not collection.ext.has("eo"):
816+
self._warn_undeclared_metadata(field="eo:bands", ext="eo")
811817
return _BandList(self._band_from_eo_bands_metadata(b) for b in collection.summaries.lists["eo:bands"])
812818
elif "bands" in collection.summaries.lists:
813819
return _BandList(self._band_from_common_bands_metadata(b) for b in collection.summaries.lists["bands"])

tests/rest/test_connection.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,13 +3047,22 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
30473047
cube = con120.load_stac(str(stac_path))
30483048
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)
30493049

3050-
def test_load_stac_default_band_handling(self, dummy_backend, build_stac_ref):
3051-
stac_ref = build_stac_ref(
3050+
@pytest.mark.parametrize(
3051+
"stac_collection",
3052+
[
30523053
StacDummyBuilder.collection(
3053-
# TODO #586 also cover STAC 1.1 style "bands"
3054-
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
3055-
)
3056-
)
3054+
stac_version="1.0.0",
3055+
stac_extensions=["https://stac-extensions.github.io/eo/v1.1.0/schema.json"],
3056+
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]},
3057+
),
3058+
StacDummyBuilder.collection(
3059+
stac_version="1.1.0",
3060+
summaries={"bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]},
3061+
),
3062+
],
3063+
)
3064+
def test_load_stac_default_band_handling(self, dummy_backend, build_stac_ref, stac_collection):
3065+
stac_ref = build_stac_ref(stac_collection)
30573066

30583067
cube = dummy_backend.connection.load_stac(stac_ref)
30593068
assert cube.metadata.band_names == ["B01", "B02", "B03"]
@@ -3078,13 +3087,24 @@ def test_load_stac_default_band_handling(self, dummy_backend, build_stac_ref):
30783087
(["B01", "B02", "B03"], None),
30793088
],
30803089
)
3081-
def test_load_stac_band_filtering(self, dummy_backend, build_stac_ref, caplog, bands, expected_warning):
3082-
stac_ref = build_stac_ref(
3090+
@pytest.mark.parametrize(
3091+
"stac_collection",
3092+
[
30833093
StacDummyBuilder.collection(
3084-
# TODO #586 also cover STAC 1.1 style "bands"
3085-
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
3086-
)
3087-
)
3094+
stac_version="1.0.0",
3095+
stac_extensions=["https://stac-extensions.github.io/eo/v1.1.0/schema.json"],
3096+
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]},
3097+
),
3098+
StacDummyBuilder.collection(
3099+
stac_version="1.1.0",
3100+
summaries={"bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]},
3101+
),
3102+
],
3103+
)
3104+
def test_load_stac_band_filtering(
3105+
self, dummy_backend, build_stac_ref, caplog, bands, expected_warning, stac_collection
3106+
):
3107+
stac_ref = build_stac_ref(stac_collection)
30883108

30893109
caplog.set_level(logging.WARNING)
30903110
# Test with non-existing bands in the collection metadata

tests/test_metadata.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ def test_band_from_common_bands_metadata(self):
11931193
) == Band(name="B04", common_name="red", wavelength_um=0.665)
11941194

11951195
@pytest.mark.parametrize(
1196-
["data", "expected"],
1196+
["data", "expected", "expected_warnings"],
11971197
[
11981198
(
11991199
{
@@ -1214,6 +1214,27 @@ def test_band_from_common_bands_metadata(self):
12141214
Band("B04", common_name="red", wavelength_um=0.665),
12151215
Band("B03", common_name="green", wavelength_um=0.560),
12161216
],
1217+
[],
1218+
),
1219+
(
1220+
{
1221+
"type": "Catalog",
1222+
"id": "catalog123",
1223+
"description": "Catalog 123",
1224+
"stac_version": "1.0.0",
1225+
"summaries": {
1226+
"eo:bands": [
1227+
{"name": "B04", "common_name": "red", "center_wavelength": 0.665},
1228+
{"name": "B03", "common_name": "green", "center_wavelength": 0.560},
1229+
],
1230+
},
1231+
"links": [],
1232+
},
1233+
[
1234+
Band("B04", common_name="red", wavelength_um=0.665),
1235+
Band("B03", common_name="green", wavelength_um=0.560),
1236+
],
1237+
["Using 'eo:bands' metadata, but STAC extension eo was not declared."],
12171238
),
12181239
(
12191240
{
@@ -1230,6 +1251,7 @@ def test_band_from_common_bands_metadata(self):
12301251
"links": [],
12311252
},
12321253
[Band("B04"), Band("B03")],
1254+
[],
12331255
),
12341256
(
12351257
{
@@ -1249,15 +1271,19 @@ def test_band_from_common_bands_metadata(self):
12491271
Band("B04", common_name="red", wavelength_um=0.665),
12501272
Band("B03", common_name="green", wavelength_um=0.560),
12511273
],
1274+
[],
12521275
),
12531276
],
12541277
)
1255-
def test_bands_from_stac_catalog(self, data, expected):
1278+
def test_bands_from_stac_catalog(self, data, expected, expected_warnings, caplog):
12561279
catalog = pystac.Catalog.from_dict(data)
12571280
assert _StacMetadataParser().bands_from_stac_catalog(catalog=catalog) == expected
12581281

1282+
if _PYSTAC_1_9_EXTENSION_INTERFACE:
1283+
assert caplog.messages == expected_warnings
1284+
12591285
@pytest.mark.parametrize(
1260-
["data", "expected"],
1286+
["data", "expected", "expected_warnings"],
12611287
[
12621288
(
12631289
StacDummyBuilder.collection(
@@ -1266,6 +1292,7 @@ def test_bands_from_stac_catalog(self, data, expected):
12661292
summaries={"eo:bands": [{"name": "B02"}, {"name": "B03"}, {"name": "B04"}]},
12671293
),
12681294
[Band("B02"), Band("B03"), Band("B04")],
1295+
[],
12691296
),
12701297
(
12711298
StacDummyBuilder.collection(
@@ -1282,13 +1309,23 @@ def test_bands_from_stac_catalog(self, data, expected):
12821309
Band("B04", common_name="red", wavelength_um=0.665),
12831310
Band("B03", common_name="green", wavelength_um=0.560),
12841311
],
1312+
[],
1313+
),
1314+
(
1315+
StacDummyBuilder.collection(
1316+
stac_version="1.0.0",
1317+
summaries={"eo:bands": [{"name": "B02"}, {"name": "B03"}, {"name": "B04"}]},
1318+
),
1319+
[Band("B02"), Band("B03"), Band("B04")],
1320+
["Using 'eo:bands' metadata, but STAC extension eo was not declared."],
12851321
),
12861322
(
12871323
StacDummyBuilder.collection(
12881324
stac_version="1.1.0",
12891325
summaries={"bands": [{"name": "B02"}, {"name": "B03"}, {"name": "B04"}]},
12901326
),
12911327
[Band("B02"), Band("B03"), Band("B04")],
1328+
[],
12921329
),
12931330
(
12941331
StacDummyBuilder.collection(
@@ -1305,13 +1342,17 @@ def test_bands_from_stac_catalog(self, data, expected):
13051342
Band("B04", common_name="red", wavelength_um=0.665),
13061343
Band("B03", common_name="green", wavelength_um=0.560),
13071344
],
1345+
[],
13081346
),
13091347
],
13101348
)
1311-
def test_bands_from_stac_collection(self, data, expected):
1349+
def test_bands_from_stac_collection(self, data, expected, caplog, expected_warnings):
13121350
collection = pystac.Collection.from_dict(data)
13131351
assert _StacMetadataParser().bands_from_stac_collection(collection=collection) == expected
13141352

1353+
if _PYSTAC_1_9_EXTENSION_INTERFACE:
1354+
assert caplog.messages == expected_warnings
1355+
13151356
@pytest.mark.parametrize(
13161357
["entities", "kwargs", "expected"],
13171358
[

0 commit comments

Comments
 (0)