Skip to content

Commit f4d024d

Browse files
jtbakerConengmo
authored andcommitted
GeoJsonTooltip warn for GeometryCollection (#988)
Warn when a user renders a GeoJsonTooltip that uses a geojson with a GeometryCollection in it. Add a test that verifies the warning triggers correctly.
1 parent 7fbf41e commit f4d024d

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

folium/features.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,24 @@ def __init__(self, fields, aliases=None, labels=True,
713713
# noqa outside of type checking.
714714
self.style = style
715715

716+
def warn_for_geometry_collections(self):
717+
"""Checks for GeoJson GeometryCollection features to warn user about incompatibility."""
718+
geom_collections = [
719+
feature.get('properties') if feature.get('properties') is not None else key
720+
for key, feature in enumerate(self._parent.data['features'])
721+
if feature['geometry']['type'] == 'GeometryCollection'
722+
]
723+
if any(geom_collections):
724+
warnings.warn(
725+
"GeoJsonTooltip is not configured to render tooltips for GeoJson GeometryCollection geometries. "
726+
"Please consider reworking these features: {} to MultiPolygon for full functionality.\n"
727+
"https://tools.ietf.org/html/rfc7946#page-9".format(geom_collections), UserWarning)
728+
716729
def render(self, **kwargs):
717730
"""Renders the HTML representation of the element."""
718731
if isinstance(self._parent, GeoJson):
719732
keys = tuple(self._parent.data['features'][0]['properties'].keys())
733+
self.warn_for_geometry_collections()
720734
elif isinstance(self._parent, TopoJson):
721735
obj_name = self._parent.object_path.split('.')[-1]
722736
keys = tuple(self._parent.data['objects'][obj_name][

tests/kuntarajat.geojson

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/test_features.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
77
"""
88

9-
from __future__ import (absolute_import, division, print_function)
10-
119
import os
12-
13-
from six import text_type
10+
import warnings
1411

1512
from branca.element import Element
1613

17-
from folium import Map, Popup
1814
import folium
15+
from folium import Map, Popup
16+
17+
from six import text_type
18+
1919

2020
tmpl = """
2121
<!DOCTYPE html>
@@ -29,6 +29,10 @@
2929
""" # noqa
3030

3131

32+
# Root path variable
33+
rootpath = os.path.abspath(os.path.dirname(__file__))
34+
35+
3236
# Figure
3337
def test_figure_creation():
3438
f = folium.Figure()
@@ -105,3 +109,15 @@ def test_color_line():
105109
opacity=1)
106110
m.add_child(color_line)
107111
m._repr_html_()
112+
113+
114+
# GeoJsonTooltip GeometryCollection
115+
def test_geojson_tooltip():
116+
m = folium.Map([30.5, -97.5], zoom_start=10)
117+
folium.GeoJson(os.path.join(rootpath, "kuntarajat.geojson"),
118+
tooltip=folium.GeoJsonTooltip(fields=['code', 'name'])
119+
).add_to(m)
120+
with warnings.catch_warnings(record=True) as w:
121+
warnings.simplefilter('always')
122+
m._repr_html_()
123+
assert issubclass(w[-1].category, UserWarning), "GeoJsonTooltip GeometryCollection test failed."

0 commit comments

Comments
 (0)