Skip to content

Commit

Permalink
feat: reset Collection extents TDE-1359
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfouquet committed Feb 25, 2025
1 parent b96c851 commit d17e78b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
23 changes: 8 additions & 15 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ def update_spatial_extent(self, item_bbox: list[float]) -> None:
Args:
item_bbox: bounding box of an item added to the Collection
"""
if "extent" not in self.stac:
self.update_extent(bbox=item_bbox)
return
if self.stac["extent"]["spatial"]["bbox"] == [None]:
if not self.stac.get("extent", {}).get("spatial", {}).get("bbox"):
self.update_extent(bbox=item_bbox)
return

Expand All @@ -219,10 +216,7 @@ def update_temporal_extent(self, item_start_datetime: str, item_end_datetime: st
item_start_datetime: start date of an item added to the Collection
item_end_datetime: end date of an item added to the Collection
"""
if "extent" not in self.stac:
self.update_extent(interval=[item_start_datetime, item_end_datetime])
return
if self.stac["extent"]["temporal"]["interval"] == [None]:
if not self.stac.get("extent", {}).get("temporal", {}).get("interval"):
self.update_extent(interval=[item_start_datetime, item_end_datetime])
return

Expand Down Expand Up @@ -253,18 +247,17 @@ def update_extent(self, bbox: list[float] | None = None, interval: list[str] | N
interval: datetime interval. Defaults to None.
"""
if "extent" not in self.stac:
self.stac["extent"] = {
"spatial": {
"bbox": [bbox],
},
"temporal": {"interval": [interval]},
}
return
self.reset_extents()
if bbox:
self.stac["extent"]["spatial"]["bbox"] = [bbox]
if interval:
self.stac["extent"]["temporal"]["interval"] = [interval]

def reset_extents(self) -> None:
"""Reset the spatial and temporal extents of the Collection."""
self.stac.setdefault("extent", {}).setdefault("spatial", {})["bbox"] = None
self.stac.setdefault("extent", {}).setdefault("temporal", {})["interval"] = None

def write_to(self, destination: str) -> None:
"""Write the Collection in JSON format to the specified `destination`.
Expand Down
11 changes: 11 additions & 0 deletions scripts/stac/imagery/tests/collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,14 @@ def test_capture_dates_added(fake_collection_metadata: CollectionMetadata, fake_
"file:checksum": "1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"file:size": 0,
}


def test_reset_extents(fake_collection_metadata: CollectionMetadata, fake_linz_slug: str) -> None:
collection = ImageryCollection(
fake_collection_metadata, any_epoch_datetime_string(), any_epoch_datetime_string(), fake_linz_slug
)
collection.update_temporal_extent("2021-01-27T00:00:00Z", "2021-01-27T00:00:00Z")
collection.update_spatial_extent([1799667.5, 5815977.0, 1800422.5, 5814986.0])
collection.reset_extents()
assert collection.stac["extent"]["spatial"]["bbox"] is None
assert collection.stac["extent"]["temporal"]["interval"] is None

0 comments on commit d17e78b

Please sign in to comment.