From 67e44c7d7abcbcdcc8316cd23f614fa5f58428d0 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Tue, 11 Oct 2022 09:32:49 +0200 Subject: [PATCH] DriverVectorCube.from_geojson: add support for Point/MultiPoint/GeometryCollection related to #71/#114/#141 --- openeo_driver/datacube.py | 4 +-- tests/test_vectorcube.py | 67 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/openeo_driver/datacube.py b/openeo_driver/datacube.py index 3edbd2f6..e19ff2a6 100644 --- a/openeo_driver/datacube.py +++ b/openeo_driver/datacube.py @@ -195,12 +195,12 @@ def from_fiona(cls, paths: List[str], driver: str, options: dict) -> "DriverVect def from_geojson(cls, geojson: dict) -> "DriverVectorCube": """Construct vector cube from GeoJson dict structure""" # TODO support more geojson types? - if geojson["type"] in {"Polygon", "MultiPolygon"}: + if geojson["type"] in {"Polygon", "MultiPolygon", "Point", "MultiPoint"}: features = [{"type": "Feature", "geometry": geojson, "properties": {}}] elif geojson["type"] in {"Feature"}: features = [geojson] elif geojson["type"] in {"GeometryCollection"}: - #TODO GeometryCollection is offically unsupported + # TODO #71 #114 Deprecate/avoid usage of GeometryCollection log.error("Input GeoJSON of deprecated type 'GeometryCollection', please use a FeatureCollection or another type of Multi geometry.") features = [{"type": "Feature", "geometry": g, "properties": {}} for g in geojson["geometries"]] elif geojson["type"] in {"FeatureCollection"}: diff --git a/tests/test_vectorcube.py b/tests/test_vectorcube.py index 1532e4d5..18b4fdfb 100644 --- a/tests/test_vectorcube.py +++ b/tests/test_vectorcube.py @@ -131,6 +131,33 @@ def test_with_cube_to_geojson(self, gdf): ], ), ( + {"type": "Point", "coordinates": [1, 1]}, + [ + DictSubSet( + { + "type": "Feature", + "geometry": {"type": "Point", "coordinates": (1, 1)}, + "properties": {}, + } + ), + ], + ), + ( + {"type": "MultiPoint", "coordinates": [[1, 1], [2, 3]]}, + [ + DictSubSet( + { + "type": "Feature", + "geometry": { + "type": "MultiPoint", + "coordinates": ((1, 1), (2, 3)), + }, + "properties": {}, + } + ), + ], + ), + ( { "type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]]}, @@ -172,8 +199,44 @@ def test_with_cube_to_geojson(self, gdf): "properties": {"id": 2}, }), ], - ), - ]) + ), + ( + { + "type": "GeometryCollection", + "geometries": [ + { + "type": "Polygon", + "coordinates": [[(1, 1), (3, 1), (2, 3), (1, 1)]], + }, + { + "type": "MultiPolygon", + "coordinates": [[[(1, 1), (3, 1), (2, 3), (1, 1)]]], + }, + ], + }, + [ + DictSubSet( + { + "type": "Feature", + "geometry": { + "type": "Polygon", + "coordinates": (((1, 1), (3, 1), (2, 3), (1, 1)),), + }, + } + ), + DictSubSet( + { + "type": "Feature", + "geometry": { + "type": "MultiPolygon", + "coordinates": [(((1, 1), (3, 1), (2, 3), (1, 1)),)], + }, + } + ), + ], + ), + ], + ) def test_from_geojson(self, geojson, expected): vc = DriverVectorCube.from_geojson(geojson) assert vc.to_geojson() == DictSubSet({