Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ def _validate_function(self, func, name):
Tests `self.style_function` and `self.highlight_function` to ensure
they are functions returning dictionaries.
"""
# If for some reason there are no features (e.g., empty API response)
# don't attempt validation
if not self.data['features']:
return

test_feature = self.data['features'][0]
if not callable(func) or not isinstance(func(test_feature), dict):
raise ValueError('{} should be a function that accepts items from '
Expand Down Expand Up @@ -629,7 +634,8 @@ def _get_self_bounds(self):

def render(self, **kwargs):
self.parent_map = get_obj_in_upper_tree(self, Map)
if self.style or self.highlight:
# Need at least one feature, otherwise style mapping fails
if (self.style or self.highlight) and self.data['features']:
mapper = GeoJsonStyleMapper(self.data, self.feature_identifier,
self)
if self.style:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_vegalite_major_version(vegalite_spec, version):
else:
assert vegalite.vegalite_major_version == version


# GeoJsonTooltip GeometryCollection
def test_geojson_tooltip():
m = folium.Map([30.5, -97.5], zoom_start=10)
Expand Down Expand Up @@ -266,6 +267,14 @@ def _assert_id_got_added(data):
assert geojson.data['features'][0]['id'] == '0'


def test_geojson_empty_features_with_styling():
# test we don't fail style function validation when there are no features
m = Map()
data = {"type": "FeatureCollection", "features": []}
GeoJson(data, style_function=lambda x: {}).add_to(m)
m.get_root().render()


def test_geometry_collection_get_bounds():
"""Assert #1599 is fixed"""
geojson_data = {
Expand Down