Skip to content

Commit

Permalink
Issue #442 make collection metadata based normalization optional
Browse files Browse the repository at this point in the history
Allow alternative operation modes where less/no metadata is available
  • Loading branch information
soxofaan committed Sep 14, 2023
1 parent 20439a6 commit dbcc1ee
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 151 deletions.
19 changes: 5 additions & 14 deletions openeo/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,6 @@ def __init__(self, metadata: dict, dimensions: List[Dimension] = None):
if dim.type == "temporal":
self._temporal_dimension = dim

@classmethod
def get_or_create(cls, metadata: Union[dict, "CollectionMetadata", None]) -> CollectionMetadata:
"""Get or create CollectionMetadata from given argument."""
if isinstance(metadata, cls):
return metadata
else:
return cls(metadata=metadata or {})

def __eq__(self, o: Any) -> bool:
return isinstance(o, CollectionMetadata) and self._dimensions == o._dimensions

Expand Down Expand Up @@ -348,15 +340,11 @@ def extent(self) -> dict:
def dimension_names(self) -> List[str]:
return list(d.name for d in self._dimensions)

def assert_valid_dimension(self, dimension: str, just_warn: bool = False) -> str:
def assert_valid_dimension(self, dimension: str) -> str:
"""Make sure given dimension name is valid."""
names = self.dimension_names()
if dimension not in names:
msg = f"Invalid dimension {dimension!r}. Should be one of {names}"
if just_warn:
_log.warning(msg)
else:
raise ValueError(msg)
raise ValueError(f"Invalid dimension {dimension!r}. Should be one of {names}")
return dimension

def has_band_dimension(self) -> bool:
Expand Down Expand Up @@ -397,6 +385,7 @@ def band_common_names(self) -> List[str]:
return self.band_dimension.common_names

def get_band_index(self, band: Union[int, str]) -> int:
# TODO: eliminate this shortcut for smaller API surface
return self.band_dimension.band_index(band)

def filter_bands(self, band_names: List[Union[int, str]]) -> CollectionMetadata:
Expand Down Expand Up @@ -452,6 +441,8 @@ def rename_dimension(self, source: str, target: str) -> CollectionMetadata:
def reduce_dimension(self, dimension_name: str) -> CollectionMetadata:
"""Create new metadata object by collapsing/reducing a dimension."""
# TODO: option to keep reduced dimension (with a single value)?
# TODO: rename argument to `name` for more internal consistency
# TODO: merge with drop_dimension (which does the same).
self.assert_valid_dimension(dimension_name)
loc = self.dimension_names().index(dimension_name)
dimensions = self._dimensions[:loc] + self._dimensions[loc + 1:]
Expand Down
20 changes: 0 additions & 20 deletions openeo/rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,15 +1183,6 @@ def load_result(
:return: a :py:class:`DataCube`
"""
# TODO: add check that back-end supports `load_result` process?
metadata = CollectionMetadata(
{},
dimensions=[
SpatialDimension(name="x", extent=[]),
SpatialDimension(name="y", extent=[]),
TemporalDimension(name="t", extent=[]),
BandDimension(name="bands", bands=[Band(name="unknown")]),
],
)
cube = self.datacube_from_process(
process_id="load_result",
id=id,
Expand All @@ -1201,7 +1192,6 @@ def load_result(
bands=bands,
),
)
cube.metadata = metadata
return cube

@openeo_process
Expand Down Expand Up @@ -1309,15 +1299,6 @@ def load_stac(
"""
# TODO #425 move this implementation to `DataCube` and just forward here (like with `load_collection`)
# TODO #425 detect actual metadata from URL
metadata = CollectionMetadata(
{},
dimensions=[
SpatialDimension(name="x", extent=[]),
SpatialDimension(name="y", extent=[]),
TemporalDimension(name="t", extent=[]),
BandDimension(name="bands", bands=[Band(name="unknown")]),
],
)
arguments = {"url": url}
# TODO #425 more normalization/validation of extent/band parameters
if spatial_extent:
Expand All @@ -1331,7 +1312,6 @@ def load_stac(
prop: build_child_callback(pred, parent_parameters=["value"]) for prop, pred in properties.items()
}
cube = self.datacube_from_process(process_id="load_stac", **arguments)
cube.metadata = metadata
return cube

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

0 comments on commit dbcc1ee

Please sign in to comment.