Skip to content

Commit

Permalink
Preserve accessories (#170)
Browse files Browse the repository at this point in the history
Expose non-data assets on a parsed item as `.accessories`

---------

Co-authored-by: Ariana Barzinpour <[email protected]>
  • Loading branch information
Ariana-B and Ariana Barzinpour authored Jan 30, 2025
1 parent b46d41c commit 8b2e5a0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions odc/stac/_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,13 @@ def _get_grid(grid_name: str, asset: pystac.asset.Asset) -> GeoBox:
_grids[grid_name] = grid
return grid

band_names = []
for bk, meta in template.meta.bands.items():
asset_name, band_idx = bk
asset = _assets.get(asset_name)
if asset is None:
continue
band_names.append(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 +713,10 @@ def _get_grid(grid_name: str, asset: pystac.asset.Asset) -> GeoBox:
driver_data=driver_data,
)

# the assets that aren't bands are accessories
acc_names = set(_assets.keys()).difference(set(band_names))
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
11 changes: 10 additions & 1 deletion 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 Expand Up @@ -395,7 +400,11 @@ def strip(self) -> "ParsedItem":
"""
Copy of self but with stripped bands.
"""
return replace(self, bands={k: band.strip() for k, band in self.bands.items()})
return replace(
self,
bands={k: band.strip() for k, band in self.bands.items()},
accessories={},
)

def assets(self) -> Dict[str, List[RasterSource]]:
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/test_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ 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")
assert xx.strip().accessories == {}


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

0 comments on commit 8b2e5a0

Please sign in to comment.