Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve accessories #170

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions odc/stac/_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ def parse_item(
band2grid = template.band2grid
has_proj = False if template.has_proj is False else has_proj_ext(item)
_assets = item.assets
_acc_names = list(_assets.keys())

_grids: Dict[str, GeoBox] = {}
bands: Dict[BandKey, RasterSource] = {}
Expand All @@ -681,6 +682,9 @@ def _get_grid(grid_name: str, asset: pystac.asset.Asset) -> GeoBox:
asset = _assets.get(asset_name)
if asset is None:
continue
# the assets that aren't bands should be accessories
if asset_name in _acc_names:
_acc_names.remove(asset_name)

grid_name = band2grid.get(asset_name, "default")
geobox: Optional[GeoBox] = _get_grid(grid_name, asset) if has_proj else None
Expand Down Expand Up @@ -711,6 +715,8 @@ def _get_grid(grid_name: str, asset: pystac.asset.Asset) -> GeoBox:
driver_data=driver_data,
)

accessories = {name: {"path": _assets[name].href} for name in _acc_names}

md = item.common_metadata
return ParsedItem(
item.id,
Expand All @@ -720,6 +726,7 @@ def _get_grid(grid_name: str, asset: pystac.asset.Asset) -> GeoBox:
datetime=item.datetime,
datetime_range=(md.start_datetime, md.end_datetime),
href=item.get_self_href(),
accessories=accessories,
)


Expand Down
1 change: 1 addition & 0 deletions odc/stac/eo3/_eo3converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def _to_dataset(
"properties": dicttoolz.keymap(
lambda k: STAC_TO_EO3_RENAMES.get(k, k), properties
),
"accessories": item.accessories,
"lineage": {},
}

Expand Down
5 changes: 5 additions & 0 deletions odc/stac/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class ParsedItem(Mapping[BandIdentifier, RasterSource]):
Only includes raster bands of interest.
"""

# pylint: disable=too-many-instance-attributes

id: str
"""Item id copied from STAC."""

Expand All @@ -227,6 +229,9 @@ class ParsedItem(Mapping[BandIdentifier, RasterSource]):
href: Optional[str] = None
"""Self link from stac item."""

accessories: dict[str, Any] = field(default_factory=dict)
"""Additional assets"""

def geoboxes(self, bands: BandQuery = None) -> Tuple[GeoBox, ...]:
"""
Unique ``GeoBox`` s, highest resolution first.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,19 @@ def test_parse_item_no_proj(sentinel_stac_ms: pystac.item.Item):
assert _auto_load_params([xx] * 3) is None


def test_accessories_preserved(ga_landsat_stac: pystac.item.Item):
item0 = ga_landsat_stac
item = pystac.Item.from_dict(item0.to_dict())

md = extract_collection_metadata(item, STAC_CFG)

xx = parse_item(item, md)
assert len(xx.accessories) == 3
assert xx.accessories.get("thumbnail:nbart")
assert xx.accessories.get("checksum:sha1")
assert xx.accessories.get("metadata:processor")


@pytest.fixture
def parsed_item_s2(sentinel_stac_ms: pystac.item.Item):
(item,) = parse_items([sentinel_stac_ms], STAC_CFG)
Expand Down
Loading