@@ -280,40 +280,39 @@ def write_assets(
280
280
def to_multipolygon (self ) -> shapely .geometry .MultiPolygon :
281
281
return shapely .ops .unary_union (self ._geometries .geometry )
282
282
283
- def _write_legacy_aggregate_polygon_result_json (
284
- self , directory : Path
285
- ) -> Dict [str , StacAsset ]:
286
- """Export to legacy AggregatePolygonResult JSON format"""
287
- # TODO: eliminate this legacy, non-standard format?
283
+ def to_legacy_save_result (self ) -> Union ["AggregatePolygonResult" , "JSONResult" ]:
284
+ """
285
+ Export to legacy AggregatePolygonResult/JSONResult objects.
286
+ Provided as temporary adaption layer while migrating to real vector cubes.
287
+ """
288
+ # TODO: eliminate these legacy, non-standard format?
288
289
from openeo_driver .save_result import AggregatePolygonResult , JSONResult
289
290
290
- def write_spatiotemporal (cube : xarray .DataArray ) -> Dict [str , StacAsset ]:
291
- """Export to legacy AggregatePolygonResult JSON format"""
291
+ cube = self ._cube
292
+ # TODO: more flexible temporal/band dimension detection?
293
+ if cube .dims == (self .DIM_GEOMETRIES , "t" ):
294
+ # Add single band dimension
295
+ cube = cube .expand_dims ({"bands" : ["band" ]}, axis = - 1 )
296
+ if cube .dims == (self .DIM_GEOMETRIES , "t" , "bands" ):
292
297
cube = cube .transpose ("t" , self .DIM_GEOMETRIES , "bands" )
293
298
timeseries = {
294
299
t .item (): t_slice .values .tolist ()
295
300
for t , t_slice in zip (cube .coords ["t" ], cube )
296
301
}
297
- result = AggregatePolygonResult (timeseries = timeseries , regions = self )
298
- return result .write_assets (directory = directory / "ignored" )
299
-
300
- def write_spatial (cube : xarray .DataArray ) -> Dict [str , StacAsset ]:
302
+ return AggregatePolygonResult (timeseries = timeseries , regions = self )
303
+ elif cube .dims == (self .DIM_GEOMETRIES , "bands" ):
301
304
cube = cube .transpose (self .DIM_GEOMETRIES , "bands" )
302
- result = JSONResult (data = cube .values .tolist ())
303
- return result .write_assets (directory / "ignored" )
305
+ return JSONResult (data = cube .values .tolist ())
306
+ raise ValueError (
307
+ f"Unsupported cube configuration { cube .dims } for _write_legacy_aggregate_polygon_result_json"
308
+ )
304
309
305
- cube = self ._cube
306
- # TODO: more flexible temporal/band dimension detection?
307
- if cube .dims == (self .DIM_GEOMETRIES , "t" ):
308
- return write_spatiotemporal (cube .expand_dims ({"bands" : ["band" ]}, axis = - 1 ))
309
- elif cube .dims == (self .DIM_GEOMETRIES , "t" , "bands" ):
310
- return write_spatiotemporal (cube )
311
- elif cube .dims == (self .DIM_GEOMETRIES , "bands" ):
312
- return write_spatial (cube )
313
- else :
314
- raise ValueError (
315
- f"Unsupported cube configuration { cube .dims } for _write_legacy_aggregate_polygon_result_json"
316
- )
310
+ def _write_legacy_aggregate_polygon_result_json (
311
+ self , directory : Path
312
+ ) -> Dict [str , StacAsset ]:
313
+ """Export to legacy AggregatePolygonResult JSON format"""
314
+ # TODO: eliminate this legacy, non-standard format?
315
+ return self .to_legacy_save_result ().write_assets (directory = directory )
317
316
318
317
def get_bounding_box (self ) -> Tuple [float , float , float , float ]:
319
318
return tuple (self ._geometries .total_bounds )
0 commit comments