Skip to content

Commit 8f90397

Browse files
committed
fixup! Issue #114/#200 support promoting feature properties to cube values
1 parent 4bdd41f commit 8f90397

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

openeo_driver/datacube.py

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def from_geodataframe(
254254
data: gpd.GeoDataFrame,
255255
*,
256256
columns_for_cube: Union[List[str], str] = COLUMN_SELECTION_NUMERICAL,
257+
# TODO: change default band name to "properties" (per `load_geojson` spec introduced by https://github.com/Open-EO/openeo-processes/pull/427)
257258
dimension_name: str = DIM_BANDS,
258259
) -> "DriverVectorCube":
259260
"""
@@ -539,6 +540,9 @@ def geometry_count(self) -> int:
539540
def get_geometries(self) -> Sequence[shapely.geometry.base.BaseGeometry]:
540541
return self._geometries.geometry
541542

543+
def get_cube(self) -> Optional[xarray.DataArray]:
544+
return self._cube
545+
542546
def get_ids(self) -> Optional[Sequence]:
543547
return self._geometries.get("id")
544548

tests/test_vectorcube.py

+84
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import textwrap
22

3+
import geopandas
34
import geopandas as gpd
45
import numpy.testing
56
import pyproj
@@ -157,6 +158,89 @@ def test_with_cube_to_geojson(self, gdf):
157158
}
158159
)
159160

161+
def test_from_geodataframe_default(self, gdf):
162+
vc = DriverVectorCube.from_geodataframe(gdf)
163+
assert vc.to_geojson() == DictSubSet(
164+
{
165+
"type": "FeatureCollection",
166+
"features": [
167+
DictSubSet(
168+
{
169+
"type": "Feature",
170+
"properties": {"id": "first", "pop": 1234},
171+
"geometry": {
172+
"coordinates": (((1.0, 1.0), (3.0, 1.0), (2.0, 3.0), (1.0, 1.0)),),
173+
"type": "Polygon",
174+
},
175+
}
176+
),
177+
DictSubSet(
178+
{
179+
"type": "Feature",
180+
"properties": {"id": "second", "pop": 5678},
181+
"geometry": {
182+
"coordinates": (((4.0, 2.0), (5.0, 4.0), (3.0, 4.0), (4.0, 2.0)),),
183+
"type": "Polygon",
184+
},
185+
}
186+
),
187+
],
188+
}
189+
)
190+
cube = vc.get_cube()
191+
assert cube.dims == ("geometries", "bands")
192+
assert cube.shape == (2, 1)
193+
assert {k: list(v.values) for k, v in cube.coords.items()} == {"geometries": [0, 1], "bands": ["pop"]}
194+
195+
@pytest.mark.parametrize(
196+
["columns_for_cube", "expected"],
197+
[
198+
("numerical", {"shape": (2, 1), "coords": {"geometries": [0, 1], "bands": ["pop"]}}),
199+
("all", {"shape": (2, 2), "coords": {"geometries": [0, 1], "bands": ["id", "pop"]}}),
200+
([], None),
201+
(["id"], {"shape": (2, 1), "coords": {"geometries": [0, 1], "bands": ["id"]}}),
202+
(["pop", "id"], {"shape": (2, 2), "coords": {"geometries": [0, 1], "bands": ["pop", "id"]}}),
203+
# TODO: test specifying non-existent column (to be filled with no-data):
204+
# (["pop", "nopenope"], {"shape": (2, 2), "coords": {"geometries": [0, 1], "bands": ["pop", "nopenope"]}}),
205+
],
206+
)
207+
def test_from_geodataframe_columns_for_cube(self, gdf, columns_for_cube, expected):
208+
vc = DriverVectorCube.from_geodataframe(gdf, columns_for_cube=columns_for_cube)
209+
assert vc.to_geojson() == DictSubSet(
210+
{
211+
"type": "FeatureCollection",
212+
"features": [
213+
DictSubSet(
214+
{
215+
"type": "Feature",
216+
"properties": {"id": "first", "pop": 1234},
217+
"geometry": {
218+
"coordinates": (((1.0, 1.0), (3.0, 1.0), (2.0, 3.0), (1.0, 1.0)),),
219+
"type": "Polygon",
220+
},
221+
}
222+
),
223+
DictSubSet(
224+
{
225+
"type": "Feature",
226+
"properties": {"id": "second", "pop": 5678},
227+
"geometry": {
228+
"coordinates": (((4.0, 2.0), (5.0, 4.0), (3.0, 4.0), (4.0, 2.0)),),
229+
"type": "Polygon",
230+
},
231+
}
232+
),
233+
],
234+
}
235+
)
236+
cube = vc.get_cube()
237+
if expected is None:
238+
assert cube is None
239+
else:
240+
assert cube.dims == ("geometries", "bands")
241+
assert cube.shape == expected["shape"]
242+
assert {k: list(v.values) for k, v in cube.coords.items()} == expected["coords"]
243+
160244
@pytest.mark.parametrize(["geojson", "expected"], [
161245
(
162246
{"type": "Polygon", "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]]},

0 commit comments

Comments
 (0)