Skip to content

Commit cdca018

Browse files
authored
Merge pull request #626 from radumas/location_init_check
Add value checking for location #625
2 parents 9eb0587 + 2d278a5 commit cdca018

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

folium/map.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@
6060
'https://rawgit.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css'), # noqa
6161
]
6262

63+
def _validate_location(values):
64+
"""Validates and formats location values before setting"""
65+
if type(values) not in [list, tuple]:
66+
raise TypeError("Location is not a list, expecting ex: location=[45.523, -122.675]")
67+
68+
if len(values) != 2:
69+
raise ValueError("Location should have two values, [lat, lon]")
70+
71+
try:
72+
values = [float(val) for val in values]
73+
except:
74+
raise ValueError("Location values should be numeric, {} is not a number".format(val))
75+
return values
76+
6377

6478
class LegacyMap(MacroElement):
6579
"""Create a Map with Folium and Leaflet.js
@@ -170,7 +184,7 @@ def __init__(self, location=None, width='100%', height='100%',
170184
self.location = [0, 0]
171185
self.zoom_start = min_zoom
172186
else:
173-
self.location = location
187+
self.location = _validate_location(location)
174188
self.zoom_start = zoom_start
175189

176190
Figure().add_child(self)
@@ -337,7 +351,6 @@ def render(self, **kwargs):
337351

338352
super(LegacyMap, self).render(**kwargs)
339353

340-
341354
class GlobalSwitches(Element):
342355
def __init__(self, prefer_canvas=False, no_touch=False, disable_3d=False):
343356
super(GlobalSwitches, self).__init__()

0 commit comments

Comments
 (0)