From 94051b83f76c9983996e52cf1ca4fb3118c82716 Mon Sep 17 00:00:00 2001 From: Megan Davidson <33814653+MDavidson17@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:13:30 +1300 Subject: [PATCH] refactor: title & description TDE-955 (#835) * refactor: title & description * refactor: elevation title * fix: improve formatting --- scripts/stac/imagery/collection.py | 92 +++++++++---------- scripts/stac/imagery/metadata_constants.py | 2 + scripts/stac/tests/collection_test.py | 16 ++-- .../stac/tests/generate_description_test.py | 2 +- scripts/stac/tests/generate_title_test.py | 14 ++- 5 files changed, 65 insertions(+), 61 deletions(-) diff --git a/scripts/stac/imagery/collection.py b/scripts/stac/imagery/collection.py index a67b32f00..6e106bc19 100644 --- a/scripts/stac/imagery/collection.py +++ b/scripts/stac/imagery/collection.py @@ -210,35 +210,31 @@ def write_to(self, destination: str) -> None: def _title(self) -> str: """Generates the title for imagery and elevation datasets. - Satellite Imagery / Urban Aerial Photos / Rural Aerial Photos: - [geographic_description / Region if no geographic_description specified] [GSD] [?Event Name] [Data Sub-Type] - ([Year(s)]) [?- Preview] + Satellite Imagery / Urban Aerial Photos / Rural Aerial Photos / Scanned Aerial Photos: + https://github.com/linz/imagery/blob/master/docs/naming.md DEM / DSM: - [geographic_description / Region if no geographic_description specified] [?- Event Name] LiDAR [GSD] - [Data Sub-Type] ([Year(s)]) [?- Preview] - If Historic Survey Number: - [geographic_description / Region if no geographic_description specified] [GSD] [Survey Number] ([Year(s)]) - [?- Preview] - + https://github.com/linz/elevation/blob/master/docs/naming.md Returns: Dataset Title """ # 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 - if self.metadata["start_datetime"].year == self.metadata["end_datetime"].year: - date = str(self.metadata["start_datetime"].year) + if (start_year := self.metadata["start_datetime"].year) == (end_year := self.metadata["end_datetime"].year): + date = str(start_year) else: - date = f"{self.metadata['start_datetime'].year}-{self.metadata['end_datetime'].year}" + date = f"{start_year}-{end_year}" # determine dataset name + region = HUMAN_READABLE_REGIONS[self.metadata["region"]] if geographic_description: - name = geographic_description + imagery_name = geographic_description + elevation_description = f"- {geographic_description}" else: - name = HUMAN_READABLE_REGIONS[self.metadata["region"]] + imagery_name = region + elevation_description = None # determine if dataset is preview if self.metadata.get("lifecycle") == "preview": @@ -249,7 +245,11 @@ def _title(self) -> str: 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()) + return " ".join( + value + for value in [imagery_name, self.metadata["gsd"], historic_survey_number, f"({date})", preview or None] + if value is not None + ) if self.metadata["category"] in [ SATELLITE_IMAGERY, @@ -257,47 +257,49 @@ def _title(self) -> str: RURAL_AERIAL_PHOTOS, ]: return " ".join( - f"{name} {self.metadata['gsd']} {event or ''} {DATA_CATEGORIES[self.metadata['category']]} ({date}) {preview or ''}".split() # pylint: disable=line-too-long + value + for value in [ + imagery_name, + self.metadata["gsd"], + DATA_CATEGORIES[self.metadata["category"]], + f"({date})", + preview or None, + ] + if value is not None ) if self.metadata["category"] in [DEM, DSM]: return " ".join( - f"{name} {self._elevation_title_event(event) or ''} LiDAR {self.metadata['gsd']} {DATA_CATEGORIES[self.metadata['category']]} ({date}) {preview or ''}".split() # pylint: disable=line-too-long + value + for value in [ + region, + elevation_description or None, + "LiDAR", + self.metadata["gsd"], + DATA_CATEGORIES[self.metadata["category"]], + f"({date})", + preview or None, + ] + if value is not None ) raise SubtypeParameterError(self.metadata["category"]) - def _elevation_title_event(self, event: Optional[str]) -> Optional[str]: - if event: - return f"- {event}" - return None - def _description(self) -> str: """Generates the descriptions for imagery and elevation datasets. Urban Aerial Photos / Rural Aerial Photos: Orthophotography within the [Region] region captured in the [Year(s)] flying season. DEM / DSM: - [Digital Surface Model / Digital Elevation Model] within the [region] - [?- geographic_description] region in [year(s)]. - Satellite Imagery: - Satellite imagery within the [Region] region captured in [Year(s)]. - Historical Imagery: - Scanned aerial imagery within the [Region] region captured in [Year(s)]. + [Digital Surface Model / Digital Elevation Model] within the [Region] region in [year(s)]. + Satellite Imagery / Scanned Aerial Photos: + [Satellite imagery | Scanned Aerial Photos] within the [Region] region captured in [Year(s)]. Returns: Dataset Description """ - # format optional metadata - geographic_description = self.metadata.get("geographic_description") - event = self.metadata.get("event_name") - # format date for metadata - if self.metadata["start_datetime"].year == self.metadata["end_datetime"].year: - date = str(self.metadata["start_datetime"].year) + if (start_year := self.metadata["start_datetime"].year) == (end_year := self.metadata["end_datetime"].year): + date = str(start_year) else: - date = f"{self.metadata['start_datetime'].year}-{self.metadata['end_datetime'].year}" - - # format geographic_description for metadata description - if geographic_description: - geographic_description = f"- {geographic_description}" + date = f"{start_year}-{end_year}" region = HUMAN_READABLE_REGIONS[self.metadata["region"]] @@ -308,17 +310,13 @@ def _description(self) -> str: elif self.metadata["category"] in [URBAN_AERIAL_PHOTOS, RURAL_AERIAL_PHOTOS]: desc = f"Orthophotography within the {region} region captured in the {date} flying season" elif self.metadata["category"] == DEM: - desc = " ".join( - f"Digital Elevation Model within the {region} {geographic_description or ''} region in {date}".split() - ) + desc = f"Digital Elevation Model within the {region} region in {date}" elif self.metadata["category"] == DSM: - desc = " ".join( - f"Digital Surface Model within the {region} {geographic_description or ''} region in {date}".split() - ) + desc = f"Digital Surface Model within the {region} region in {date}" else: raise SubtypeParameterError(self.metadata["category"]) - if event: + if event := self.metadata.get("event_name"): desc = desc + f", published as a record of the {event} event" return desc + "." diff --git a/scripts/stac/imagery/metadata_constants.py b/scripts/stac/imagery/metadata_constants.py index 09a17806e..4a71f40e0 100644 --- a/scripts/stac/imagery/metadata_constants.py +++ b/scripts/stac/imagery/metadata_constants.py @@ -4,6 +4,8 @@ class CollectionMetadata(TypedDict): """ + Used to generate dataset collection titles and descriptions. + region: Region of Dataset gsd: Dataset Ground Sample Distance start_date: Dataset capture start date diff --git a/scripts/stac/tests/collection_test.py b/scripts/stac/tests/collection_test.py index 379e05cb9..94445348d 100644 --- a/scripts/stac/tests/collection_test.py +++ b/scripts/stac/tests/collection_test.py @@ -24,24 +24,24 @@ def setup() -> Generator[CollectionMetadata, None, None]: "start_datetime": datetime(2022, 2, 2), "end_datetime": datetime(2022, 2, 2), "lifecycle": "completed", - "event_name": "Forest assessment", + "event_name": "Forest Assessment", "historic_survey_number": None, - "geographic_description": "Auckland North", + "geographic_description": "Auckland North Forest Assessment", } yield metadata def test_title_description_id_created_on_init(metadata: CollectionMetadata) -> None: collection = ImageryCollection(metadata) - assert collection.stac["title"] == "Auckland North 0.3m Forest assessment Urban Aerial Photos (2022)" + assert collection.stac["title"] == "Auckland North Forest Assessment 0.3m Urban Aerial Photos (2022)" assert ( collection.stac["description"] - == "Orthophotography within the Auckland region captured in the 2022 flying season, published as a record of the Forest assessment event." # pylint: disable=line-too-long + == "Orthophotography within the Auckland region captured in the 2022 flying season, published as a record of the Forest Assessment event." # pylint: disable=line-too-long ) assert collection.stac["id"] assert collection.stac["linz:region"] == "auckland" - assert collection.stac["linz:geographic_description"] == "Auckland North" - assert collection.stac["linz:event_name"] == "Forest assessment" + assert collection.stac["linz:geographic_description"] == "Auckland North Forest Assessment" + assert collection.stac["linz:event_name"] == "Forest Assessment" assert collection.stac["linz:lifecycle"] == "completed" assert collection.stac["linz:geospatial_category"] == "urban-aerial-photos" @@ -179,9 +179,9 @@ def test_default_provider_is_present(metadata: CollectionMetadata) -> None: def test_event_name_is_present(metadata: CollectionMetadata) -> None: collection = ImageryCollection(metadata) - assert "Forest assessment" == collection.stac["linz:event_name"] + assert "Forest Assessment" == collection.stac["linz:event_name"] def test_geographic_description_is_present(metadata: CollectionMetadata) -> None: collection = ImageryCollection(metadata) - assert "Auckland North" == collection.stac["linz:geographic_description"] + assert "Auckland North Forest Assessment" == collection.stac["linz:geographic_description"] diff --git a/scripts/stac/tests/generate_description_test.py b/scripts/stac/tests/generate_description_test.py index 28e685e63..5e06c7f7e 100644 --- a/scripts/stac/tests/generate_description_test.py +++ b/scripts/stac/tests/generate_description_test.py @@ -57,7 +57,7 @@ def test_generate_description_elevation_geographic_description_input( metadata_auck["category"] = "dem" metadata_auck["geographic_description"] = "Central" collection = ImageryCollection(metadata_auck) - description = "Digital Elevation Model within the Auckland - Central region in 2023." + description = "Digital Elevation Model within the Auckland region in 2023." assert collection.stac["description"] == description diff --git a/scripts/stac/tests/generate_title_test.py b/scripts/stac/tests/generate_title_test.py index 4832a318c..6e1b82e07 100644 --- a/scripts/stac/tests/generate_title_test.py +++ b/scripts/stac/tests/generate_title_test.py @@ -103,27 +103,30 @@ def test_generate_title_geographic_description(metadata: Tuple[CollectionMetadat def test_generate_title_event_imagery(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: _, metadata_hb = metadata + metadata_hb["geographic_description"] = "Hawke's Bay Cyclone Gabrielle" metadata_hb["event_name"] = "Cyclone Gabrielle" collection = ImageryCollection(metadata_hb) - title = "Hawke's Bay 0.3m Cyclone Gabrielle Rural Aerial Photos (2023)" + title = "Hawke's Bay Cyclone Gabrielle 0.3m Rural Aerial Photos (2023)" assert collection.stac["title"] == title def test_generate_title_event_elevation(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: _, metadata_hb = metadata metadata_hb["category"] = "dsm" + metadata_hb["geographic_description"] = "Hawke's Bay Cyclone Gabrielle" metadata_hb["event_name"] = "Cyclone Gabrielle" collection = ImageryCollection(metadata_hb) - title = "Hawke's Bay - Cyclone Gabrielle LiDAR 0.3m DSM (2023)" + title = "Hawke's Bay - Hawke's Bay Cyclone Gabrielle LiDAR 0.3m DSM (2023)" assert collection.stac["title"] == title def test_generate_title_event_satellite_imagery(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: _, metadata_hb = metadata metadata_hb["category"] = "satellite-imagery" + metadata_hb["geographic_description"] = "Hawke's Bay Cyclone Gabrielle" metadata_hb["event_name"] = "Cyclone Gabrielle" collection = ImageryCollection(metadata_hb) - title = "Hawke's Bay 0.3m Cyclone Gabrielle Satellite Imagery (2023)" + title = "Hawke's Bay Cyclone Gabrielle 0.3m Satellite Imagery (2023)" assert collection.stac["title"] == title @@ -147,7 +150,8 @@ def test_generate_imagery_title_empty_optional_str(metadata: Tuple[CollectionMet def test_generate_imagery_title_with_event(metadata: Tuple[CollectionMetadata, CollectionMetadata]) -> None: metadata_auck, _ = metadata - metadata_auck["event_name"] = "Forest assessment" + metadata_auck["geographic_description"] = "Auckland Forest Assessment" + metadata_auck["event_name"] = "Forest Assessment" collection = ImageryCollection(metadata_auck) - title = "Auckland 0.3m Forest assessment Rural Aerial Photos (2023)" + title = "Auckland Forest Assessment 0.3m Rural Aerial Photos (2023)" assert collection.stac["title"] == title