diff --git a/libs/unity-py/CHANGELOG.md b/libs/unity-py/CHANGELOG.md index 9376c28e..42d852f8 100644 --- a/libs/unity-py/CHANGELOG.md +++ b/libs/unity-py/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.11.0] - 2025-05-19 + +### Added +* When creating STAC catalogs using the Collection resource, if an asset is a file local to the catalog file, then the file:size and file:checksum values will be added to the asset's STAC entry. + ## [0.10.1] - 2025-03-04 ### Added diff --git a/libs/unity-py/pyproject.toml b/libs/unity-py/pyproject.toml index be96f0df..4e16acd1 100644 --- a/libs/unity-py/pyproject.toml +++ b/libs/unity-py/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "unity-sds-client" -version = "0.10.1" +version = "0.11.0" description = "Unity-Py is a Python client to simplify interactions with NASA's Unity Platform." authors = ["Anil Natha, Mike Gangl"] diff --git a/libs/unity-py/unity_sds_client/resources/collection.py b/libs/unity-py/unity_sds_client/resources/collection.py index bcf03836..bb4cb2cb 100644 --- a/libs/unity-py/unity_sds_client/resources/collection.py +++ b/libs/unity-py/unity_sds_client/resources/collection.py @@ -2,7 +2,9 @@ from unity_sds_client.resources.dataset import Dataset from unity_sds_client.resources.data_file import DataFile from pystac import Catalog, get_stac_version, ItemCollection, Item, Asset +from pystac.extensions.file import FileExtension from pystac.errors import STACTypeError +import hashlib import json import os from datetime import datetime @@ -132,15 +134,33 @@ def to_stac(collection, data_dir): if key.startswith("./"): key = os.path.basename(key) - item.add_asset( - key = key, - asset = Asset( - href = item_location, - title = "{} file".format(df.type), - description = "", - roles = df.roles + asset = Asset( + href = item_location, + title = "{} file".format(df.type), + description = "", + roles = df.roles + ) + + # If the file exists locally on disk then add optional STAC + # attribuites: file:size, file:checksum + data_filename = os.path.join(data_dir, item_location) + if os.path.exists(data_filename): + # Get file size + file_stats = os.stat(data_filename) + + # Compute MD5SUM + md5 = hashlib.md5() + with open(data_filename, "rb") as f: + while chunk := f.read(4096): + md5.update(chunk) + + file_ext = FileExtension.ext(asset) + file_ext.apply( + size=file_stats.st_size, + checksum=md5.hexdigest() ) - ) + + item.add_asset(key=key, asset=asset) from pystac.layout import TemplateLayoutStrategy write_dir = data_dir