Skip to content

Commit

Permalink
Merge pull request #626 from radumas/location_init_check
Browse files Browse the repository at this point in the history
Add value checking for location #625
  • Loading branch information
ocefpaf committed Jun 27, 2017
2 parents 9eb0587 + 2d278a5 commit cdca018
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions folium/map.py
Expand Up @@ -60,6 +60,20 @@
'https://rawgit.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css'), # noqa
]

def _validate_location(values):
"""Validates and formats location values before setting"""
if type(values) not in [list, tuple]:
raise TypeError("Location is not a list, expecting ex: location=[45.523, -122.675]")

if len(values) != 2:
raise ValueError("Location should have two values, [lat, lon]")

try:
values = [float(val) for val in values]
except:
raise ValueError("Location values should be numeric, {} is not a number".format(val))
return values


class LegacyMap(MacroElement):
"""Create a Map with Folium and Leaflet.js
Expand Down Expand Up @@ -170,7 +184,7 @@ def __init__(self, location=None, width='100%', height='100%',
self.location = [0, 0]
self.zoom_start = min_zoom
else:
self.location = location
self.location = _validate_location(location)
self.zoom_start = zoom_start

Figure().add_child(self)
Expand Down Expand Up @@ -337,7 +351,6 @@ def render(self, **kwargs):

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


class GlobalSwitches(Element):
def __init__(self, prefer_canvas=False, no_touch=False, disable_3d=False):
super(GlobalSwitches, self).__init__()
Expand Down

0 comments on commit cdca018

Please sign in to comment.