From 99cc8588ddc3d1416c6b7c01db34ddbfc636f48d Mon Sep 17 00:00:00 2001 From: Jan Van den bosch Date: Thu, 2 May 2024 15:19:37 +0200 Subject: [PATCH] respect layercatalog bands order #460 --- openeogeotrellis/layercatalog.py | 2 +- openeogeotrellis/load_stac.py | 7 +++-- tests/layercatalog.json | 50 +------------------------------- tests/test_load_collection.py | 15 ++++++---- 4 files changed, 16 insertions(+), 58 deletions(-) diff --git a/openeogeotrellis/layercatalog.py b/openeogeotrellis/layercatalog.py index dbafff00..ef6485a7 100644 --- a/openeogeotrellis/layercatalog.py +++ b/openeogeotrellis/layercatalog.py @@ -695,7 +695,7 @@ def file_agera5_pyramid(): elif layer_source_type == 'stac': cube = load_stac(layer_source_info["url"], load_params, env, layer_properties=metadata.get("_vito", "properties", default={}), - batch_jobs=None) + batch_jobs=None, override_band_names=metadata.band_names) pyramid = cube.pyramid.levels metadata = cube.metadata elif layer_source_type == 'accumulo': diff --git a/openeogeotrellis/load_stac.py b/openeogeotrellis/load_stac.py index f6ca8783..5468c8d4 100644 --- a/openeogeotrellis/load_stac.py +++ b/openeogeotrellis/load_stac.py @@ -29,7 +29,10 @@ def load_stac(url: str, load_params: LoadParameters, env: EvalEnv, layer_properties: Dict[str, object], - batch_jobs: Optional[backend.BatchJobs]) -> GeopysparkDataCube: + batch_jobs: Optional[backend.BatchJobs], override_band_names: List[str] = None) -> GeopysparkDataCube: + if override_band_names is None: + override_band_names = [] + logger.info("load_stac from url {u!r} with load params {p!r}".format(u=url, p=load_params)) no_data_available_exception = OpenEOApiException(message="There is no data available for the given extents.", @@ -368,7 +371,7 @@ def intersects_temporally(interval) -> bool: # TODO: detect actual dimensions instead of this simple default? SpatialDimension(name="x", extent=[]), SpatialDimension(name="y", extent=[]), TemporalDimension(name='t', extent=[]), - BandDimension(name="bands", bands=[Band(band_name) for band_name in band_names]) + BandDimension(name="bands", bands=[Band(band_name) for band_name in (override_band_names or band_names)]) ]) if load_params.bands: diff --git a/tests/layercatalog.json b/tests/layercatalog.json index a42c5ae6..d77926d8 100644 --- a/tests/layercatalog.json +++ b/tests/layercatalog.json @@ -1427,20 +1427,8 @@ "bands": { "type": "bands", "values": [ - "SOSD", - "EOSD", - "MAXD", - "SOSV", - "EOSV", - "MINV", - "MAXV", - "AMPL", - "LENGTH", - "LSLOPE", - "RSLOPE", "SPROD", - "TPROD", - "QFLAG" + "TPROD" ] } }, @@ -1454,47 +1442,11 @@ }, "summaries": { "eo:bands": [ - { - "name": "SOSD" - }, - { - "name": "EOSD" - }, - { - "name": "MAXD" - }, - { - "name": "SOSV" - }, - { - "name": "EOSV" - }, - { - "name": "MINV" - }, - { - "name": "MAXV" - }, - { - "name": "AMPL" - }, - { - "name": "LENGTH" - }, - { - "name": "LSLOPE" - }, - { - "name": "RSLOPE" - }, { "name": "SPROD" }, { "name": "TPROD" - }, - { - "name": "QFLAG" } ] } diff --git a/tests/test_load_collection.py b/tests/test_load_collection.py index 2736e5b8..8ef30252 100644 --- a/tests/test_load_collection.py +++ b/tests/test_load_collection.py @@ -477,7 +477,11 @@ def test_data_cube_params(catalog): assert "Average" == str(cube_params.resampleMethod()) -def test_load_stac_collection_with_property_filters(catalog, tmp_path, requests_mock): +@pytest.mark.parametrize(["bands", "expected_bands"], [ + ([], ["SPROD", "TPROD"]), # ordered as specified in layercatalog.json + (["TPROD", "SPROD"], ["TPROD", "SPROD"]) # override order +]) +def test_load_stac_collection_with_property_filters(catalog, tmp_path, requests_mock, bands, expected_bands): requests_mock.get("https://stac.openeo.vito.be/", text=get_test_data_file("stac/issue640-api-layer-property-filter/stac.openeo.vito.be.json").read_text()) requests_mock.get("https://stac.openeo.vito.be/search", [ {'text': get_test_data_file("stac/issue640-api-layer-property-filter/copernicus_r_utm-wgs84_10_m_hrvpp-vpp_p_2017-now_v01_features.json") @@ -492,7 +496,7 @@ def test_load_stac_collection_with_property_filters(catalog, tmp_path, requests_ load_params = LoadParameters(spatial_extent={"west": 5.00, "south": 51.20, "east": 5.01, "north": 51.21}, temporal_extent=["2017-07-01T00:00Z", "2018-07-31T00:00Z"], - bands=["SPROD", "TPROD"]) # TODO: remove other bands from layercatalog.json, then drop this bands argument + bands=bands) env = EvalEnv({'pyramid_levels': 'highest', 'user': None}) @@ -503,7 +507,6 @@ def test_load_stac_collection_with_property_filters(catalog, tmp_path, requests_ data_cube.save_result(output_file, format="GTiff") with rasterio.open(output_file) as ds: - expected_band_count = 2 - assert ds.count == expected_band_count - assert ds.tags(1)["DESCRIPTION"] == "SPROD" - assert ds.tags(2)["DESCRIPTION"] == "TPROD" + assert ds.count == len(expected_bands) + for band_index, band_name in enumerate(expected_bands): + assert ds.tags(band_index + 1)["DESCRIPTION"] == expected_bands[band_index]