Skip to content

Commit

Permalink
merge_cubes: merge temporal extents
Browse files Browse the repository at this point in the history
  • Loading branch information
bossie committed Sep 16, 2024
1 parent a47292b commit d195916
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 14 additions & 2 deletions openeogeotrellis/geopysparkcubemetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Union

import dateutil.parser
from openeo.metadata import CollectionMetadata, Dimension
from openeo.metadata import CollectionMetadata, Dimension, TemporalDimension
from openeogeotrellis.utils import reproject_cellsize

_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -73,7 +73,8 @@ def filter_temporal(self, start, end) -> 'GeopysparkCubeMetadata':

this_start, this_end = self._temporal_extent

if this_start > end or this_end < start: # no overlap
if this_start > end or this_end < start: # compared lexicographically
# no overlap
raise ValueError(start, end)

return self._clone_and_update(temporal_extent=(max(this_start, start), min(this_end, end)))
Expand All @@ -82,6 +83,17 @@ def filter_temporal(self, start, end) -> 'GeopysparkCubeMetadata':
def temporal_extent(self) -> tuple:
return self._temporal_extent

def with_temporal_extent(self, temporal_extent: tuple):
assert self.has_temporal_dimension()

return self._clone_and_update(
dimensions=[
TemporalDimension(d.name, temporal_extent) if isinstance(d, TemporalDimension) else d
for d in self._dimensions
],
temporal_extent=temporal_extent,
)

@property
def opensearch_link_titles(self) -> List[str]:
"""Get opensearch_link_titles from band dimension"""
Expand Down
10 changes: 9 additions & 1 deletion openeogeotrellis/geopysparkdatacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ def merge_cubes(self, other: 'GeopysparkDataCube', overlaps_resolver:str=None):
+ f" Left names: {leftBandNames}, right names: {rightBandNames}.",
)

# TODO properly combine bbox and temporal extents in metadata? Yes! Then pass to _apply_to_levels_geotrellis_rdd
# TODO properly combine bbox in metadata?
pr = pysc._jvm.org.openeo.geotrellis.OpenEOProcesses()
if self._is_spatial() and other._is_spatial():
def merge(rdd,other,level):
Expand Down Expand Up @@ -1003,6 +1003,14 @@ def merge(rdd,other,level):
if iband.name not in merged_data.metadata.band_names:
merged_data.metadata = merged_data.metadata.append_band(iband)

if self.metadata.has_temporal_dimension() and other.metadata.has_temporal_dimension():
self_lower, self_upper = self.metadata.temporal_dimension.extent
other_lower, other_upper = other.metadata.temporal_dimension.extent

merged_data.metadata = merged_data.metadata.with_temporal_extent(
(min([self_lower, other_lower]), max([self_upper, other_upper])) # compared lexicographically
)

return merged_data

# TODO legacy alias to be removed
Expand Down

0 comments on commit d195916

Please sign in to comment.