7
7
import json
8
8
import logging
9
9
import os
10
+ import pystac
10
11
import shlex
11
12
import sys
12
13
import warnings
27
28
from openeo .internal .jupyter import VisualDict , VisualList
28
29
from openeo .internal .processes .builder import ProcessBuilderBase
29
30
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
31
32
from openeo .rest import (
32
33
CapabilitiesException ,
33
34
OpenEoApiError ,
@@ -1149,6 +1150,39 @@ def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] =
1149
1150
"""
1150
1151
return self .datacube_from_flat_graph (load_json_resource (src ), parameters = parameters )
1151
1152
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
+
1152
1186
@openeo_process
1153
1187
def load_collection (
1154
1188
self ,
@@ -1247,6 +1281,7 @@ def load_stac(
1247
1281
temporal_extent : Union [Sequence [InputDate ], Parameter , str , None ] = None ,
1248
1282
bands : Optional [List [str ]] = None ,
1249
1283
properties : Optional [Dict [str , Union [str , PGNode , Callable ]]] = None ,
1284
+ get_metadata : Optional [bool ] = False ,
1250
1285
) -> DataCube :
1251
1286
"""
1252
1287
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(
1340
1375
The value must be a condition (user-defined process) to be evaluated against a STAC API.
1341
1376
This parameter is not supported for static STAC.
1342
1377
1378
+ :param get_metadata:
1379
+ Specify whether to also load the band name metadata from the URL.
1380
+
1343
1381
.. versionadded:: 0.17.0
1344
1382
1345
1383
.. versionchanged:: 0.23.0
@@ -1361,6 +1399,8 @@ def load_stac(
1361
1399
prop : build_child_callback (pred , parent_parameters = ["value" ]) for prop , pred in properties .items ()
1362
1400
}
1363
1401
cube = self .datacube_from_process (process_id = "load_stac" , ** arguments )
1402
+ if get_metadata :
1403
+ cube .metadata = self .metadata_from_stac (url )
1364
1404
return cube
1365
1405
1366
1406
def load_ml_model (self , id : Union [str , BatchJob ]) -> MlModel :
0 commit comments