Skip to content

Commit

Permalink
refactor: change function name
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfouquet committed Feb 25, 2025
1 parent a21a889 commit fbb18e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
23 changes: 8 additions & 15 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,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 @@ -261,10 +258,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 @@ -295,18 +289,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_extent()
if bbox:
self.stac["extent"]["spatial"]["bbox"] = [bbox]
if interval:
self.stac["extent"]["temporal"]["interval"] = [interval]

def reset_extent(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 get_items_stac(self) -> list[dict[str, Any]]:
"""Get the STAC Items from the Collection.
Expand Down
11 changes: 6 additions & 5 deletions scripts/stac/imagery/create_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create_collection(
os.path.join(odr_url, "collection.json"), collection_metadata, current_datetime
)
published_items = collection.get_items_stac()
stac_items = prepare_resupply(collection, published_items, stac_items)
stac_items = merge_item_list_for_resupply(collection, published_items, stac_items)

else:
collection = ImageryCollection(
Expand Down Expand Up @@ -111,10 +111,11 @@ def get_items_to_replace(supplied_items: list[dict[str, Any]], published_items:
return items_to_replace


def prepare_resupply(
def merge_item_list_for_resupply(
collection: ImageryCollection, published_items: list[dict[str, Any]], supplied_items: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Prepare the STAC Collection for resupply and merge the existing Items with the resupply Items into a single list.
"""Merge the existing Items with the resupply Items into a single list.
Prepare the Collection for resupply by removing the Items that need to be replaced.
Args:
collection: a published Collection
Expand All @@ -132,8 +133,8 @@ def prepare_resupply(

# Remove all Item links
collection.stac["links"] = [link for link in collection.stac.get("links", []) if link["rel"] != "item"]
# Remove extents so they can be recalculated
collection.stac.pop("extent", None)
# Empty extents so they can be recalculated
collection.reset_extent()

return supplied_items + published_items

Expand Down
11 changes: 7 additions & 4 deletions scripts/stac/imagery/tests/create_stac_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from scripts.datetimes import format_rfc_3339_datetime_string
from scripts.gdal.gdalinfo import GdalInfo
from scripts.stac.imagery.collection import ImageryCollection
from scripts.stac.imagery.create_stac import create_collection, create_item, get_items_to_replace, prepare_resupply
from scripts.stac.imagery.create_stac import create_collection, create_item, get_items_to_replace, merge_item_list_for_resupply
from scripts.stac.imagery.metadata_constants import CollectionMetadata
from scripts.stac.imagery.tests.generators import any_multihash_as_hex
from scripts.stac.util.STAC_VERSION import STAC_VERSION
Expand Down Expand Up @@ -530,7 +530,9 @@ def test_get_items_to_replace() -> None:
]


def test_prepare_resupply(fake_collection_metadata: CollectionMetadata, fake_linz_slug: str, subtests: SubTests) -> None:
def test_merge_item_list_for_resupply(
fake_collection_metadata: CollectionMetadata, fake_linz_slug: str, subtests: SubTests
) -> None:
published_items = [
{"type": "Feature", "id": "item_a"},
{"type": "Feature", "id": "item_b"},
Expand All @@ -555,7 +557,7 @@ def test_prepare_resupply(fake_collection_metadata: CollectionMetadata, fake_lin
fake_collection_metadata, any_epoch_datetime_string(), any_epoch_datetime_string(), fake_linz_slug
)
collection.stac["links"] = links
merged_items = prepare_resupply(collection, published_items, supplied_items)
merged_items = merge_item_list_for_resupply(collection, published_items, supplied_items)

with subtests.test("merged items"):
assert merged_items == [
Expand All @@ -573,4 +575,5 @@ def test_prepare_resupply(fake_collection_metadata: CollectionMetadata, fake_lin
assert collection.stac["links"] == [{"rel": "self", "href": "./collection.json"}]

with subtests.test("extent"):
assert "extent" not in collection.stac
assert collection.stac["extent"]["spatial"]["bbox"] is None
assert collection.stac["extent"]["temporal"]["interval"] is None

0 comments on commit fbb18e0

Please sign in to comment.