Skip to content

Commit

Permalink
feat: allow forcing Collection title to not have suffix TDE-1289 (#1120)
Browse files Browse the repository at this point in the history
### Motivation

Some datasets might not required a suffix in their title. For example, a
dataset with an `ongoing` lifecycle could not be tagged as a ` - Draft`.

### Modifications

- add an optional parameter `--add-title-suffix` (defaulted `True`) to
the `collection_from_items.py` script so the title suffix, for example `
- Draft` can be ommitted in any case

### Verification

automatic tests
  • Loading branch information
paulfouquet authored Oct 10, 2024
1 parent 8167cce commit 7ca3c43
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
8 changes: 8 additions & 0 deletions scripts/collection_from_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def parse_args(args: List[str] | None) -> Namespace:
help="Add a capture-dates.geojson.gz file to the collection assets",
required=False,
)
parser.add_argument(
"--add-title-suffix",
dest="add_title_suffix",
action="store_true",
help="Add a title suffix to the collection title based on the lifecycle. For example, '[TITLE] - Preview'",
required=False,
)

return parser.parse_args(args)

Expand Down Expand Up @@ -170,6 +177,7 @@ def main(args: List[str] | None = None) -> None:
item_polygons=polygons,
add_capture_dates=arguments.capture_dates,
uri=uri,
add_title_suffix=arguments.add_title_suffix,
)

destination = os.path.join(uri, "collection.json")
Expand Down
33 changes: 21 additions & 12 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
now: Callable[[], datetime],
collection_id: str | None = None,
providers: list[Provider] | None = None,
add_title_suffix: bool = True,
) -> None:
if not collection_id:
collection_id = str(ulid.ULID())
Expand All @@ -55,7 +56,7 @@ def __init__(
"type": "Collection",
"stac_version": STAC_VERSION,
"id": collection_id,
"title": self._title(),
"title": self._title(add_title_suffix),
"description": self._description(),
"license": "CC-BY-4.0",
"links": [{"rel": "self", "href": "./collection.json", "type": "application/json"}],
Expand Down Expand Up @@ -264,12 +265,20 @@ def write_to(self, destination: str) -> None:
"""
write(destination, dict_to_json_bytes(self.stac), content_type=ContentType.JSON.value)

def _title(self) -> str:
def _title(self, add_suffix: bool = True) -> str:
"""Generates the title for imagery and elevation datasets.
Satellite Imagery / Urban Aerial Photos / Rural Aerial Photos / Scanned Aerial Photos:
/~https://github.com/linz/imagery/blob/master/docs/naming.md
DEM / DSM:
/~https://github.com/linz/elevation/blob/master/docs/naming.md
Args:
add_suffix: Weither to add a suffix based on the lifecycle. For example, " - Preview". Defaults to True.
Raises:
MissingMetadataError: if required metadata is missing
SubtypeParameterError: if category is not recognised
Returns:
Dataset Title
"""
Expand All @@ -292,13 +301,13 @@ def _title(self) -> str:
imagery_name = region
elevation_description = None

# determine if the dataset title requires a lifecycle tag
if self.metadata.get("lifecycle") == "preview":
lifecycle_tag = "- Preview"
elif self.metadata.get("lifecycle") == "ongoing":
lifecycle_tag = "- Draft"
else:
lifecycle_tag = None
# determine if the dataset title requires a suffix based on its lifecycle
lifecycle_suffix = None
if add_suffix:
if self.metadata.get("lifecycle") == "preview":
lifecycle_suffix = "- Preview"
elif self.metadata.get("lifecycle") == "ongoing":
lifecycle_suffix = "- Draft"

if self.metadata["category"] == SCANNED_AERIAL_PHOTOS:
if not historic_survey_number:
Expand All @@ -310,7 +319,7 @@ def _title(self) -> str:
f"{self.metadata['gsd']}{GSD_UNIT}",
historic_survey_number,
f"({date})",
lifecycle_tag,
lifecycle_suffix,
]
if value is not None
)
Expand All @@ -327,7 +336,7 @@ def _title(self) -> str:
f"{self.metadata['gsd']}{GSD_UNIT}",
DATA_CATEGORIES[self.metadata["category"]],
f"({date})",
lifecycle_tag,
lifecycle_suffix,
]
if value is not None
)
Expand All @@ -341,7 +350,7 @@ def _title(self) -> str:
f"{self.metadata['gsd']}{GSD_UNIT}",
DATA_CATEGORIES[self.metadata["category"]],
f"({date})",
lifecycle_tag,
lifecycle_suffix,
]
if value is not None
)
Expand Down
4 changes: 4 additions & 0 deletions scripts/stac/imagery/create_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from scripts.stac.util.media_type import StacMediaType


# pylint: disable=too-many-arguments
def create_collection(
collection_id: str,
collection_metadata: CollectionMetadata,
Expand All @@ -27,6 +28,7 @@ def create_collection(
item_polygons: list[BaseGeometry],
add_capture_dates: bool,
uri: str,
add_title_suffix: bool = False,
) -> ImageryCollection:
"""Create an ImageryCollection object.
If `item_polygons` is not empty, it will add a generated capture area to the collection.
Expand All @@ -40,6 +42,7 @@ def create_collection(
item_polygons: polygons of the items linked to the collection
add_capture_dates: whether to add a capture-dates.geojson.gz file to the collection assets
uri: path of the dataset
add_title_suffix: whether to add a title suffix to the collection title based on the lifecycle
Returns:
an ImageryCollection object
Expand All @@ -55,6 +58,7 @@ def create_collection(
now=utc_now,
collection_id=collection_id,
providers=providers,
add_title_suffix=add_title_suffix,
)

for item in stac_items:
Expand Down
8 changes: 8 additions & 0 deletions scripts/stac/imagery/tests/generate_title_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def test_generate_imagery_title_draft(fake_collection_metadata: CollectionMetada
assert collection.stac["title"] == title


def test_generate_imagery_title_without_suffix(fake_collection_metadata: CollectionMetadata) -> None:
# `ongoing` lifecycle nominal case adds a suffix
fake_collection_metadata["lifecycle"] = "ongoing"
collection = ImageryCollection(metadata=fake_collection_metadata, now=any_epoch_datetime, add_title_suffix=False)
title = "Hawke's Bay 0.3m Rural Aerial Photos (2023)"
assert collection.stac["title"] == title


def test_generate_imagery_title_empty_optional_str(fake_collection_metadata: CollectionMetadata) -> None:
fake_collection_metadata["geographic_description"] = ""
fake_collection_metadata["event_name"] = ""
Expand Down

0 comments on commit 7ca3c43

Please sign in to comment.