Skip to content

Commit 476dcc0

Browse files
talespaivaocefpaf
authored andcommitted
New plugin added: Leaflet.TextPath
1 parent 574d020 commit 476dcc0

File tree

4 files changed

+175
-1
lines changed

4 files changed

+175
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# This is to import the repository's version of folium ; not the installed one.
4+
import sys, os
5+
sys.path.insert(0, '..')
6+
import folium
7+
from folium import plugins
8+
9+
m = folium.Map([30.,0.], zoom_start=3)
10+
wind_locations = [[59.3556, -31.99219], [55.17887, -42.89062], [47.7541, -43.94531], [38.27269, -37.96875],
11+
[27.05913, -41.13281], [16.29905, -36.5625], [8.40717, -30.23437], [1.05463, -22.5],
12+
[-8.75479, -18.28125], [-21.61658, -20.03906], [-31.35364, -24.25781], [-39.90974, -30.9375],
13+
[-43.83453, -41.13281], [-47.7541, -49.92187], [-50.95843, -54.14062], [-55.9738, -56.60156]]
14+
15+
wind_line = folium.PolyLine(wind_locations, weight=15, color='#8EE9FF').add_to(m)
16+
attr = {'fill': '#007DEF', 'font-weight': 'bold', 'font-size': '24'}
17+
plugins.PolyLineTextPath(wind_line, ") ", repeat=True, offset=7, attributes=attr).add_to(m)
18+
19+
danger_line = folium.PolyLine([[-40.311, -31.952], [-12.086, -18.727]], weight=10, color='orange', opacity=0.8).add_to(m)
20+
attr = {'fill': 'red'}
21+
plugins.PolyLineTextPath(danger_line, "\u25BA", repeat=True, offset=6, attributes=attr).add_to(m)
22+
23+
plane_line = folium.PolyLine([[-49.38237, -37.26562], [-1.75754, -14.41406], [51.61802, -23.20312]], weight=1, color='black').add_to(m)
24+
attr = {'font-weight':'bold', 'font-size':'24'}
25+
plugins.PolyLineTextPath(plane_line, "\u2708 ", repeat=True, offset=8, attributes=attr).add_to(m)
26+
27+
line_to_new_dehli = folium.PolyLine([[46.67959447, 3.33984375],
28+
[46.5588603, 29.53125],
29+
[42.29356419, 51.328125],
30+
[35.74651226, 68.5546875],
31+
[28.65203063, 76.81640625]]).add_to(m)
32+
33+
line_to_hanoi = folium.PolyLine([[28.76765911, 77.60742188],
34+
[27.83907609, 88.72558594],
35+
[25.68113734, 97.3828125],
36+
[21.24842224, 105.77636719]]).add_to(m)
37+
38+
plugins.PolyLineTextPath(line_to_new_dehli, "To New Delhi", offset=-5).add_to(m)
39+
plugins.PolyLineTextPath(line_to_hanoi, "To Hanoi", offset=-5).add_to(m)
40+
41+
m.save(outfile='polyline_text.html')

folium/plugins/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .heat_map import HeatMap
1515
from .image_overlay import ImageOverlay
1616
from .fullscreen import Fullscreen
17+
from .polyline_text_path import PolyLineTextPath
1718

1819
__all__ = ['MarkerCluster',
1920
'ScrollZoomToggler',
@@ -22,4 +23,5 @@
2223
'TimestampedGeoJson',
2324
'HeatMap',
2425
'ImageOverlay',
25-
'Fullscreen']
26+
'Fullscreen',
27+
'PolyLineTextPath']

