Skip to content

Commit

Permalink
Issue #527/#548 minor metadata_from_stac fine-tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Mar 26, 2024
1 parent 59655b8 commit 5cddb33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 6 additions & 7 deletions openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ def metadata_from_stac(url: str) -> CubeMetadata:
"""

def get_band_metadata(eo_bands_location: dict) -> List[Band]:
# TODO: return None iso empty list when no metadata?
return [
Band(name=band["name"], common_name=band.get("common_name"), wavelength_um=band.get("center_wavelength"))
for band in eo_bands_location.get("eo:bands", [])
Expand All @@ -547,15 +548,12 @@ def is_band_asset(asset: pystac.Asset) -> bool:

stac_object = pystac.read_file(href=url)

bands = []
collection = None

if isinstance(stac_object, pystac.Item):
item = stac_object
if "eo:bands" in item.properties:
eo_bands_location = item.properties
elif item.get_collection() is not None:
collection = item.get_collection()
# TODO: Also do asset based band detection (like below)?
eo_bands_location = item.get_collection().summaries.lists
else:
eo_bands_location = {}
Expand All @@ -574,12 +572,13 @@ def is_band_asset(asset: pystac.Asset) -> bool:
for asset_band in asset_bands:
if asset_band.name not in get_band_names(bands):
bands.append(asset_band)

else:
assert isinstance(stac_object, pystac.Catalog)
elif isinstance(stac_object, pystac.Catalog):
catalog = stac_object
bands = get_band_metadata(catalog.extra_fields.get("summaries", {}))
else:
raise ValueError(stac_object)

# TODO: conditionally include band dimension when there was actual indication of band metadata?
band_dimension = BandDimension(name="bands", bands=bands)
metadata = CubeMetadata(dimensions=[band_dimension])
return metadata
2 changes: 2 additions & 0 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ def filter_bbox(self, bbox):
},
["B01", "B02"],
),
# TODO: test asset handling in collection?
(
{
"type": "Catalog",
Expand All @@ -830,6 +831,7 @@ def filter_bbox(self, bbox):
},
["SCL", "B08"],
),
# TODO: test asset handling in item?
],
)
def test_metadata_from_stac(tmp_path, test_stac, expected):
Expand Down

0 comments on commit 5cddb33

Please sign in to comment.