From 795f004e6bd30cf02f5b22bddb84720a9a340455 Mon Sep 17 00:00:00 2001 From: Paul Fouquet Date: Wed, 24 Jan 2024 11:35:00 +1300 Subject: [PATCH] refactor: check historical imagery based on its category rather than survey number --- scripts/stac/imagery/collection.py | 9 ++++++--- scripts/stac/imagery/metadata_constants.py | 5 +++++ scripts/stac/tests/generate_description_test.py | 2 +- scripts/stac/tests/generate_title_test.py | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/scripts/stac/imagery/collection.py b/scripts/stac/imagery/collection.py index 0609cf226..a67b32f00 100644 --- a/scripts/stac/imagery/collection.py +++ b/scripts/stac/imagery/collection.py @@ -13,8 +13,10 @@ HUMAN_READABLE_REGIONS, RURAL_AERIAL_PHOTOS, SATELLITE_IMAGERY, + SCANNED_AERIAL_PHOTOS, URBAN_AERIAL_PHOTOS, CollectionMetadata, + MissingMetadataError, SubtypeParameterError, ) from scripts.stac.imagery.provider import Provider, ProviderRole @@ -244,7 +246,9 @@ def _title(self) -> str: else: preview = None - if historic_survey_number: + if self.metadata["category"] == SCANNED_AERIAL_PHOTOS: + if not historic_survey_number: + raise MissingMetadataError("historic_survey_number") return " ".join(f"{name} {self.metadata['gsd']} {historic_survey_number} ({date}) {preview or ''}".split()) if self.metadata["category"] in [ @@ -283,7 +287,6 @@ def _description(self) -> str: """ # format optional metadata geographic_description = self.metadata.get("geographic_description") - historic_survey_number = self.metadata.get("historic_survey_number") event = self.metadata.get("event_name") # format date for metadata @@ -298,7 +301,7 @@ def _description(self) -> str: region = HUMAN_READABLE_REGIONS[self.metadata["region"]] - if historic_survey_number: + if self.metadata["category"] == SCANNED_AERIAL_PHOTOS: desc = f"Scanned aerial imagery within the {region} region captured in {date}" elif self.metadata["category"] == SATELLITE_IMAGERY: desc = f"Satellite imagery within the {region} region captured in {date}" diff --git a/scripts/stac/imagery/metadata_constants.py b/scripts/stac/imagery/metadata_constants.py index 2bdf5b386..09a17806e 100644 --- a/scripts/stac/imagery/metadata_constants.py +++ b/scripts/stac/imagery/metadata_constants.py @@ -31,6 +31,11 @@ def __init__(self, category: str) -> None: self.message = f"Unrecognised/Unimplemented Subtype Parameter: {category}" +class MissingMetadataError(Exception): + def __init__(self, metadata: str) -> None: + self.message = f"Missing metadata: {metadata}" + + AERIAL_PHOTOS = "aerial-photos" SCANNED_AERIAL_PHOTOS = "scanned-aerial-photos" RURAL_AERIAL_PHOTOS = "rural-aerial-photos" diff --git a/scripts/stac/tests/generate_description_test.py b/scripts/stac/tests/generate_description_test.py index 74c26cb0b..f17a0a631 100644 --- a/scripts/stac/tests/generate_description_test.py +++ b/scripts/stac/tests/generate_description_test.py @@ -70,7 +70,7 @@ def test_generate_description_satellite_imagery(metadata: Tuple[CollectionMetada def test_generate_description_historic_imagery(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: metadata_auck, _ = metadata - metadata_auck["category"] = "aerial-photos" + metadata_auck["category"] = "scanned-aerial-photos" metadata_auck["historic_survey_number"] = "SNC8844" collection = ImageryCollection(metadata_auck) description = "Scanned aerial imagery within the Auckland region captured in 2023." diff --git a/scripts/stac/tests/generate_title_test.py b/scripts/stac/tests/generate_title_test.py index a299db007..82629503d 100644 --- a/scripts/stac/tests/generate_title_test.py +++ b/scripts/stac/tests/generate_title_test.py @@ -4,7 +4,7 @@ import pytest from scripts.stac.imagery.collection import ImageryCollection -from scripts.stac.imagery.metadata_constants import CollectionMetadata +from scripts.stac.imagery.metadata_constants import CollectionMetadata, MissingMetadataError @pytest.fixture(name="metadata", autouse=True) @@ -68,12 +68,22 @@ def test_generate_satellite_imagery_title(metadata: Tuple[CollectionMetadata, Co def test_generate_historic_imagery_title(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: title = "Auckland 0.3m SNC8844 (2023)" metadata_auck, _ = metadata - metadata_auck["category"] = "aerial-photos" + metadata_auck["category"] = "scanned-aerial-photos" metadata_auck["historic_survey_number"] = "SNC8844" collection = ImageryCollection(metadata_auck) assert collection.stac["title"] == title +def test_generate_historic_imagery_title_missing_number(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: + metadata_auck, _ = metadata + metadata_auck["category"] = "scanned-aerial-photos" + metadata_auck["historic_survey_number"] = None + with pytest.raises(MissingMetadataError) as excinfo: + ImageryCollection(metadata_auck) + + assert "historic_survey_number" in str(excinfo.value) + + def test_generate_title_long_date(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: metadata_auck, _ = metadata metadata_auck["end_datetime"] = datetime(2024, 1, 1)