Skip to content

Commit bf2df5c

Browse files
committed
Issue #298 finetune raster:bands handling a bit
1 parent 28d4a11 commit bf2df5c

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

openeo_driver/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class STAC_EXTENSION:
99
MLMODEL = "https://stac-extensions.github.io/ml-model/v1.0.0/schema.json"
1010
CARD4LOPTICAL = "https://stac-extensions.github.io/card4l/v0.1.0/optical/schema.json"
1111
CARD4LSAR = "https://stac-extensions.github.io/card4l/v0.1.0/sar/schema.json"
12+
RASTER_V110 = "https://stac-extensions.github.io/raster/v1.1.0/schema.json"
13+
RASTER_V200 = "https://stac-extensions.github.io/raster/v2.0.0/schema.json"
1214

1315

1416
class JOB_STATUS:

openeo_driver/save_result.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ def __init__(
306306
warnings.warn("AggregatePolygonResult: GeometryCollection or DriverVectorCube expected but got {t}".format(t=type(regions)))
307307
self._regions = regions
308308
self._metadata = metadata
309+
# TODO #298 this "raster:bands" helper is old-style
310+
# and just used for "statistics" which moved to the common metadata in v2
309311
self.raster_bands = None
310312

311313
def get_data(self):
@@ -662,7 +664,6 @@ class AggregatePolygonResultCSV(AggregatePolygonResult):
662664
def __init__(self, csv_dir, regions: Union[GeometryCollection, DriverVectorCube, DelayedVector, BaseGeometry], metadata: CollectionMetadata = None):
663665
super().__init__(timeseries=None, regions=regions, metadata=metadata)
664666
self._csv_dir = csv_dir
665-
self.raster_bands = None
666667

667668
def get_data(self):
668669
if self.data is None:
@@ -717,6 +718,8 @@ def stats(band):
717718
stats["stddev"] = series.std()
718719
stats["valid_percent"] = ((100.0 * len(series.dropna()) / len(series)) if len(series) else None)
719720
return {"statistics": stats}
721+
722+
# TODO #298 `raster:bands>statistics` has moved to common STAC in raster extension 2.0.0
720723
self.raster_bands = [stats(b) for b in bands]
721724

722725
if self.is_format('covjson', 'coveragejson'):

openeo_driver/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,7 @@ def _asset_object(job_id, user_id, filename: str, asset_metadata: dict, job_info
15171517
),
15181518
"type": asset_metadata.get("type", asset_metadata.get("media_type", "application/octet-stream")),
15191519
"roles": asset_metadata.get("roles", ["data"]),
1520+
# TODO: eliminate this legacy "raster:bands" construct at some point?
15201521
"raster:bands": asset_metadata.get("raster:bands"),
15211522
"file:size": asset_metadata.get("file:size"),
15221523
"alternate": asset_metadata.get("alternate"),

tests/test_save_result.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import pytest
99
from shapely.geometry import GeometryCollection, Polygon
10+
import dirty_equals
1011

1112
from openeo.metadata import CollectionMetadata
1213
from openeo_driver.datacube import DriverVectorCube
@@ -181,8 +182,6 @@ def test_aggregate_polygon_result_inconsistent_bands():
181182

182183

183184
def test_aggregate_polygon_result_CSV(tmp_path):
184-
185-
186185
metadata = CollectionMetadata({
187186
"cube:dimensions": {
188187
"x": {"type": "spatial"},
@@ -196,28 +195,66 @@ def test_aggregate_polygon_result_CSV(tmp_path):
196195
Polygon([(6, 1), (1, 7), (9, 9)])
197196
])
198197

199-
200198
result = AggregatePolygonResultCSV(csv_dir=Path(__file__).parent / "data" /"aggregate_spatial_spacetime_cube", regions=regions_with_nonexistant, metadata=metadata)
201199
result.set_format("json")
202200

203201
assets = result.write_assets(tmp_path / "ignored")
204-
theAsset = assets.popitem()[1]
205-
filename = theAsset['href']
202+
[(_, asset_metadata)] = assets.items()
206203

207-
assert 'application/json' == theAsset['type']
208-
assert ["red", "green", "blue"] == [b['name'] for b in theAsset['bands']]
209-
assert 'raster:bands' in theAsset
210-
assert 'file:size' in theAsset
204+
assert "application/json" == asset_metadata["type"]
205+
assert asset_metadata["bands"] == [
206+
dirty_equals.IsPartialDict(name="red"),
207+
dirty_equals.IsPartialDict(name="green"),
208+
dirty_equals.IsPartialDict(name="blue"),
209+
]
210+
assert asset_metadata["raster:bands"] == [
211+
{
212+
"statistics": {
213+
"minimum": pytest.approx(4646, rel=0.1),
214+
"mean": pytest.approx(4646, rel=0.1),
215+
"maximum": pytest.approx(4646, rel=0.1),
216+
"stddev": pytest.approx(0.31, rel=0.1),
217+
"valid_percent": 100.0,
218+
}
219+
},
220+
{
221+
"statistics": {
222+
"minimum": pytest.approx(4865, rel=0.1),
223+
"mean": pytest.approx(4865, rel=0.1),
224+
"maximum": pytest.approx(4865, rel=0.1),
225+
"stddev": pytest.approx(0.265, rel=0.1),
226+
"valid_percent": 100.0,
227+
}
228+
},
229+
{
230+
"statistics": {
231+
"minimum": pytest.approx(5178, rel=0.1),
232+
"mean": pytest.approx(5178, rel=0.1),
233+
"maximum": pytest.approx(5178, rel=0.1),
234+
"stddev": pytest.approx(0.41, rel=0.1),
235+
"valid_percent": 100.0,
236+
}
237+
},
238+
]
211239

212-
assert 'mean' in theAsset['raster:bands'][0]["statistics"]
213-
assert 'minimum' in theAsset['raster:bands'][0]["statistics"]
214-
assert 100.0 == theAsset['raster:bands'][0]["statistics"]['valid_percent']
240+
assert "file:size" in asset_metadata
215241

216-
expected = {'2017-09-05T00:00:00Z': [[4646.262612301313, 4865.926572218383, 5178.517363510712], [None, None, None], [4645.719597475695, 4865.467252259935, 5177.803342998465]], '2017-09-06T00:00:00Z': [[None, None, None], [None, None, None], [4645.719597475695, 4865.467252259935, 5177.803342998465]]}
242+
filename = asset_metadata["href"]
243+
expected = {
244+
"2017-09-05T00:00:00Z": [
245+
[4646.262612301313, 4865.926572218383, 5178.517363510712],
246+
[None, None, None],
247+
[4645.719597475695, 4865.467252259935, 5177.803342998465],
248+
],
249+
"2017-09-06T00:00:00Z": [
250+
[None, None, None],
251+
[None, None, None],
252+
[4645.719597475695, 4865.467252259935, 5177.803342998465],
253+
],
254+
}
217255
with open(filename) as f:
218-
219256
timeseries_ds = json.load(f)
220-
assert expected == timeseries_ds
257+
assert timeseries_ds == expected
221258

222259
class TestAggregatePolygonSpatialResult:
223260

0 commit comments

Comments
 (0)