Skip to content

Commit efbd1db

Browse files
committed
Support multi_polyline
1 parent d71d01c commit efbd1db

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

folium/folium.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,58 @@ def line(self, locations,
382382
add_line))
383383

384384

385+
@iter_obj('multiline')
386+
def multiline(self, locations,
387+
line_color=None, line_opacity=None, line_weight=None):
388+
'''Add a multiPolyline to the map with optional styles.
389+
390+
A multiPolyline is single layer that consists of several polylines that
391+
share styling/popup.
392+
393+
Parameters
394+
----------
395+
locations: list of lists of points (latitude, longitude)
396+
Latitude and Longitude of line (Northing, Easting)
397+
line_color: string, default Leaflet's default ('#03f')
398+
line_opacity: float, default Leaflet's default (0.5)
399+
line_weight: float, default Leaflet's default (5)
400+
401+
Note: If the optional styles are omitted, they will not be included
402+
in the HTML output and will obtain the Leaflet defaults listed above.
403+
404+
Example
405+
-------
406+
>>>map.multiline(locations=[[(45.5236, -122.6750), (45.5236, -122.6751)],
407+
[(45.5237, -122.6750), (45.5237, -122.6751)],
408+
[(45.5238, -122.6750), (45.5238, -122.6751)]])
409+
>>>map.multiline(locations=[[(45.5236, -122.6750), (45.5236, -122.6751)],
410+
[(45.5237, -122.6750), (45.5237, -122.6751)],
411+
[(45.5238, -122.6750), (45.5238, -122.6751)]],
412+
line_color='red',
413+
line_weight=2,
414+
line_opacity=1.0)
415+
'''
416+
417+
count = self.mark_cnt['multiline']
418+
419+
multiline_temp = self.env.get_template('multi_polyline.js')
420+
421+
multiline_opts = {'color': line_color,
422+
'weight': line_weight,
423+
'opacity': line_opacity}
424+
425+
varname = 'multiline_{}'.format(count)
426+
multiline_rendered = multiline_temp.render({'multiline': varname,
427+
'locations': locations,
428+
'options': multiline_opts})
429+
430+
add_multiline = 'map.addLayer({});'.format(varname)
431+
432+
self.template_vars.setdefault('multilines', []).append((multiline_rendered,
433+
add_multiline))
434+
435+
436+
385437
@iter_obj('circle')
386438
def circle_marker(self, location=None, radius=500, popup='Pop Text',
387439
popup_on=True, line_color='black', fill_color='black',

folium/templates/fol_template.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@
139139
{{ add_line }}
140140
{% endfor %}
141141

142+
{% for multiline, add_multiline in multilines %}
143+
{{ multiline }}
144+
{{ add_multiline }}
145+
{% endfor %}
146+
142147
{{ lat_lng_pop }}
143148

144149
{{ click_pop }}

folium/templates/geojson_template.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
{{ add_line }}
9090
{% endfor %}
9191

92+
{% for multiline, add_multiline in multilines %}
93+
{{ multiline }}
94+
{{ add_multiline }}
95+
{% endfor %}
96+
9297
{{ lat_lng_pop }}
9398

9499
{{ click_pop }}

folium/templates/multi_polyline.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var latLngs = [
2+
{% for location in locations %}
3+
[
4+
{% for loc in location %}
5+
[{{ loc[0] }}, {{ loc[1] }}],
6+
{% endfor %}
7+
],
8+
{% endfor %}];
9+
10+
var {{ multiline }} = L.multiPolyline(latLngs,{
11+
{% if options.color != None %}color: '{{ options.color }}',{% endif %}
12+
{% if options.weight != None %}weight: {{ options.weight }},{% endif %}
13+
{% if options.opacity != None %}opacity: {{ options.opacity }},{% endif %}
14+
});

tests/folium_tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,27 @@ def test_create_map(self):
411411

412412
# Test write
413413
map.create_map()
414+
415+
def test_multi_polyline(self):
416+
'''Test multi_polyline'''
417+
418+
multiline_temp = self.env.get_template('multi_polyline.js')
419+
420+
multiline_opts = {'color': 'blue',
421+
'weight': 2,
422+
'opacity': 1}
423+
locations = [
424+
[[45.5236, -122.6750], [45.5236, -122.6751]],
425+
[[45.5237, -122.6750], [45.5237, -122.6751]],
426+
[[45.5238, -122.6750], [45.5238, -122.6751]]
427+
]
428+
multiline_rendered = multiline_temp.render({'multiline': 'multiline_1',
429+
'locations': locations,
430+
'options': multiline_opts})
431+
432+
self.map.multiline(locations=locations,
433+
line_color=multiline_opts['color'],
434+
line_weight=multiline_opts['weight'],
435+
line_opacity=multiline_opts['opacity'])
436+
assert self.map.template_vars['multilines'][0][0] == multiline_rendered
437+

0 commit comments

Comments
 (0)