Skip to content

Commit bea069b

Browse files
committed
Issue #666 load_stac: fix band filtering in cube metadata
1 parent d086d30 commit bea069b

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
@@ -2655,6 +2655,19 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
26552655
cube = con120.load_stac(str(stac_path))
26562656
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)
26572657

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

26592672
@pytest.mark.parametrize(
26602673
"data",

0 commit comments

Comments
 (0)