Skip to content

Commit

Permalink
Issue #666 load_stac: fix band filtering in cube metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Nov 25, 2024
1 parent f932f5b commit 4de8c61
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def load_collection(
metadata = None
if metadata:
bands = [b if isinstance(b, str) else metadata.band_dimension.band_name(b) for b in bands]
# TODO: also apply spatial/temporal filters to metadata?
metadata = metadata.filter_bands(bands)
arguments['bands'] = bands

Expand Down Expand Up @@ -385,6 +386,9 @@ def load_stac(
graph = PGNode("load_stac", arguments=arguments)
try:
metadata = metadata_from_stac(url)
if bands:
# TODO: also apply spatial/temporal filters to metadata?
metadata = metadata.filter_bands(band_names=bands)
except Exception:
log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
metadata = None
Expand Down
13 changes: 13 additions & 0 deletions tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,19 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
cube = con120.load_stac(str(stac_path))
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)

def test_load_stac_band_filtering(self, con120, tmp_path):
stac_path = tmp_path / "stac.json"
stac_data = StacDummyBuilder.collection(
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
)
stac_path.write_text(json.dumps(stac_data))

cube = con120.load_stac(str(stac_path))
assert cube.metadata.band_names == ["B01", "B02", "B03"]

cube = con120.load_stac(str(stac_path), bands=["B03", "B02"])
assert cube.metadata.band_names == ["B03", "B02"]


@pytest.mark.parametrize(
"data",
Expand Down

0 comments on commit 4de8c61

Please sign in to comment.