Skip to content

Add support for leaflet div markers to folium #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2015
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
53 changes: 53 additions & 0 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def __init__(self, location=None, width='100%', height='100%',
'stamenterrain', 'stamentoner',
'stamenwatercolor',
'cartodbpositron', 'cartodbdark_matter']

self.tile_types = {}
for tile in self.default_tiles:
tile_path = 'tiles/%s' % tile
Expand Down Expand Up @@ -357,6 +358,58 @@ def simple_marker(self, location=None, popup=None,
append = (icon, marker, popup_out, add_mark)
self.template_vars.setdefault(name, []).append(append)

@iter_obj('div_mark')
def div_markers(self, locations=None, popups=None, marker_size=10, popup_width=300):
"""Create a simple div marker on the map, with optional
popup text or Vincent visualization. Useful for marking points along a
line.

Parameters
----------
locations: list of locations, where each location is an array
Latitude and Longitude of Marker (Northing, Easting)
popup: list of popups, each popup should be a string or tuple, default 'Pop Text'
Input text or visualization for object. Can pass either text,
or a tuple of the form (Vincent object, 'vis_path.json')
It is possible to adjust the width of text/HTML popups
using the optional keywords `popup_width`. (Leaflet default is 300px.)
marker_size
default is 5

Returns
-------
Marker names and HTML in obj.template_vars

Example
-------
>>>map.div_markers(locations=[[37.421114, -122.128314], [37.391637, -122.085416], [37.388832, -122.087709]], popups=['1437494575531', '1437492135937', '1437493590434'])

"""
call_cnt = self.mark_cnt['div_mark']
if locations is None or popups is None:
raise RuntimeError("Both locations and popups are mandatory")
for (point_cnt, (location, popup)) in enumerate(zip(locations, popups)):
marker_num = 'div_marker_{0}_{1}'.format(call_cnt, point_cnt)

icon_temp = self.env.get_template('static_div_icon.js')
icon_name = marker_num+"_icon"
icon = icon_temp.render({'icon_name': icon_name,
'size': marker_size})

mark_temp = self.env.get_template('simple_marker.js')
# Get marker and popup.
marker = mark_temp.render({'marker': marker_num,
'lat': location[0],
'lon': location[1],
'icon': "{'icon':"+icon_name+"}"
})

popup_out = self._popup_render(popup=popup, mk_name='div_marker_{0}_'.format(call_cnt),
count=point_cnt, width=popup_width)
add_mark = 'map.addLayer(div_marker_{0}_{1})'.format(call_cnt, point_cnt)
append = (icon, marker, popup_out, add_mark)
self.template_vars.setdefault('div_markers', []).append(append)

@iter_obj('line')
def line(self, locations,
line_color=None, line_opacity=None, line_weight=None,
Expand Down
7 changes: 7 additions & 0 deletions folium/templates/fol_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
{{ add_mark }}
{% endfor %}

{% for icon, mark, popup, add_mark in div_markers %}
{{ icon }}
{{ mark }}
{{ popup }}
{{ add_mark }}
{% endfor %}

{% for mark, popup, add_mark in markers %}
{{ mark }}
{{ popup }}
Expand Down
2 changes: 2 additions & 0 deletions folium/templates/static_div_icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var {{icon_name}} = L.divIcon({className: 'leaflet-div-icon',
'iconSize': [{{size}},{{size}}]});
38 changes: 38 additions & 0 deletions tests/folium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,44 @@ def test_simple_marker(self):
nopopup = ''
assert self.map.template_vars['custom_markers'][2][2] == nopopup

def test_div_markers(self):
'''Test div marker list addition'''

icon_templ = self.env.get_template('static_div_icon.js')
mark_templ = self.env.get_template('simple_marker.js')
popup_templ = self.env.get_template('simple_popup.js')

# Test with popups (expected use case)
self.map.div_markers(locations=[[37.421114, -122.128314], [37.391637, -122.085416], [37.388832, -122.087709]], popups=['1437494575531', '1437492135937', '1437493590434'])
icon_1 = icon_templ.render({'icon_name': 'div_marker_1_0_icon', 'size': 10})
mark_1 = mark_templ.render({'marker': 'div_marker_1_0', 'lat': 37.421114,
'lon': -122.128314,
'icon': "{'icon':div_marker_1_0_icon}"})
popup_1 = popup_templ.render({'pop_name': 'div_marker_1_0',
'pop_txt': '"1437494575531"',
'width': 300})
nt.assert_equals(self.map.mark_cnt['div_mark'], 1)
nt.assert_equals(self.map.template_vars['div_markers'][0][0], icon_1)
nt.assert_equals(self.map.template_vars['div_markers'][0][1], mark_1)
nt.assert_equals(self.map.template_vars['div_markers'][0][2], popup_1)

# Second set of markers with popups to test the numbering
self.map.div_markers(locations=[[37.421114, -122.128314], [37.391637, -122.085416], [37.388832, -122.087709]], popups=['1437494575531', '1437492135937', '1437493590434'])
icon_2 = icon_templ.render({'icon_name': 'div_marker_2_1_icon', 'size': 10})
mark_2 = mark_templ.render({'marker': 'div_marker_2_1', 'lat': 37.391637,
'lon': -122.085416,
'icon': "{'icon':div_marker_2_1_icon}"})
popup_2 = popup_templ.render({'pop_name': 'div_marker_2_1',
'pop_txt': '"1437492135937"',
'width': 300})
nt.assert_equals(self.map.mark_cnt['div_mark'], 2)
nt.assert_equals(self.map.template_vars['div_markers'][4][0], icon_2)
nt.assert_equals(self.map.template_vars['div_markers'][4][1], mark_2)
nt.assert_equals(self.map.template_vars['div_markers'][4][2], popup_2)

# Test no popup. If there are no popups, then we should get a runtimeerror.
nt.assert_raises(RuntimeError, self.map.div_markers, [[45.60, -122.8]])

def test_circle_marker(self):
"""Test circle marker additions."""

Expand Down