Skip to content

Commit

Permalink
DriverVectorCube.from_geojson: add support for Point/MultiPoint/Geome…
Browse files Browse the repository at this point in the history
…tryCollection

related to #71/#114/#141
  • Loading branch information
soxofaan committed Oct 11, 2022
1 parent 56b8e7b commit 67e44c7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
4 changes: 2 additions & 2 deletions openeo_driver/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}:
Expand Down
67 changes: 65 additions & 2 deletions tests/test_vectorcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]]]},
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 67e44c7

Please sign in to comment.