Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into refactor/boto3-clie…
Browse files Browse the repository at this point in the history
…nt-instead-of-resource-tde-1034
  • Loading branch information
l0b0 committed Nov 24, 2024
2 parents 2095f00 + bc93aaf commit 1563df7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
24 changes: 11 additions & 13 deletions scripts/stac/imagery/create_stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ def create_item(
if not gdalinfo_result:
gdalinfo_result = gdal_info(asset_path)

geometry, bbox = get_extents(gdalinfo_result)

if derived_from is not None:
for derived in derived_from:
derived_item_content = read(derived)
Expand All @@ -130,7 +128,7 @@ def create_item(
)

item.update_datetime(start_datetime, end_datetime)
item.update_spatial(geometry, bbox)
item.update_spatial(*get_extents(gdalinfo_result))
item.add_collection(collection_id)

get_log().info("ImageryItem created", path=asset_path)
Expand All @@ -148,17 +146,8 @@ def create_base_item(asset_path: str, gdal_version: str) -> ImageryItem:
"""
id_ = get_file_name_from_path(asset_path)
file_content = fs.read(asset_path)
file_content_checksum = checksum.multihash_as_hex(file_content)
file_modified_datetime = format_rfc_3339_datetime_string(modified(asset_path))

stac_asset = STACAsset(
**{
"href": os.path.join(".", os.path.basename(asset_path)),
"file:checksum": checksum.multihash_as_hex(file_content),
"created": file_modified_datetime,
"updated": file_modified_datetime,
}
)

now_string = format_rfc_3339_datetime_string(utc_now())

if (topo_imagery_hash := os.environ.get("GIT_HASH")) is not None:
Expand All @@ -174,4 +163,13 @@ def create_base_item(asset_path: str, gdal_version: str) -> ImageryItem:
}
)

stac_asset = STACAsset(
**{
"href": os.path.join(".", os.path.basename(asset_path)),
"file:checksum": file_content_checksum,
"created": file_modified_datetime,
"updated": file_modified_datetime,
}
)

return ImageryItem(id_, now_string, stac_asset, stac_processing)
9 changes: 8 additions & 1 deletion scripts/stac/imagery/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ def add_collection(self, collection_id: str) -> None:
self.add_link(Link(path="./collection.json", rel=Relation.PARENT, media_type=StacMediaType.JSON))

def add_link(self, link: Link) -> None:
self.stac["links"].append(link.stac)
if self.stac.get("links") and link.stac["rel"] in [
Relation.COLLECTION,
Relation.PARENT,
Relation.SELF,
]: # STAC specification prescribes there can be only one of these
self.stac["links"][:] = [l for l in self.stac["links"] if l.get("rel") != link.stac["rel"]]

self.stac.setdefault("links", []).append(link.stac)
17 changes: 8 additions & 9 deletions scripts/stac/imagery/tests/collection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,19 @@ def test_add_item(fake_collection_metadata: CollectionMetadata, fake_linz_slug:
now = any_epoch_datetime()
now_string = format_rfc_3339_datetime_string(now)
collection = ImageryCollection(fake_collection_metadata, fixed_now_function(now), fake_linz_slug)
asset_created_datetime = any_epoch_datetime_string()
asset_updated_datetime = any_epoch_datetime_string()
asset_datetimes = {
"created": any_epoch_datetime_string(),
"updated": any_epoch_datetime_string(),
}
item = ImageryItem(
"BR34_5000_0304",
now_string,
STACAsset(
**{
"href": "any href",
"file:checksum": "any checksum",
"created": asset_created_datetime,
"updated": asset_updated_datetime,
"created": asset_datetimes["created"],
"updated": asset_datetimes["updated"],
}
),
any_stac_processing(),
Expand Down Expand Up @@ -163,11 +165,8 @@ def test_add_item(fake_collection_metadata: CollectionMetadata, fake_linz_slug:
with subtests.test(msg=f"item properties.{property_name}"):
assert item.stac["properties"][property_name] == now_string

with subtests.test(msg="item assets.visual.created"):
assert item.stac["assets"]["visual"]["created"] == asset_created_datetime

with subtests.test(msg="item assets.visual.updated"):
assert item.stac["assets"]["visual"]["updated"] == asset_updated_datetime
with subtests.test(msg=f"item assets.visual.{property_name}"):
assert item.stac["assets"]["visual"][property_name] == asset_datetimes[property_name]


def test_write_collection(fake_collection_metadata: CollectionMetadata, fake_linz_slug: str) -> None:
Expand Down
11 changes: 7 additions & 4 deletions scripts/stac/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ class Relation(str, Enum):
DERIVED_FROM = "derived_from"
""" https://github.com/radiantearth/stac-spec/blob/master/best-practices.md#derived-from-relation-derived_from"""

def __str__(self) -> str:
return self.value


# pylint: disable=too-few-public-methods
class Link:
"""Represents a STAC Link Object (https://github.com/radiantearth/stac-spec/blob/master/commons/links.md#link-object).
Attributes:
path: A string that represents the actual link in the format of an URL.
rel: A string that represents the relationship that the link has to the object it will be added to.
rel: `Relation` that represents the relationship that the link has to the object it will be added to.
media_type: `StacMediaType` of the link file.
file_content: Optional. The content of the file that will be used to store the checksum in `file:checksum`.
It assumes using the STAC `file` extension.
"""

stac: dict[str, str]

def __init__(self, path: str, rel: str, media_type: StacMediaType, file_content: bytes | None = None) -> None:
def __init__(self, path: str, rel: Relation, media_type: StacMediaType, file_content: bytes | None = None) -> None:
self.stac = {
"href": path,
"rel": rel,
"type": media_type,
"rel": str(rel),
"type": str(media_type),
}

if file_content:
Expand Down
3 changes: 3 additions & 0 deletions scripts/stac/util/media_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class StacMediaType(str, Enum):
For STAC Item
"""

def __str__(self) -> str:
return self.value

0 comments on commit 1563df7

Please sign in to comment.