folium/plugins/polyline_text_path.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
PolyLine Text Path
4+
-----------
5+
6+
Shows a text along a PolyLine.
7+
8+
"""
9+
from jinja2 import Template
10+
11+
from branca.element import JavascriptLink, Figure
12+
from folium.features import MacroElement
13+
14+
15+
class PolyLineTextPath(MacroElement):
16+
"""Shows a text along a PolyLine."""
17+
def __init__(self, polyline, text, repeat=False, center=False, below=False, offset=0, orientation=0, attributes={}):
18+
"""Shows a text along a PolyLine.
19+
20+
Parameters
21+
----------
22+
polyline: folium.features.PolyLine object
23+
The folium.features.PolyLine object to attach the text to.
24+
25+
text: string
26+
The string to be attached to the polyline.
27+
28+
repeat: bool, default False
29+
Specifies if the text should be repeated along the polyline.
30+
31+
center: bool, default False
32+
Centers the text according to the polyline's bounding box
33+
34+
below: bool, default False
35+
Show text below the path
36+
37+
offset: int, default 0
38+
Set an offset to position text relative to the polyline.
39+
40+
orientation: int, default 0
41+
Rotate text to a specified angle.
42+
43+
attributes: dictionary
44+
Object containing the attributes applied to the text tag.
45+
Check valid attributes here: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/text#Attributes
46+
Example: {'fill': '#007DEF', 'font-weight': 'bold', 'font-size': '24'}
47+
48+
49+
"""
50+
super(PolyLineTextPath, self).__init__()
51+
self._name = 'PolyLineTextPath'
52+
self.polyline = polyline
53+
self.text = text
54+
self.repeat = bool(repeat)
55+
self.center = bool(center)
56+
self.below = bool(below)
57+
self.orientation = orientation
58+
self.offset = offset
59+
self.attributes = attributes
60+
61+
self._template = Template(u"""
62+
{% macro script(this, kwargs) %}
63+
{{this.polyline.get_name()}}.setText("{{this.text}}", {
64+
repeat: {{'true' if this.repeat else 'false'}},
65+
center: {{'true' if this.center else 'false'}},
66+
below: {{'true' if this.below else 'false'}},
67+
offset: {{this.offset}},
68+
orientation: {{this.orientation}},
69+
attributes: {{this.attributes}}
70+
});
71+
{% endmacro %}
72+
""") # noqa
73+
74+
def render(self, **kwargs):
75+
super(PolyLineTextPath, self).render(**kwargs)
76+
77+
figure = self.get_root()
78+
assert isinstance(figure, Figure), ("You cannot render this Element "
79+
"if it's not in a Figure.")
80+
81+
figure.header.add_child(
82+
JavascriptLink("https://rawgit.com/makinacorpus/Leaflet.TextPath/gh-pages/leaflet.textpath.js"), # noqa
83+
name='polylinetextpath')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Test Terminator
4+
---------------
5+
"""
6+
7+
from jinja2 import Template
8+
9+
import folium
10+
from folium import plugins
11+
12+
13+
def test_polyline_text_path():
14+
m = folium.Map([20., 0.], zoom_start=3)
15+
16+
wind_locations = [[59.3556, -31.99219], [55.17887, -42.89062], [47.7541, -43.94531], [38.27269, -37.96875],
17+
[27.05913, -41.13281], [16.29905, -36.5625], [8.40717, -30.23437], [1.05463, -22.5],
18+
[-8.75479, -18.28125], [-21.61658, -20.03906], [-31.35364, -24.25781], [-39.90974, -30.9375],
19+
[-43.83453, -41.13281], [-47.7541, -49.92187], [-50.95843, -54.14062], [-55.9738, -56.60156]]
20+
21+
22+
wind_line = folium.PolyLine(wind_locations, weight=15, color='#8EE9FF')
23+
attr = {'fill': '#007DEF', 'font-weight': 'bold', 'font-size': '24'}
24+
wind_textpath = plugins.PolyLineTextPath(wind_line, ") ", repeat=True, offset=7, attributes=attr)
25+
26+
m.add_child(wind_line)
27+
m.add_child(wind_textpath)
28+
m._repr_html_()
29+
30+
out = m._parent.render()
31+
32+
# We verify that the script import is present.
33+
script = '<script src="https://rawgit.com/makinacorpus/Leaflet.TextPath/gh-pages/leaflet.textpath.js"></script>' # noqa
34+
assert script in out
35+
36+
# We verify that the script part is correct.
37+
tmpl = Template("""
38+
{{this.polyline.get_name()}}.setText("{{this.text}}", {
39+
repeat: {{'true' if this.repeat else 'false'}},
40+
center: {{'true' if this.center else 'false'}},
41+
below: {{'true' if this.below else 'false'}},
42+
offset: {{this.offset}},
43+
orientation: {{this.orientation}},
44+
attributes: {{this.attributes}}
45+
});
46+
""") # noqa
47+
48+
assert tmpl.render(this=wind_textpath) in out

0 commit comments

Comments
 (0)