Skip to content

Commit

Permalink
Geojson: allow null geometry objects (#1858)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hansthen authored Jan 21, 2024
1 parent 0ee09a6 commit c18677f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 7 additions & 2 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,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(
Expand All @@ -1164,7 +1165,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]
Expand Down
14 changes: 11 additions & 3 deletions folium/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c18677f

Please sign in to comment.