Skip to content

Commit

Permalink
fix: canonical band names are now asset keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski authored and Kirill888 committed Jun 27, 2022
1 parent 064c5f0 commit 12155c7
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 29 deletions.
48 changes: 28 additions & 20 deletions odc/stac/_mdtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,36 +333,44 @@ def alias_map_from_eo(item: pystac.item.Item, quiet: bool = False) -> Dict[str,
"""
Generate mapping ``common name -> canonical name``.
For all unique common names defined on the Item eo extension record mapping to the canonical
name. Non-unique common names are ignored with a warning unless ``quiet`` flag is set.
For all unique common names defined on the Item's assets via the eo
extension, record a mapping to the asset key ("canonical name"). Non-unique
common names are ignored with a warning unless ``quiet`` flag is set.
:param item: STAC :class:`~pystac.item.Item` to process
:param quiet: Do not print warning if duplicate common names are found, defaults to False
:return: common name to canonical name mapping
"""
try:
bands = EOExtension.ext(item, add_if_missing=False).bands
except pystac.errors.ExtensionNotImplemented:
return {}

if bands is None:
return {} # pragma: no cover

common_names: Dict[str, Set[str]] = {}
for band in bands:
common_name = band.common_name
if common_name is not None:
common_names.setdefault(common_name, set()).add(band.name)

def _aliases(common_names):
for alias, bands in common_names.items():
aliases: Dict[str, Set[str]] = {}
for key, asset in item.assets.items():
try:
eo = EOExtension.ext(asset)
except pystac.errors.ExtensionNotImplemented:
return {}
if eo.bands is None:
continue
if len(eo.bands) != 1:
if not quiet:
warn(
f"Aliases are not supported for multi-band assets, skipping `{key}`"
)
continue
name = eo.bands[0].name
if name is not None and name != key:
aliases.setdefault(name, set()).add(key)
common_name = eo.bands[0].common_name
if common_name is not None and common_name != key:
aliases.setdefault(common_name, set()).add(key)

def _aliases(aliases):
for alias, bands in aliases.items():
if len(bands) == 1:
(band,) = bands
yield (alias, band)
elif not quiet:
warn(f"Common name `{alias}` is repeated, skipping")
warn(f"Alias `{alias}` is repeated, skipping")

return dict(_aliases(common_names))
return dict(_aliases(aliases))


def normalise_product_name(name: str) -> str:
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
SENTINEL_ODC: str = "S2A_28QCH_20200714_0_L2A.odc-metadata.json"
USGS_LANDSAT_STAC_v1b: str = "LC08_L2SR_081119_20200101_20200823_02_T2.json"
USGS_LANDSAT_STAC_v1: str = "LC08_L2SP_028030_20200114_20200824_02_T1_SR.json"
USGS_LANDSAT_STAC_v1_1_1: str = "LE07_L2SP_044033_20210329_20210424_02_T1_SR.json"
LIDAR_STAC: str = "lidar_dem.json"
BENCH_SITE1: str = "site1-20200606-tall-strip-africa.geojson"
BENCH_SITE2: str = "site2-2020_jun_jul-35MNM.geojson"
Expand Down Expand Up @@ -66,6 +67,13 @@ def usgs_landsat_stac_v1b():
)


@pytest.fixture
def usgs_landsat_stac_v1_1_1():
return pystac.item.Item.from_file(
str(TEST_DATA_FOLDER.joinpath(USGS_LANDSAT_STAC_v1_1_1))
)


@pytest.fixture
def ga_landsat_stac():
return pystac.item.Item.from_file(str(TEST_DATA_FOLDER.joinpath(GA_LANDSAT_STAC)))
Expand Down
Loading

0 comments on commit 12155c7

Please sign in to comment.