From a6b3f0044de02651079641318f1fcb7fe69aec60 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Mon, 8 Jan 2024 21:01:33 +0100 Subject: [PATCH 1/3] 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. --- folium/utilities.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/folium/utilities.py b/folium/utilities.py index 942206930..80526db22 100644 --- a/folium/utilities.py +++ b/folium/utilities.py @@ -276,10 +276,18 @@ def iter_coords(obj: Any) -> Iterator[Tuple[float, ...]]: if isinstance(obj, (tuple, list)): coords = obj elif "features" in obj: - coords = [geom["geometry"]["coordinates"] for geom in obj["features"]] + coords = [ + geom["geometry"]["coordinates"] + for geom in obj["features"] + if geom["geometry"] + ] elif "geometry" in obj: - coords = obj["geometry"]["coordinates"] - elif "geometries" in obj and "coordinates" in obj["geometries"][0]: + coords = obj["geometry"]["coordinates"] if obj["geometry"] else [] + elif ( + "geometries" in obj + and obj["geometries"][0] + and "coordinates" in obj["geometries"][0] + ): coords = obj["geometries"][0]["coordinates"] else: coords = obj.get("coordinates", obj) From b66354662fd7d81c011386ad4bf5cfff4d96fe54 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Tue, 9 Jan 2024 19:09:21 +0100 Subject: [PATCH 2/3] Missed an instance geometry cannot be null --- folium/features.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/folium/features.py b/folium/features.py index d09cf6e7e..5b929be05 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1120,7 +1120,8 @@ def warn_for_geometry_collections(self) -> None: geom_collections = [ feature.get("properties") if feature.get("properties") is not None else key for key, feature in enumerate(self._parent.data["features"]) - if feature["geometry"]["type"] == "GeometryCollection" + if feature["geometry"] + and feature["geometry"]["type"] == "GeometryCollection" ] if any(geom_collections): warnings.warn( From 36143c42ae6d7634cceb130385ebdcd44cd62a1d Mon Sep 17 00:00:00 2001 From: Hans Then Date: Wed, 17 Jan 2024 18:43:40 +0100 Subject: [PATCH 3/3] Handle empty geojson features --- folium/features.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/folium/features.py b/folium/features.py index 5b929be05..a66e9ae29 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1137,7 +1137,11 @@ def render(self, **kwargs) -> None: """Renders the HTML representation of the element.""" figure = self.get_root() if isinstance(self._parent, GeoJson): - keys = tuple(self._parent.data["features"][0]["properties"].keys()) + keys = tuple( + self._parent.data["features"][0]["properties"].keys() + if self._parent.data["features"] + else [] + ) self.warn_for_geometry_collections() elif isinstance(self._parent, TopoJson): obj_name = self._parent.object_path.split(".")[-1]