Skip to content

Commit e3a1f19

Browse files
Initial band metadata added to load_stac Open-EO#527
1 parent 70b43f2 commit e3a1f19

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

openeo/rest/connection.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import logging
99
import os
10+
import pystac
1011
import shlex
1112
import sys
1213
import warnings
@@ -27,7 +28,7 @@
2728
from openeo.internal.jupyter import VisualDict, VisualList
2829
from openeo.internal.processes.builder import ProcessBuilderBase
2930
from openeo.internal.warnings import deprecated, legacy_alias
30-
from openeo.metadata import Band, BandDimension, CollectionMetadata, SpatialDimension, TemporalDimension
31+
from openeo.metadata import Band, BandDimension, CollectionMetadata, CubeMetadata, SpatialDimension, TemporalDimension
3132
from openeo.rest import (
3233
CapabilitiesException,
3334
OpenEoApiError,
@@ -1149,6 +1150,39 @@ def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] =
11491150
"""
11501151
return self.datacube_from_flat_graph(load_json_resource(src), parameters=parameters)
11511152

1153+
def metadata_from_stac(self, url: str) -> CubeMetadata:
1154+
"""
1155+
Reads the band metadata a static STAC catalog or a STAC API Collection and returns it as a :py:class:`CubeMetadata`
1156+
1157+
:param url: The URL to a static STAC catalog (STAC Item, STAC Collection, or STAC Catalog) or a specific STAC API Collection
1158+
:return: A :py:class:`CubeMetadata` containing the DataCube band metadata from the url.
1159+
"""
1160+
collection = pystac.read_file(href=url)
1161+
1162+
def get_band_names(itm: pystac.Item, asst: pystac.Asset) -> List[Band]:
1163+
return [Band(eo_band["name"]) for eo_band in asst.extra_fields["eo:bands"]]
1164+
1165+
def is_band_asset(asset: pystac.Asset) -> bool:
1166+
return "eo:bands" in asset.extra_fields
1167+
1168+
band_names = []
1169+
for itm in collection.get_items():
1170+
band_assets = {
1171+
asset_id: asset
1172+
for asset_id, asset in dict(sorted(itm.get_assets().items())).items()
1173+
if is_band_asset(asset)
1174+
}
1175+
1176+
for asset_id, asset in band_assets.items():
1177+
asset_band_names = get_band_names(itm, asset)
1178+
for asset_band_name in asset_band_names:
1179+
if asset_band_name not in band_names:
1180+
band_names.append(asset_band_name)
1181+
1182+
band_dimension = BandDimension(name="bands", bands=band_names)
1183+
metadata = CubeMetadata(dimensions=[band_dimension])
1184+
return metadata
1185+
11521186
@openeo_process
11531187
def load_collection(
11541188
self,
@@ -1247,6 +1281,7 @@ def load_stac(
12471281
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
12481282
bands: Optional[List[str]] = None,
12491283
properties: Optional[Dict[str, Union[str, PGNode, Callable]]] = None,
1284+
get_metadata: Optional[bool] = False,
12501285
) -> DataCube:
12511286
"""
12521287
Loads data from a static STAC catalog or a STAC API Collection and returns the data as a processable :py:class:`DataCube`.
@@ -1340,6 +1375,9 @@ def load_stac(
13401375
The value must be a condition (user-defined process) to be evaluated against a STAC API.
13411376
This parameter is not supported for static STAC.
13421377
1378+
:param get_metadata:
1379+
Specify whether to also load the band name metadata from the URL.
1380+
13431381
.. versionadded:: 0.17.0
13441382
13451383
.. versionchanged:: 0.23.0
@@ -1361,6 +1399,8 @@ def load_stac(
13611399
prop: build_child_callback(pred, parent_parameters=["value"]) for prop, pred in properties.items()
13621400
}
13631401
cube = self.datacube_from_process(process_id="load_stac", **arguments)
1402+
if get_metadata:
1403+
cube.metadata = self.metadata_from_stac(url)
13641404
return cube
13651405

13661406
def load_ml_model(self, id: Union[str, BatchJob]) -> MlModel:

0 commit comments

Comments
 (0)