Skip to content

Commit 3f50dbc

Browse files
rewritten metadata_from_stac to also read the band wavelength and common names Open-EO#527
1 parent e29b2a4 commit 3f50dbc

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

openeo/metadata.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,21 @@ def metadata_from_stac(url: str) -> CubeMetadata:
533533
:return: A :py:class:`CubeMetadata` containing the DataCube band metadata from the url.
534534
"""
535535

536-
def get_band_names(asst: pystac.Asset) -> List[Band]:
537-
return [Band(eo_band["name"]) for eo_band in asst.extra_fields["eo:bands"]]
536+
def get_band_metadata(eo_bands_location: dict) -> List[Band]:
537+
return [
538+
Band(name=band["name"], common_name=band.get("common_name"), wavelength_um=band.get("center_wavelength"))
539+
for band in eo_bands_location.get("eo:bands", [])
540+
]
541+
542+
def get_band_names(bands: List[Band]) -> List[str]:
543+
return [band.name for band in bands]
538544

539545
def is_band_asset(asset: pystac.Asset) -> bool:
540546
return "eo:bands" in asset.extra_fields
541547

542548
stac_object = pystac.read_file(href=url)
543549

544-
band_names = []
550+
bands = []
545551
collection = None
546552

547553
if isinstance(stac_object, pystac.Item):
@@ -553,11 +559,11 @@ def is_band_asset(asset: pystac.Asset) -> bool:
553559
eo_bands_location = item.get_collection().summaries.lists
554560
else:
555561
eo_bands_location = {}
556-
band_names = [Band(b["name"]) for b in eo_bands_location.get("eo:bands", [])]
562+
bands = get_band_metadata(eo_bands_location)
557563

558564
elif isinstance(stac_object, pystac.Collection):
559565
collection = stac_object
560-
band_names = [Band(b["name"]) for b in collection.summaries.lists.get("eo:bands", [])]
566+
bands = get_band_metadata(collection.summaries.lists)
561567

562568
# Summaries is not a required field in a STAC collection, so also check the assets
563569
for itm in collection.get_items():
@@ -568,16 +574,16 @@ def is_band_asset(asset: pystac.Asset) -> bool:
568574
}
569575

570576
for asset in band_assets.values():
571-
asset_band_names = get_band_names(asset)
572-
for asset_band_name in asset_band_names:
573-
if asset_band_name not in band_names:
574-
band_names.append(asset_band_name)
577+
asset_bands = get_band_metadata(asset.extra_fields)
578+
for asset_band in asset_bands:
579+
if asset_band.name not in get_band_names(bands):
580+
bands.append(asset_band)
575581

576582
else:
577583
assert isinstance(stac_object, pystac.Catalog)
578584
catalog = stac_object
579-
band_names = [Band(b["name"]) for b in catalog.extra_fields.get("summaries", {}).get("eo:bands", [])]
585+
bands = get_band_metadata(catalog.extra_fields.get("summaries", {}))
580586

581-
band_dimension = BandDimension(name="bands", bands=band_names)
587+
band_dimension = BandDimension(name="bands", bands=bands)
582588
metadata = CubeMetadata(dimensions=[band_dimension])
583589
return metadata

0 commit comments

Comments
 (0)