Skip to content

Commit de423f2

Browse files
VictorVerhaertsoxofaan
authored andcommitted
made load_stac nicer to mismatch in band names
1 parent 4a3dff6 commit de423f2

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

openeo/metadata.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ def rename_labels(self, target, source) -> Dimension:
218218
def rename(self, name) -> Dimension:
219219
return BandDimension(name=name, bands=self.bands)
220220

221+
def contains_band(self, band: Union[int, str]) -> bool:
222+
"""
223+
Check if the given band name or index is present in the dimension.
224+
"""
225+
try:
226+
self.band_index(band)
227+
return True
228+
except ValueError:
229+
return False
230+
221231

222232
class GeometryDimension(Dimension):
223233
# TODO: how to model/store labels of geometry dimension?

openeo/rest/datacube.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,23 @@ def load_stac(
441441
graph = PGNode("load_stac", arguments=arguments)
442442
try:
443443
metadata = metadata_from_stac(url)
444+
# TODO: also apply spatial/temporal filters to metadata?
445+
444446
if isinstance(bands, list):
445-
# TODO: also apply spatial/temporal filters to metadata?
446-
metadata = metadata.filter_bands(band_names=bands)
447-
except Exception:
447+
if not metadata.has_band_dimension():
448+
metadata = metadata.add_dimension(
449+
name="bands",
450+
type="bands",
451+
label=None,
452+
)
453+
if all(metadata.band_dimension.contains_band(b) for b in bands):
454+
metadata = metadata.filter_bands(band_names=bands)
455+
else:
456+
logging.warning(
457+
"Some bands are not available in the collection metadata. Using requested bands as is."
458+
)
459+
metadata = metadata.rename_labels(dimension="bands", target=bands)
460+
except Exception as e:
448461
log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
449462
metadata = None
450463
return cls(graph=graph, connection=connection, metadata=metadata)

tests/rest/test_connection.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,7 @@ def test_load_stac_no_cube_extension_temporal_dimension(self, con120, tmp_path,
29572957
cube = con120.load_stac(str(stac_path))
29582958
assert cube.metadata.temporal_dimension == TemporalDimension(name="t", extent=dim_extent)
29592959

2960-
def test_load_stac_band_filtering(self, con120, tmp_path):
2960+
def test_load_stac_band_filtering(self, con120, tmp_path, caplog):
29612961
stac_path = tmp_path / "stac.json"
29622962
stac_data = StacDummyBuilder.collection(
29632963
summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}, {"name": "B03"}]}
@@ -2971,6 +2971,18 @@ def test_load_stac_band_filtering(self, con120, tmp_path):
29712971
cube = con120.load_stac(str(stac_path), bands=["B03", "B02"])
29722972
assert cube.metadata.band_names == ["B03", "B02"]
29732973

2974+
caplog.set_level(logging.WARNING)
2975+
# Test with non-existing bands in the collection metadata
2976+
cube = con120.load_stac(str(stac_path), bands=["B04"])
2977+
assert cube.metadata.band_names == ["B04"]
2978+
assert "Some bands are not available in the collection metadata. Using requested bands as is." in caplog.text
2979+
caplog.clear()
2980+
2981+
cube = con120.load_stac(str(stac_path), bands=["B03", "B04"])
2982+
assert cube.metadata.band_names == ["B03", "B04"]
2983+
assert "Some bands are not available in the collection metadata. Using requested bands as is." in caplog.text
2984+
caplog.clear()
2985+
29742986
@pytest.mark.parametrize(
29752987
"bands",
29762988
[

0 commit comments

Comments
 (0)