Skip to content

Commit

Permalink
preserve accessory assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariana Barzinpour committed Sep 3, 2024
1 parent e7aa2e4 commit 8a53d07
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
12 changes: 12 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,13 @@ 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 +731,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
3 changes: 3 additions & 0 deletions odc/stac/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,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, ...]:
"""
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

0 comments on commit 8a53d07

Please sign in to comment.