Skip to content

Commit

Permalink
Allow creating load_stac datacube without connection object. #638
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileSonneveld committed Oct 2, 2024
1 parent 33317b0 commit a49493d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
29 changes: 8 additions & 21 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
CollectionMetadata,
SpatialDimension,
TemporalDimension,
metadata_from_stac,
)
from openeo.rest import (
DEFAULT_DOWNLOAD_CHUNK_SIZE,
Expand Down Expand Up @@ -1415,26 +1414,14 @@ def load_stac(
Argument ``temporal_extent``: add support for year/month shorthand notation
as discussed at :ref:`date-shorthand-handling`.
"""
# TODO #425 move this implementation to `DataCube` and just forward here (like with `load_collection`)
# TODO #425 detect actual metadata from URL
arguments = {"url": url}
# TODO #425 more normalization/validation of extent/band parameters
if spatial_extent:
arguments["spatial_extent"] = spatial_extent
if temporal_extent:
arguments["temporal_extent"] = DataCube._get_temporal_extent(extent=temporal_extent)
if bands:
arguments["bands"] = bands
if properties:
arguments["properties"] = {
prop: build_child_callback(pred, parent_parameters=["value"]) for prop, pred in properties.items()
}
cube = self.datacube_from_process(process_id="load_stac", **arguments)
try:
cube.metadata = metadata_from_stac(url)
except Exception:
_log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
return cube
return DataCube.load_stac(
url=url,
spatial_extent=spatial_extent,
temporal_extent=temporal_extent,
bands=bands,
properties=properties,
connection=self,
)

def load_stac_from_job(
self,
Expand Down
31 changes: 31 additions & 0 deletions openeo/rest/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
CollectionMetadata,
SpatialDimension,
TemporalDimension,
metadata_from_stac,
)
from openeo.processes import ProcessBuilder
from openeo.rest import BandMathException, OpenEoClientException, OperatorException
Expand Down Expand Up @@ -259,6 +260,36 @@ def load_disk_collection(cls, connection: Connection, file_format: str, glob_pat
)
return cls(graph=pg, connection=connection)

@classmethod
def load_stac(
cls,
url: str,
spatial_extent: Union[Dict[str, float], Parameter, None] = None,
temporal_extent: Union[Sequence[InputDate], Parameter, str, None] = None,
bands: Optional[List[str]] = None,
properties: Optional[Dict[str, Union[str, PGNode, Callable]]] = None,
connection: Connection = None,
) -> DataCube:
arguments = {"url": url}
# TODO #425 more normalization/validation of extent/band parameters
if spatial_extent:
arguments["spatial_extent"] = spatial_extent
if temporal_extent:
arguments["temporal_extent"] = DataCube._get_temporal_extent(extent=temporal_extent)
if bands:
arguments["bands"] = bands
if properties:
arguments["properties"] = {
prop: build_child_callback(pred, parent_parameters=["value"]) for prop, pred in properties.items()
}
graph = PGNode("load_stac", arguments=arguments)
try:
metadata = metadata_from_stac(url)
except Exception:
log.warning(f"Failed to extract cube metadata from STAC URL {url}", exc_info=True)
metadata = None
return cls(graph=graph, connection=connection, metadata=metadata)

@classmethod
def _get_temporal_extent(
cls,
Expand Down

0 comments on commit a49493d

Please sign in to comment.