Skip to content

Commit c18677f

Browse files
authored
Geojson: allow null geometry objects (#1858)
* GeoJson objects can have null geometry objects See https://datatracker.ietf.org/doc/html/rfc7946#section-3.2 A Feature object has a member with the name "geometry". The value of the geometry member SHALL be either a Geometry object as defined above or . . . a JSON null value. * Missed an instance geometry cannot be null * Handle empty geojson features
1 parent 0ee09a6 commit c18677f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

folium/features.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,8 @@ def warn_for_geometry_collections(self) -> None:
11481148
geom_collections = [
11491149
feature.get("properties") if feature.get("properties") is not None else key
11501150
for key, feature in enumerate(self._parent.data["features"])
1151-
if feature["geometry"]["type"] == "GeometryCollection"
1151+
if feature["geometry"]
1152+
and feature["geometry"]["type"] == "GeometryCollection"
11521153
]
11531154
if any(geom_collections):
11541155
warnings.warn(
@@ -1164,7 +1165,11 @@ def render(self, **kwargs) -> None:
11641165
"""Renders the HTML representation of the element."""
11651166
figure = self.get_root()
11661167
if isinstance(self._parent, GeoJson):
1167-
keys = tuple(self._parent.data["features"][0]["properties"].keys())
1168+
keys = tuple(
1169+
self._parent.data["features"][0]["properties"].keys()
1170+
if self._parent.data["features"]
1171+
else []
1172+
)
11681173
self.warn_for_geometry_collections()
11691174
elif isinstance(self._parent, TopoJson):
11701175
obj_name = self._parent.object_path.split(".")[-1]

folium/utilities.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,18 @@ def iter_coords(obj: Any) -> Iterator[Tuple[float, ...]]:
276276
if isinstance(obj, (tuple, list)):
277277
coords = obj
278278
elif "features" in obj:
279-
coords = [geom["geometry"]["coordinates"] for geom in obj["features"]]
279+
coords = [
280+
geom["geometry"]["coordinates"]
281+
for geom in obj["features"]
282+
if geom["geometry"]
283+
]
280284
elif "geometry" in obj:
281-
coords = obj["geometry"]["coordinates"]
282-
elif "geometries" in obj and "coordinates" in obj["geometries"][0]:
285+
coords = obj["geometry"]["coordinates"] if obj["geometry"] else []
286+
elif (
287+
"geometries" in obj
288+
and obj["geometries"][0]
289+
and "coordinates" in obj["geometries"][0]
290+
):
283291
coords = obj["geometries"][0]["coordinates"]
284292
else:
285293
coords = obj.get("coordinates", obj)

0 commit comments

Comments
 (0)