Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `load_stac` will now take over the requested band names if there is a mismatch/problem with the extracted collection metadata ([#752](https://github.com/Open-EO/openeo-python-client/issues/752))

### Removed

### Fixed
Expand Down
10 changes: 10 additions & 0 deletions openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ def rename_labels(self, target, source) -> Dimension:
def rename(self, name) -> Dimension:
return BandDimension(name=name, bands=self.bands)

def contains_band(self, band: Union[int, str]) -> bool:
"""
Check if the given band name or index is present in the dimension.
"""
try:
self.band_index(band)
return True
except ValueError:
return False


class GeometryDimension(Dimension):
# TODO: how to model/store labels of geometry dimension?
Expand Down
20 changes: 17 additions & 3 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,24 @@ def load_stac(
graph = PGNode("load_stac", arguments=arguments)
try:
metadata = metadata_from_stac(url)
# TODO: also apply spatial/temporal filters to metadata?

if isinstance(bands, list):
# TODO: also apply spatial/temporal filters to metadata?
metadata = metadata.filter_bands(band_names=bands)
except Exception:
if not metadata.has_band_dimension():
metadata = metadata.add_dimension(
name="bands",
type="bands",
label=None,
)
missing_bands = [b for b in bands if not metadata.band_dimension.contains_band(b)]
if len(missing_bands) == 0:
metadata = metadata.filter_bands(band_names=bands)
else:
logging.warning(
f"Bands {missing_bands} are not available in the collection metadata. Using requested bands as is."
)
metadata = metadata.rename_labels(dimension="bands", target=bands)
except Exception as e:
log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
metadata = None
return cls(graph=graph, connection=connection, metadata=metadata)
Expand Down
18 changes: 17 additions & 1 deletion tests/rest/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,7 @@ 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):
def test_load_stac_band_filtering(self, con120, tmp_path, caplog):
stac_path = tmp_path / "stac.json"
stac_data = StacDummyBuilder.collection(
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
Expand All @@ -2971,6 +2971,22 @@ def test_load_stac_band_filtering(self, con120, tmp_path):
cube = con120.load_stac(str(stac_path), bands=["B03", "B02"])
assert cube.metadata.band_names == ["B03", "B02"]

caplog.set_level(logging.WARNING)
# Test with non-existing bands in the collection metadata
cube = con120.load_stac(str(stac_path), bands=["B04"])
assert cube.metadata.band_names == ["B04"]
expected_warning = "Bands ['B04'] are not available in the collection metadata. Using requested bands as is."
assert expected_warning in caplog.text
caplog.clear()

cube = con120.load_stac(str(stac_path), bands=["B03", "B04", "B05"])
assert cube.metadata.band_names == ["B03", "B04", "B05"]
expected_warning = (
"Bands ['B04', 'B05'] are not available in the collection metadata. Using requested bands as is."
)
assert expected_warning in caplog.text
caplog.clear()

@pytest.mark.parametrize(
"bands",
[
Expand Down