Skip to content

Commit 7409ad1

Browse files
committed
saveStitchedTileGridTemporal/saveSamples: handle items instead of files #1111
1 parent f69d5f1 commit 7409ad1

File tree

3 files changed

+167
-6
lines changed

3 files changed

+167
-6
lines changed

openeogeotrellis/deploy/batch_job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def run_job(
271271
access_token: str = None,
272272
):
273273
result_metadata = {}
274+
items = []
274275

275276
try:
276277
# We actually expect type Path, but in reality paths as strings tend to
@@ -368,7 +369,6 @@ def run_job(
368369
)
369370
# perform a first metadata write _before_ actually computing the result. This provides a bit more info, even if the job fails.
370371
write_metadata({**result_metadata, **_get_tracker_metadata("")}, metadata_file)
371-
items = {}
372372

373373
for result in results:
374374
result.options["batch_mode"] = True

openeogeotrellis/geopysparkdatacube.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,11 +2016,14 @@ def add_gdalinfo_objects(assets_original):
20162016
compression = get_jvm().geotrellis.raster.io.geotiff.compression.DeflateCompression(
20172017
zlevel)
20182018

2019-
band_indices_per_file = None
20202019
if tile_grid:
2021-
timestamped_paths = (get_jvm()
2022-
.org.openeo.geotrellis.geotiff.package.saveStitchedTileGridTemporal(
2023-
max_level_rdd, save_directory, tile_grid, compression, filename_prefix))
2020+
java_items = get_jvm().org.openeo.geotrellis.geotiff.package.saveStitchedTileGridTemporal(
2021+
max_level_rdd,
2022+
save_directory,
2023+
tile_grid,
2024+
compression,
2025+
filename_prefix,
2026+
)
20242027
elif sample_by_feature:
20252028
if separate_asset_per_band:
20262029
raise OpenEOApiException(
@@ -2033,7 +2036,7 @@ def add_gdalinfo_objects(assets_original):
20332036
geometries = GeometryCollection(geometries.geoms)
20342037
projected_polygons = to_projected_polygons(get_jvm(), geometries)
20352038
labels = self.get_labels(geometries,feature_id_property)
2036-
timestamped_paths = get_jvm().org.openeo.geotrellis.geotiff.package.saveSamples(
2039+
java_items = get_jvm().org.openeo.geotrellis.geotiff.package.saveSamples(
20372040
max_level_rdd, save_directory, projected_polygons, labels, compression,
20382041
filename_prefix)
20392042
else:

tests/test_batch_result.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,165 @@ def test_unified_asset_keys(tmp_path, separate_asset_per_band, expected_tiff_fil
26802680
print(f"items={json.dumps(items, indent=2)}")
26812681

26822682
assert len(items) == 2
2683+
assert len({item["id"] for item in items}) == 2
26832684
assert {item["properties"]["datetime"] for item in items} == {"2025-04-05T00:00:00Z", "2025-04-15T00:00:00Z"}
26842685

26852686
for item in items:
26862687
assert set(item["assets"].keys()) == expected_asset_keys
2688+
2689+
2690+
def test_unified_asset_keys_tile_grid(tmp_path):
2691+
process_graph = {
2692+
"load2": {
2693+
"process_id": "load_collection",
2694+
"arguments": {
2695+
"bands": [
2696+
"Flat:0",
2697+
"Flat:1",
2698+
"Flat:2",
2699+
],
2700+
"id": "TestCollection-LonLat16x16",
2701+
"spatial_extent": {
2702+
"west": 0,
2703+
"south": 50,
2704+
"east": 2,
2705+
"north": 51,
2706+
},
2707+
"temporal_extent": ["2025-04-01", "2025-04-21"],
2708+
},
2709+
},
2710+
"save1": {
2711+
"process_id": "save_result",
2712+
"arguments": {
2713+
"data": {"from_node": "load2"},
2714+
"format": "GTIFF",
2715+
"options": {"tile_grid": "wgs84-1degree"},
2716+
},
2717+
"result": True,
2718+
},
2719+
}
2720+
2721+
process = {
2722+
"process_graph": process_graph,
2723+
}
2724+
2725+
job_dir = tmp_path
2726+
metadata_file = job_dir / "job_metadata.json"
2727+
2728+
run_job(
2729+
process,
2730+
output_file=job_dir / "out",
2731+
metadata_file=metadata_file,
2732+
api_version="2.0.0",
2733+
job_dir=job_dir,
2734+
dependencies=[],
2735+
)
2736+
2737+
tiff_files = {file for file in os.listdir(job_dir) if file.endswith(".tif")}
2738+
assert tiff_files == {
2739+
"openEO_2025-04-05Z_N50E000.tif",
2740+
"openEO_2025-04-05Z_N50E001.tif",
2741+
"openEO_2025-04-15Z_N50E000.tif",
2742+
"openEO_2025-04-15Z_N50E001.tif",
2743+
}
2744+
2745+
with open(metadata_file) as f:
2746+
items = json.load(f)["items"]
2747+
2748+
print(f"items={json.dumps(items, indent=2)}")
2749+
2750+
assert len(items) == 4
2751+
assert len({item["id"] for item in items}) == 4
2752+
assert {item["properties"]["datetime"] for item in items} == {"2025-04-05T00:00:00Z", "2025-04-15T00:00:00Z"}
2753+
2754+
for item in items:
2755+
assert set(item["assets"].keys()) == {"openEO"}
2756+
2757+
2758+
def test_unified_asset_keys_sample_by_feature(tmp_path):
2759+
process_graph = {
2760+
"load2": {
2761+
"process_id": "load_collection",
2762+
"arguments": {
2763+
"bands": [
2764+
"Flat:0",
2765+
"Flat:1",
2766+
"Flat:2",
2767+
],
2768+
"id": "TestCollection-LonLat16x16",
2769+
"temporal_extent": ["2025-04-01", "2025-04-21"],
2770+
},
2771+
},
2772+
"filterspatial1": {
2773+
"process_id": "filter_spatial",
2774+
"arguments": {
2775+
"data": {"from_node": "load2"},
2776+
"geometries": {
2777+
"type": "FeatureCollection",
2778+
"features": [
2779+
{
2780+
"type": "Feature",
2781+
"properties": {},
2782+
"geometry": {
2783+
"type": "Polygon",
2784+
"coordinates": [[[0, 50], [0, 51], [1, 51], [1, 50], [0, 50]]],
2785+
},
2786+
},
2787+
{
2788+
"type": "Feature",
2789+
"properties": {},
2790+
"geometry": {
2791+
"type": "Polygon",
2792+
"coordinates": [[[1, 50], [1, 51], [2, 51], [2, 50], [1, 50]]],
2793+
},
2794+
},
2795+
],
2796+
},
2797+
},
2798+
},
2799+
"save1": {
2800+
"process_id": "save_result",
2801+
"arguments": {
2802+
"data": {"from_node": "filterspatial1"},
2803+
"format": "GTIFF",
2804+
"options": {"sample_by_feature": True},
2805+
},
2806+
"result": True,
2807+
},
2808+
}
2809+
2810+
process = {
2811+
"process_graph": process_graph,
2812+
}
2813+
2814+
job_dir = tmp_path
2815+
metadata_file = job_dir / "job_metadata.json"
2816+
2817+
run_job(
2818+
process,
2819+
output_file=job_dir / "out",
2820+
metadata_file=metadata_file,
2821+
api_version="2.0.0",
2822+
job_dir=job_dir,
2823+
dependencies=[],
2824+
)
2825+
2826+
tiff_files = {file for file in os.listdir(job_dir) if file.endswith(".tif")}
2827+
assert tiff_files == {
2828+
"openEO_2025-04-05Z_0.tif",
2829+
"openEO_2025-04-05Z_1.tif",
2830+
"openEO_2025-04-15Z_0.tif",
2831+
"openEO_2025-04-15Z_1.tif",
2832+
}
2833+
2834+
with open(metadata_file) as f:
2835+
items = json.load(f)["items"]
2836+
2837+
print(f"items={json.dumps(items, indent=2)}")
2838+
2839+
assert len(items) == 4
2840+
assert len({item["id"] for item in items}) == 4
2841+
assert {item["properties"]["datetime"] for item in items} == {"2025-04-05T00:00:00Z", "2025-04-15T00:00:00Z"}
2842+
2843+
for item in items:
2844+
assert set(item["assets"].keys()) == {"openEO"}

0 commit comments

Comments
 (0)