|
7 | 7 | import json
|
8 | 8 | import logging
|
9 | 9 | import os
|
10 |
| -import pystac |
11 | 10 | import shlex
|
12 | 11 | import sys
|
13 | 12 | import warnings
|
|
28 | 27 | from openeo.internal.jupyter import VisualDict, VisualList
|
29 | 28 | from openeo.internal.processes.builder import ProcessBuilderBase
|
30 | 29 | from openeo.internal.warnings import deprecated, legacy_alias
|
31 |
| -from openeo.metadata import Band, BandDimension, CollectionMetadata, CubeMetadata, SpatialDimension, TemporalDimension |
| 30 | +from openeo.metadata import ( |
| 31 | + Band, |
| 32 | + BandDimension, |
| 33 | + CollectionMetadata, |
| 34 | + SpatialDimension, |
| 35 | + TemporalDimension, |
| 36 | + metadata_from_stac, |
| 37 | +) |
32 | 38 | from openeo.rest import (
|
33 | 39 | CapabilitiesException,
|
34 | 40 | OpenEoApiError,
|
@@ -1150,63 +1156,6 @@ def datacube_from_json(self, src: Union[str, Path], parameters: Optional[dict] =
|
1150 | 1156 | """
|
1151 | 1157 | return self.datacube_from_flat_graph(load_json_resource(src), parameters=parameters)
|
1152 | 1158 |
|
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 |
| - |
1161 |
| - def get_band_names(asst: pystac.Asset) -> List[Band]: |
1162 |
| - return [Band(eo_band["name"]) for eo_band in asst.extra_fields["eo:bands"]] |
1163 |
| - |
1164 |
| - def is_band_asset(asset: pystac.Asset) -> bool: |
1165 |
| - return "eo:bands" in asset.extra_fields |
1166 |
| - |
1167 |
| - stac_object = pystac.read_file(href=url) |
1168 |
| - |
1169 |
| - band_names = [] |
1170 |
| - collection = None |
1171 |
| - |
1172 |
| - if isinstance(stac_object, pystac.Item): |
1173 |
| - item = stac_object |
1174 |
| - if "eo:bands" in item.properties: |
1175 |
| - eo_bands_location = item.properties |
1176 |
| - elif item.get_collection() is not None: |
1177 |
| - collection = item.get_collection() |
1178 |
| - eo_bands_location = item.get_collection().summaries.lists |
1179 |
| - else: |
1180 |
| - eo_bands_location = {} |
1181 |
| - band_names = [Band(b["name"]) for b in eo_bands_location.get("eo:bands", [])] |
1182 |
| - |
1183 |
| - elif isinstance(stac_object, pystac.Collection): |
1184 |
| - collection = stac_object |
1185 |
| - band_names = [Band(b["name"]) for b in collection.summaries.lists.get("eo:bands", [])] |
1186 |
| - |
1187 |
| - # Summaries is not a required field in a STAC collection, so also check the assets |
1188 |
| - for itm in collection.get_items(): |
1189 |
| - band_assets = { |
1190 |
| - asset_id: asset |
1191 |
| - for asset_id, asset in dict(sorted(itm.get_assets().items())).items() |
1192 |
| - if is_band_asset(asset) |
1193 |
| - } |
1194 |
| - |
1195 |
| - for asset in band_assets.values(): |
1196 |
| - asset_band_names = get_band_names(asset) |
1197 |
| - for asset_band_name in asset_band_names: |
1198 |
| - if asset_band_name not in band_names: |
1199 |
| - band_names.append(asset_band_name) |
1200 |
| - |
1201 |
| - else: |
1202 |
| - assert isinstance(stac_object, pystac.Catalog) |
1203 |
| - catalog = stac_object |
1204 |
| - band_names = [Band(b["name"]) for b in catalog.extra_fields.get("summaries", {}).get("eo:bands", [])] |
1205 |
| - |
1206 |
| - band_dimension = BandDimension(name="bands", bands=band_names) |
1207 |
| - metadata = CubeMetadata(dimensions=[band_dimension]) |
1208 |
| - return metadata |
1209 |
| - |
1210 | 1159 | @openeo_process
|
1211 | 1160 | def load_collection(
|
1212 | 1161 | self,
|
@@ -1420,7 +1369,7 @@ def load_stac(
|
1420 | 1369 | }
|
1421 | 1370 | cube = self.datacube_from_process(process_id="load_stac", **arguments)
|
1422 | 1371 | try:
|
1423 |
| - cube.metadata = self.metadata_from_stac(url) |
| 1372 | + cube.metadata = metadata_from_stac(url) |
1424 | 1373 | except Exception:
|
1425 | 1374 | _log.warning("Python client could not read band metadata from URL.")
|
1426 | 1375 | return cube
|
|
0 commit comments