Skip to content

Commit de5ddc6

Browse files
committed
Fix bounds for multi-member and wrapped GeometryCollections
1 parent 6a72da6 commit de5ddc6

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

folium/utilities.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,11 @@ def iter_coords(obj: Any) -> Iterator[tuple[float, ...]]:
280280
if isinstance(obj, (tuple, list)):
281281
coords = obj
282282
elif "features" in obj:
283-
coords = [
284-
geom["geometry"]["coordinates"]
285-
for geom in obj["features"]
286-
if geom["geometry"]
287-
]
283+
coords = obj["features"]
288284
elif "geometry" in obj:
289-
coords = obj["geometry"]["coordinates"] if obj["geometry"] else []
290-
elif (
291-
"geometries" in obj
292-
and obj["geometries"][0]
293-
and "coordinates" in obj["geometries"][0]
294-
):
295-
coords = obj["geometries"][0]["coordinates"]
285+
coords = [obj["geometry"]] if obj["geometry"] else []
286+
elif "geometries" in obj:
287+
coords = obj["geometries"]
296288
else:
297289
coords = obj.get("coordinates", obj)
298290
for coord in coords:

tests/test_features.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,41 @@ def test_geometry_collection_get_bounds():
388388
assert folium.GeoJson(geojson_data).get_bounds() == [[0, -3], [4, 2]]
389389

390390

391+
@pytest.mark.parametrize("container", ["geometry", "feature", "feature_collection"])
392+
@pytest.mark.parametrize("collection", ["multiple", "nested", "empty"])
393+
def test_geometry_collection_bounds_all_members(container, collection):
394+
geometries = [
395+
{"type": "Point", "coordinates": [2, 1]},
396+
{"type": "LineString", "coordinates": [[-3, 4], [40, 30]]},
397+
]
398+
expected = [[1, -3], [30, 40]]
399+
if collection == "nested":
400+
geometries = [
401+
{"type": "GeometryCollection", "geometries": []},
402+
{"type": "GeometryCollection", "geometries": geometries},
403+
]
404+
elif collection == "empty":
405+
geometries = []
406+
expected = [[None, None], [None, None]]
407+
408+
data = {"type": "GeometryCollection", "geometries": geometries}
409+
if container != "geometry":
410+
data = {"type": "Feature", "properties": {}, "geometry": data}
411+
if container == "feature_collection":
412+
data = {
413+
"type": "FeatureCollection",
414+
"features": [
415+
{"type": "Feature", "properties": {}, "geometry": None},
416+
data,
417+
],
418+
}
419+
420+
m = folium.Map()
421+
layer = folium.GeoJson(data).add_to(m)
422+
assert layer.get_bounds() == expected
423+
assert m.get_bounds() == expected
424+
425+
391426
def test_choropleth_get_by_key():
392427
geojson_data = {
393428
"id": "0",

0 commit comments

Comments
 (0)