Skip to content

Commit

Permalink
fix: Treat item as dict
Browse files Browse the repository at this point in the history
We load the item from a JSON file into a `dict` in the production code,
but none of our automated tests verify this code path, so it broke.

This partially reverts commit ed177c6.
  • Loading branch information
l0b0 committed Jun 19, 2024
1 parent ed177c6 commit d191eb0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
13 changes: 6 additions & 7 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from collections.abc import Callable
from dataclasses import asdict
from datetime import datetime
from typing import Any

Expand All @@ -12,7 +11,7 @@
from scripts.files.fs import write
from scripts.json_codec import dict_to_json_bytes
from scripts.stac.imagery.capture_area import generate_capture_area, gsd_to_float
from scripts.stac.imagery.item import BoundingBox, ImageryItem
from scripts.stac.imagery.item import BoundingBox
from scripts.stac.imagery.metadata_constants import (
DATA_CATEGORIES,
DEM,
Expand Down Expand Up @@ -130,18 +129,18 @@ def add_capture_area(self, polygons: list[BaseGeometry], target: str, artifact_t
if StacExtensions.file.value not in self.stac["stac_extensions"]:
self.stac["stac_extensions"].append(StacExtensions.file.value)

def add_item(self, item: ImageryItem) -> None:
def add_item(self, item: dict[Any, Any]) -> None:
"""Add an `Item` to the `links` of the `Collection`.
Args:
item: STAC Item to add
"""
item_self_link = next((feat for feat in item.links if feat["rel"] == "self"), None)
file_checksum = checksum.multihash_as_hex(dict_to_json_bytes(asdict(item)))
item_self_link = next((feat for feat in item["links"] if feat["rel"] == "self"), None)
file_checksum = checksum.multihash_as_hex(dict_to_json_bytes(item))
if item_self_link:
self.add_link(href=item_self_link["href"], file_checksum=file_checksum)
self.update_temporal_extent(item.properties.start_datetime, item.properties.end_datetime)
self.update_spatial_extent(item.bbox)
self.update_temporal_extent(item["properties"]["start_datetime"], item["properties"]["end_datetime"])
self.update_spatial_extent(item["bbox"])

def add_link(self, href: str, file_checksum: str) -> None:
"""Add a `link` to the existing `links` list of the Collection.
Expand Down
3 changes: 2 additions & 1 deletion scripts/stac/imagery/tests/collection_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
from dataclasses import asdict
from datetime import datetime, timezone
from shutil import rmtree
from tempfile import TemporaryDirectory, mkdtemp
Expand Down Expand Up @@ -138,7 +139,7 @@ def test_add_item(metadata: CollectionMetadata, subtests: SubTests) -> None:
"BR34_5000_0304", item_file_path, now_function, start_datetime, end_datetime, geometry, bbox, collection.stac["id"]
)

collection.add_item(item)
collection.add_item(asdict(item))

links = collection.stac["links"].copy()

Expand Down

0 comments on commit d191eb0

Please sign in to comment.