Skip to content

Commit 4de8c61

Browse files
committed
Issue #666 load_stac: fix band filtering in cube metadata
1 parent f932f5b commit 4de8c61

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

openeo/rest/datacube.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def load_collection(
203203
metadata = None
204204
if metadata:
205205
bands = [b if isinstance(b, str) else metadata.band_dimension.band_name(b) for b in bands]
206+
# TODO: also apply spatial/temporal filters to metadata?
206207
metadata = metadata.filter_bands(bands)
207208
arguments['bands'] = bands
208209

@@ -385,6 +386,9 @@ def load_stac(
385386
graph = PGNode("load_stac", arguments=arguments)
386387
try:
387388
metadata = metadata_from_stac(url)
389+
if bands:
390+
# TODO: also apply spatial/temporal filters to metadata?
391+
metadata = metadata.filter_bands(band_names=bands)
388392
except Exception:
389393
log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
390394
metadata = None

tests/rest/test_connection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,19 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
26512651
cube = con120.load_stac(str(stac_path))
26522652
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)
26532653

2654+
def test_load_stac_band_filtering(self, con120, tmp_path):
2655+
stac_path = tmp_path / "stac.json"
2656+
stac_data = StacDummyBuilder.collection(
2657+
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
2658+
)
2659+
stac_path.write_text(json.dumps(stac_data))
2660+
2661+
cube = con120.load_stac(str(stac_path))
2662+
assert cube.metadata.band_names == ["B01", "B02", "B03"]
2663+
2664+
cube = con120.load_stac(str(stac_path), bands=["B03", "B02"])
2665+
assert cube.metadata.band_names == ["B03", "B02"]
2666+
26542667

26552668
@pytest.mark.parametrize(
26562669
"data",

0 commit comments

Comments
 (0)