Skip to content

Commit

Permalink
feat: add checksum to item links in collections TDE-1138 (#953)
Browse files Browse the repository at this point in the history
* feat: add checksum to item links in collections

* fix: formatting

* fix: update checksum after rebase

* fix: remove copy paste error

* fix: refactor didn't update docstring

* fix: simplify function as only called once
  • Loading branch information
MDavidson17 authored May 2, 2024
1 parent 2c96c13 commit afea8f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 6 additions & 5 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,21 @@ def add_item(self, item: Dict[Any, Any]) -> None:
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(json.dumps(item).encode("utf-8"))
if item_self_link:
self.add_link(href=item_self_link["href"])
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"])

def add_link(self, href: str, rel: str = "item", file_type: str = "application/json") -> None:
def add_link(self, href: str, file_checksum: str) -> None:
"""Add a `link` to the existing `links` list of the Collection.
Args:
href: path
rel: type of link. Defaults to "item".
file_type: type of file pointed by the link. Defaults to "application/json".
file_checksum: Optional checksum of file.
"""
self.stac["links"].append({"rel": rel, "href": href, "type": file_type})
link = {"rel": "item", "href": href, "type": "application/json", "file:checksum": file_checksum}
self.stac["links"].append(link)

def add_providers(self, providers: List[Provider]) -> None:
"""Add a list of Providers to the existing list of `providers` of the Collection.
Expand Down
7 changes: 6 additions & 1 deletion scripts/stac/imagery/tests/collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ def test_add_item(mocker: MockerFixture, metadata: CollectionMetadata, subtests:
collection.add_item(item.stac)

with subtests.test():
assert {"rel": "item", "href": "./BR34_5000_0304.json", "type": "application/json"} in collection.stac["links"]
assert {
"file:checksum": "1220a049888b3971d9ed3fd52b830cfeb379d7069d6b7a927456bcf1fabab0ec4f46",
"rel": "item",
"href": "./BR34_5000_0304.json",
"type": "application/json",
} in collection.stac["links"]

with subtests.test():
assert collection.stac["extent"]["temporal"]["interval"] == [[start_datetime, end_datetime]]
Expand Down

0 comments on commit afea8f0

Please sign in to comment.