Skip to content

Commit a0ee76e

Browse files
agussmanConengmo
andauthored
Fix empty geojson failing with style_function (#1213)
* Fix for #1212 empty geojson failing with style_function * address comments * add test Co-authored-by: Frank <[email protected]>
1 parent 9818410 commit a0ee76e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

folium/features.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,11 @@ def _validate_function(self, func, name):
579579
Tests `self.style_function` and `self.highlight_function` to ensure
580580
they are functions returning dictionaries.
581581
"""
582+
# If for some reason there are no features (e.g., empty API response)
583+
# don't attempt validation
584+
if not self.data['features']:
585+
return
586+
582587
test_feature = self.data['features'][0]
583588
if not callable(func) or not isinstance(func(test_feature), dict):
584589
raise ValueError('{} should be a function that accepts items from '
@@ -629,7 +634,8 @@ def _get_self_bounds(self):
629634

630635
def render(self, **kwargs):
631636
self.parent_map = get_obj_in_upper_tree(self, Map)
632-
if self.style or self.highlight:
637+
# Need at least one feature, otherwise style mapping fails
638+
if (self.style or self.highlight) and self.data['features']:
633639
mapper = GeoJsonStyleMapper(self.data, self.feature_identifier,
634640
self)
635641
if self.style:

tests/test_features.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def test_vegalite_major_version(vegalite_spec, version):
154154
else:
155155
assert vegalite.vegalite_major_version == version
156156

157+
157158
# GeoJsonTooltip GeometryCollection
158159
def test_geojson_tooltip():
159160
m = folium.Map([30.5, -97.5], zoom_start=10)
@@ -266,6 +267,14 @@ def _assert_id_got_added(data):
266267
assert geojson.data['features'][0]['id'] == '0'
267268

268269

270+
def test_geojson_empty_features_with_styling():
271+
# test we don't fail style function validation when there are no features
272+
m = Map()
273+
data = {"type": "FeatureCollection", "features": []}
274+
GeoJson(data, style_function=lambda x: {}).add_to(m)
275+
m.get_root().render()
276+
277+
269278
def test_geometry_collection_get_bounds():
270279
"""Assert #1599 is fixed"""
271280
geojson_data = {

0 commit comments

Comments
 (0)