Skip to content

Commit 1dd06e3

Browse files
committed
Issue #114/#211/#197 from_geodataframe: add dummy cube when no properties are specified
1 parent 69cf166 commit 1dd06e3

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

openeo_driver/datacube.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ class DriverVectorCube:
225225
COLUMN_SELECTION_ALL = "all"
226226
COLUMN_SELECTION_NUMERICAL = "numerical"
227227

228+
# Xarray cube attribute to indicate that it is a dummy cube
229+
CUBE_ATTR_VECTOR_CUBE_DUMMY = "vector_cube_dummy"
230+
228231
def __init__(
229232
self,
230233
geometries: gpd.GeoDataFrame,
@@ -319,8 +322,14 @@ def from_geodataframe(
319322
return cls(geometries=geometries_df, cube=cube)
320323

321324
else:
322-
# TODO: add a dummy 1D no-data cube?
323-
return cls(geometries=data)
325+
# Use 1D dummy cube of NaN values
326+
cube: xarray.DataArray = xarray.DataArray(
327+
data=numpy.full(shape=[data.shape[0]], fill_value=numpy.nan),
328+
dims=[cls.DIM_GEOMETRIES],
329+
coords={cls.DIM_GEOMETRIES: data.geometry.index.to_list()},
330+
attrs={cls.CUBE_ATTR_VECTOR_CUBE_DUMMY: True},
331+
)
332+
return cls(geometries=data, cube=cube)
324333

325334
@classmethod
326335
def from_fiona(
@@ -412,7 +421,7 @@ def _as_geopandas_df(
412421
"""Join geometries and cube as a geopandas dataframe"""
413422
# TODO: avoid copy?
414423
df = self._geometries.copy(deep=True)
415-
if self._cube is not None:
424+
if self._cube is not None and not self._cube.attrs.get(self.CUBE_ATTR_VECTOR_CUBE_DUMMY):
416425
assert self._cube.dims[0] == self.DIM_GEOMETRIES
417426
# TODO: better way to combine cube with geometries
418427
# Flatten multiple (non-geometry) dimensions from cube to new properties in geopandas dataframe
@@ -507,7 +516,7 @@ def to_legacy_save_result(self) -> Union["AggregatePolygonResult", "JSONResult"]
507516
# TODO: eliminate these legacy, non-standard formats?
508517
from openeo_driver.save_result import AggregatePolygonResult, JSONResult
509518

510-
if self._cube is None:
519+
if self._cube is None or self._cube.attrs.get(self.CUBE_ATTR_VECTOR_CUBE_DUMMY):
511520
# No cube: no real data to return (in legacy style), so let's just return a `null` per geometry.
512521
return JSONResult(data=[None] * self.geometry_count())
513522

tests/test_vectorcube.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,18 @@ def test_to_internal_json_defaults(self, gdf):
145145
"attrs": {},
146146
},
147147
),
148-
([], None),
148+
(
149+
[],
150+
{
151+
"name": None,
152+
"dims": ("geometries",),
153+
"coords": {
154+
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
155+
},
156+
"data": [IsNan(), IsNan()],
157+
"attrs": {"vector_cube_dummy": True},
158+
},
159+
),
149160
(
150161
["pop", "id"],
151162
{
@@ -359,7 +370,18 @@ def test_from_geodataframe_default(self, gdf):
359370
"attrs": {},
360371
},
361372
),
362-
([], None),
373+
(
374+
[],
375+
{
376+
"name": None,
377+
"dims": ("geometries",),
378+
"coords": {
379+
"geometries": {"attrs": {}, "data": [0, 1], "dims": ("geometries",)},
380+
},
381+
"data": [IsNan(), IsNan()],
382+
"attrs": {"vector_cube_dummy": True},
383+
},
384+
),
363385
(
364386
["id"],
365387
{

0 commit comments

Comments
 (0)