Skip to content

Commit add5864

Browse files
committed
Support STAC-1.1-style bands from CollectionMetadata._parse_dimensions
related to eu-cdse/openeo-cdse-infra#799 (load_stac based collection configuration)
1 parent 2612c8d commit add5864

File tree

3 files changed

+149
-43
lines changed

3 files changed

+149
-43
lines changed

openeo/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.47.0a1"
1+
__version__ = "0.47.0a2"

openeo/metadata.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,22 +588,23 @@ def _parse_dimensions(cls, spec: dict, complain: Callable[[str], None] = _log.wa
588588
dimensions.append(Dimension(name=name, type=dim_type))
589589

590590
# Detailed band information: `summaries/[eo|raster]:bands` (and 0.4 style `properties/eo:bands`)
591-
eo_bands = (
592-
deep_get(spec, "summaries", "eo:bands", default=None)
591+
summaries_bands = (
592+
deep_get(spec, "summaries", "bands", default=None)
593+
or deep_get(spec, "summaries", "eo:bands", default=None)
593594
or deep_get(spec, "summaries", "raster:bands", default=None)
594595
or deep_get(spec, "properties", "eo:bands", default=None)
595596
)
596-
if eo_bands:
597-
# center_wavelength is in micrometer according to spec
597+
if summaries_bands:
598598
bands_detailed = [
599599
Band(
600600
name=b["name"],
601-
common_name=b.get("common_name"),
602-
wavelength_um=b.get("center_wavelength"),
601+
common_name=b.get("eo:common_name") or b.get("common_name"),
602+
# center_wavelength is in micrometer according to spec
603+
wavelength_um=b.get("eo:center_wavelength") or b.get("center_wavelength"),
603604
aliases=b.get("aliases"),
604605
gsd=b.get("openeo:gsd"),
605606
)
606-
for b in eo_bands
607+
for b in summaries_bands
607608
]
608609
# Update band dimension with more detailed info
609610
band_dimensions = [d for d in dimensions if d.type == "bands"]

tests/test_metadata.py

Lines changed: 140 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,28 @@ def test_get_dimensions_cube_dimensions_no_band_names():
332332
assert logs == ["No band names in dimension 'spectral'"]
333333

334334

335-
def test_get_dimensions_cube_dimensions_eo_bands():
335+
@pytest.mark.parametrize(
336+
"summaries",
337+
[
338+
{
339+
# Pre-STAC-1.1-style eo:bands
340+
"eo:bands": [
341+
{"name": "r", "common_name": "red", "center_wavelength": 5},
342+
{"name": "g", "center_wavelength": 8},
343+
{"name": "b", "common_name": "blue"},
344+
]
345+
},
346+
{
347+
# STAC 1.1-style bands
348+
"bands": [
349+
{"name": "r", "eo:common_name": "red", "eo:center_wavelength": 5},
350+
{"name": "g", "eo:center_wavelength": 8},
351+
{"name": "b", "eo:common_name": "blue"},
352+
]
353+
},
354+
],
355+
)
356+
def test_get_dimensions_cube_dimensions_eo_bands(summaries):
336357
dims = CollectionMetadata._parse_dimensions(
337358
{
338359
"cube:dimensions": {
@@ -341,13 +362,7 @@ def test_get_dimensions_cube_dimensions_eo_bands():
341362
"t": {"type": "temporal", "extent": ["2020-02-20", None]},
342363
"spectral": {"type": "bands", "values": ["r", "g", "b"]},
343364
},
344-
"summaries": {
345-
"eo:bands": [
346-
{"name": "r", "common_name": "red", "center_wavelength": 5},
347-
{"name": "g", "center_wavelength": 8},
348-
{"name": "b", "common_name": "blue"},
349-
]
350-
},
365+
"summaries": summaries,
351366
}
352367
)
353368
assert_same_dimensions(
@@ -368,21 +383,36 @@ def test_get_dimensions_cube_dimensions_eo_bands():
368383
)
369384

370385

371-
def test_get_dimensions_cube_dimensions_eo_bands_mismatch():
386+
@pytest.mark.parametrize(
387+
"summaries",
388+
[
389+
{
390+
# Pre-STAC-1.1-style eo:bands
391+
"eo:bands": [
392+
{"name": "y", "common_name": "yellow", "center_wavelength": 5},
393+
{"name": "c", "center_wavelength": 8},
394+
{"name": "m", "common_name": "magenta"},
395+
]
396+
},
397+
{
398+
# STAC 1.1-style bands
399+
"bands": [
400+
{"name": "y", "eo:common_name": "yellow", "eo:center_wavelength": 5},
401+
{"name": "c", "eo:center_wavelength": 8},
402+
{"name": "m", "eo:common_name": "magenta"},
403+
]
404+
},
405+
],
406+
)
407+
def test_get_dimensions_cube_dimensions_eo_bands_mismatch(summaries):
372408
logs = []
373409
dims = CollectionMetadata._parse_dimensions(
374410
{
375411
"cube:dimensions": {
376412
"x": {"type": "spatial", "extent": [-10, 10]},
377413
"spectral": {"type": "bands", "values": ["r", "g", "b"]},
378414
},
379-
"summaries": {
380-
"eo:bands": [
381-
{"name": "y", "common_name": "yellow", "center_wavelength": 5},
382-
{"name": "c", "center_wavelength": 8},
383-
{"name": "m", "common_name": "magenta"},
384-
]
385-
},
415+
"summaries": summaries,
386416
},
387417
complain=logs.append,
388418
)
@@ -403,7 +433,28 @@ def test_get_dimensions_cube_dimensions_eo_bands_mismatch():
403433
assert logs == ["Band name mismatch: ['r', 'g', 'b'] != ['y', 'c', 'm']"]
404434

405435

406-
def test_get_dimensions_eo_bands_only():
436+
@pytest.mark.parametrize(
437+
"summaries",
438+
[
439+
{
440+
# Pre-STAC-1.1-style eo:bands
441+
"eo:bands": [
442+
{"name": "y", "common_name": "yellow", "center_wavelength": 5},
443+
{"name": "c", "center_wavelength": 8},
444+
{"name": "m", "common_name": "magenta"},
445+
]
446+
},
447+
{
448+
# STAC 1.1-style bands
449+
"bands": [
450+
{"name": "y", "eo:common_name": "yellow", "eo:center_wavelength": 5},
451+
{"name": "c", "eo:center_wavelength": 8},
452+
{"name": "m", "eo:common_name": "magenta"},
453+
]
454+
},
455+
],
456+
)
457+
def test_get_dimensions_eo_bands_only(summaries):
407458
logs = []
408459
dims = CollectionMetadata._parse_dimensions(
409460
{
@@ -433,20 +484,35 @@ def test_get_dimensions_eo_bands_only():
433484
assert logs == ["No cube:dimensions metadata", "Assuming name 'bands' for anonymous band dimension."]
434485

435486

436-
def test_get_dimensions_no_band_dimension_with_eo_bands():
487+
@pytest.mark.parametrize(
488+
"summaries",
489+
[
490+
{
491+
# Pre-STAC-1.1-style eo:bands
492+
"eo:bands": [
493+
{"name": "y", "common_name": "yellow", "center_wavelength": 5},
494+
{"name": "c", "center_wavelength": 8},
495+
{"name": "m", "common_name": "magenta"},
496+
]
497+
},
498+
{
499+
# STAC 1.1-style bands
500+
"bands": [
501+
{"name": "y", "eo:common_name": "yellow", "eo:center_wavelength": 5},
502+
{"name": "c", "eo:center_wavelength": 8},
503+
{"name": "m", "eo:common_name": "magenta"},
504+
]
505+
},
506+
],
507+
)
508+
def test_get_dimensions_no_band_dimension_with_eo_bands(summaries):
437509
logs = []
438510
dims = CollectionMetadata._parse_dimensions(
439511
{
440512
"cube:dimensions": {
441513
"x": {"type": "spatial", "extent": [-10, 10]},
442514
},
443-
"summaries": {
444-
"eo:bands": [
445-
{"name": "y", "common_name": "yellow", "center_wavelength": 5},
446-
{"name": "c", "center_wavelength": 8},
447-
{"name": "m", "common_name": "magenta"},
448-
]
449-
},
515+
"summaries": summaries,
450516
},
451517
complain=logs.append,
452518
)
@@ -459,7 +525,24 @@ def test_get_dimensions_no_band_dimension_with_eo_bands():
459525
assert logs == ["No 'bands' dimension in 'cube:dimensions' while having 'eo:bands' or 'raster:bands'"]
460526

461527

462-
def test_get_dimensions_multiple_band_dimensions_with_eo_bands():
528+
@pytest.mark.parametrize(
529+
"summaries",
530+
[
531+
{
532+
# Pre-STAC-1.1-style eo:bands
533+
"eo:bands": [
534+
{"name": "zu", "common_name": "foo"},
535+
]
536+
},
537+
{
538+
# STAC 1.1-style bands
539+
"bands": [
540+
{"name": "zu", "eo:common_name": "foo"},
541+
]
542+
},
543+
],
544+
)
545+
def test_get_dimensions_multiple_band_dimensions_with_eo_bands(summaries):
463546
logs = []
464547
dims = CollectionMetadata._parse_dimensions(
465548
{
@@ -468,11 +551,7 @@ def test_get_dimensions_multiple_band_dimensions_with_eo_bands():
468551
"spectral": {"type": "bands", "values": ["alpha", "beta"]},
469552
"bands": {"type": "bands", "values": ["r", "g", "b"]},
470553
},
471-
"summaries": {
472-
"eo:bands": [
473-
{"name": "zu", "common_name": "foo"},
474-
]
475-
},
554+
"summaries": summaries,
476555
},
477556
complain=logs.append,
478557
)
@@ -574,18 +653,24 @@ def test_cubemetadata_bands_dimension_no_band_dimensions():
574653
@pytest.mark.parametrize(
575654
"spec",
576655
[
577-
# API 0.4 style
578656
{
657+
# API 0.4 style
579658
"properties": {
580659
"eo:bands": [{"name": "foo", "common_name": "F00", "center_wavelength": 0.543}, {"name": "bar"}]
581660
}
582661
},
583-
# API 1.0 style
584662
{
663+
# API 1.0 style
585664
"summaries": {
586665
"eo:bands": [{"name": "foo", "common_name": "F00", "center_wavelength": 0.543}, {"name": "bar"}]
587666
}
588667
},
668+
{
669+
# STAC 1.1 style
670+
"summaries": {
671+
"bands": [{"name": "foo", "eo:common_name": "F00", "eo:center_wavelength": 0.543}, {"name": "bar"}]
672+
}
673+
},
589674
],
590675
)
591676
def test_metadata_bands_dimension_eo_bands(spec):
@@ -599,8 +684,8 @@ def test_metadata_bands_dimension_eo_bands(spec):
599684
@pytest.mark.parametrize(
600685
"spec",
601686
[
602-
# API 0.4 style
603687
{
688+
# API 0.4 style
604689
"properties": {
605690
"cube:dimensions": {
606691
"x": {"type": "spatial", "axis": "x"},
@@ -609,8 +694,8 @@ def test_metadata_bands_dimension_eo_bands(spec):
609694
"eo:bands": [{"name": "foo", "common_name": "F00", "center_wavelength": 0.543}, {"name": "bar"}],
610695
}
611696
},
612-
# API 1.0 style
613697
{
698+
# API 1.0 style
614699
"cube:dimensions": {
615700
"x": {"type": "spatial", "axis": "x"},
616701
"b": {"type": "bands", "values": ["foo", "bar"]},
@@ -619,6 +704,16 @@ def test_metadata_bands_dimension_eo_bands(spec):
619704
"eo:bands": [{"name": "foo", "common_name": "F00", "center_wavelength": 0.543}, {"name": "bar"}]
620705
},
621706
},
707+
{
708+
# STAC 1.1 bands style
709+
"cube:dimensions": {
710+
"x": {"type": "spatial", "axis": "x"},
711+
"b": {"type": "bands", "values": ["foo", "bar"]},
712+
},
713+
"summaries": {
714+
"bands": [{"name": "foo", "eo:common_name": "F00", "eo:center_wavelength": 0.543}, {"name": "bar"}]
715+
},
716+
},
622717
],
623718
)
624719
def test_metadata_bands_dimension(spec):
@@ -996,6 +1091,10 @@ def filter_bbox(self, bbox):
9961091
StacDummyBuilder.collection(summaries={"eo:bands": [{"name": "B01"}, {"name": "B02"}]}),
9971092
["B01", "B02"],
9981093
),
1094+
(
1095+
StacDummyBuilder.collection(summaries={"bands": [{"name": "B01"}, {"name": "B02"}]}),
1096+
["B01", "B02"],
1097+
),
9991098
# TODO: test asset handling in collection?
10001099
(
10011100
StacDummyBuilder.catalog(),
@@ -1007,6 +1106,12 @@ def filter_bbox(self, bbox):
10071106
),
10081107
["SCL", "B08"],
10091108
),
1109+
(
1110+
StacDummyBuilder.item(
1111+
properties={"datetime": "2020-05-22T00:00:00Z", "bands": [{"name": "SCL"}, {"name": "B08"}]}
1112+
),
1113+
["SCL", "B08"],
1114+
),
10101115
# TODO: test asset handling in item?
10111116
],
10121117
)

0 commit comments

Comments
 (0)