Skip to content

Commit

Permalink
refactor: title & description TDE-955 (#835)
Browse files Browse the repository at this point in the history
* refactor: title & description

* refactor: elevation title

* fix: improve formatting
  • Loading branch information
MDavidson17 authored Feb 8, 2024
1 parent 6979470 commit 94051b8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 61 deletions.
92 changes: 45 additions & 47 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -249,55 +245,61 @@ 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,
URBAN_AERIAL_PHOTOS,
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"]]

Expand All @@ -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 + "."
2 changes: 2 additions & 0 deletions scripts/stac/imagery/metadata_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions scripts/stac/tests/collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion scripts/stac/tests/generate_description_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
14 changes: 9 additions & 5 deletions scripts/stac/tests/generate_title_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

0 comments on commit 94051b8

Please sign in to comment.