From 446fdb959e79c9cd81b156de6195d17f65bec8b5 Mon Sep 17 00:00:00 2001 From: Paul Fouquet Date: Wed, 18 Dec 2024 11:07:43 +1300 Subject: [PATCH 1/2] fix: derived_from links should not be duplicated when Item is updated TDE-1356 --- scripts/stac/imagery/create_stac.py | 3 +++ .../stac/imagery/tests/create_stac_test.py | 20 +++++++++++++++++++ scripts/tests/data/fake_item.json | 8 ++++++++ 3 files changed, 31 insertions(+) create mode 100644 scripts/tests/data/fake_item.json diff --git a/scripts/stac/imagery/create_stac.py b/scripts/stac/imagery/create_stac.py index 6374bb770..3787839dc 100644 --- a/scripts/stac/imagery/create_stac.py +++ b/scripts/stac/imagery/create_stac.py @@ -130,6 +130,9 @@ def create_item( gdalinfo_result = gdal_info(asset_path) if derived_from is not None: + # Remove existing derived_from links in case of resupply + item.stac["links"] = [link for link in item.stac["links"] if link["rel"] != "derived_from"] + for derived in derived_from: derived_item_content = read(derived) derived_stac = json.loads(derived_item_content.decode("UTF-8")) diff --git a/scripts/stac/imagery/tests/create_stac_test.py b/scripts/stac/imagery/tests/create_stac_test.py index ed7ad613e..a3048a97f 100644 --- a/scripts/stac/imagery/tests/create_stac_test.py +++ b/scripts/stac/imagery/tests/create_stac_test.py @@ -46,11 +46,27 @@ def test_create_item_when_resupplying(subtests: SubTests, tmp_path: Path) -> Non item_name = "empty" existing_item = tmp_path / f"{item_name}.json" tiff_path = f"./scripts/tests/data/{item_name}.tiff" + derived_from_path = "./scripts/tests/data/fake_item.json" created_datetime = "created datetime" updated_datetime = "updated datetime" + links = [ + { + "href": derived_from_path, + "rel": "derived_from", + "type": "application/geo+json", + "file:checksum": "1220f33d983b9c84d3d0c44f37f4a1b842295a960abcdd3889b898f42988f9e93604", + }, + { + "href": "path/to/an/old/derived_from", + "rel": "derived_from", + "type": "application/geo+json", + "file:checksum": "123", + }, + ] existing_item_content = { "type": "Feature", "id": item_name, + "links": links, "assets": { "visual": { "href": tiff_path, @@ -77,6 +93,7 @@ def test_create_item_when_resupplying(subtests: SubTests, tmp_path: Path) -> Non "any GDAL version", current_datetime, fake_gdal_info, + derived_from=[derived_from_path], odr_url=tmp_path.as_posix(), ) @@ -92,6 +109,9 @@ def test_create_item_when_resupplying(subtests: SubTests, tmp_path: Path) -> Non with subtests.test(msg="assets.visual.updated"): assert item.stac["assets"]["visual"]["updated"] == updated_datetime + with subtests.test(msg="links"): + assert len(item.stac["links"]) == 3 + def test_create_item_when_resupplying_with_changed_file(subtests: SubTests, tmp_path: Path) -> None: item_name = "empty" diff --git a/scripts/tests/data/fake_item.json b/scripts/tests/data/fake_item.json new file mode 100644 index 000000000..cad19b572 --- /dev/null +++ b/scripts/tests/data/fake_item.json @@ -0,0 +1,8 @@ +{ + "type": "Feature", + "id": "Fake Item", + "properties": { + "start_datetime": "2018-11-30T11:00:00Z", + "end_datetime": "2020-01-31T11:00:00Z" + } +} From ab2cf9ead235de964293d179490aeeac20440dbd Mon Sep 17 00:00:00 2001 From: Paul Fouquet Date: Wed, 18 Dec 2024 16:30:25 +1300 Subject: [PATCH 2/2] fix: should always remove existing derived_from links --- scripts/stac/imagery/create_stac.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/stac/imagery/create_stac.py b/scripts/stac/imagery/create_stac.py index 3787839dc..ae44152ee 100644 --- a/scripts/stac/imagery/create_stac.py +++ b/scripts/stac/imagery/create_stac.py @@ -129,10 +129,11 @@ def create_item( if not gdalinfo_result: gdalinfo_result = gdal_info(asset_path) - if derived_from is not None: + if item.stac.get("links") is not None: # Remove existing derived_from links in case of resupply item.stac["links"] = [link for link in item.stac["links"] if link["rel"] != "derived_from"] + if derived_from is not None: for derived in derived_from: derived_item_content = read(derived) derived_stac = json.loads(derived_item_content.decode("UTF-8"))