Skip to content

New plugin added: Leaflet.TextPath #451

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 87 additions & 17 deletions examples/plugins_examples.ipynb

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions examples/polyline_text_path_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-

# This is to import the repository's version of folium ; not the installed one.
import sys, os
sys.path.insert(0, '..')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can drop these two lines. (BTW I am planning to re-do all examples as notebooks that we can run on Travis as "visual" tests.)

import folium
from folium import plugins

m = folium.Map([30.,0.], zoom_start=3)
wind_locations = [[59.3556, -31.99219], [55.17887, -42.89062], [47.7541, -43.94531], [38.27269, -37.96875],
[27.05913, -41.13281], [16.29905, -36.5625], [8.40717, -30.23437], [1.05463, -22.5],
[-8.75479, -18.28125], [-21.61658, -20.03906], [-31.35364, -24.25781], [-39.90974, -30.9375],
[-43.83453, -41.13281], [-47.7541, -49.92187], [-50.95843, -54.14062], [-55.9738, -56.60156]]

wind_line = folium.PolyLine(wind_locations, weight=15, color='#8EE9FF').add_to(m)
attr = {'fill': '#007DEF', 'font-weight': 'bold', 'font-size': '24'}
plugins.PolyLineTextPath(wind_line, ") ", repeat=True, offset=7, attributes=attr).add_to(m)

danger_line = folium.PolyLine([[-40.311, -31.952], [-12.086, -18.727]], weight=10, color='orange', opacity=0.8).add_to(m)
attr = {'fill': 'red'}
plugins.PolyLineTextPath(danger_line, "\u25BA", repeat=True, offset=6, attributes=attr).add_to(m)

plane_line = folium.PolyLine([[-49.38237, -37.26562], [-1.75754, -14.41406], [51.61802, -23.20312]], weight=1, color='black').add_to(m)
attr = {'font-weight':'bold', 'font-size':'24'}
plugins.PolyLineTextPath(plane_line, "\u2708 ", repeat=True, offset=8, attributes=attr).add_to(m)

line_to_new_dehli = folium.PolyLine([[46.67959447, 3.33984375],
[46.5588603, 29.53125],
[42.29356419, 51.328125],
[35.74651226, 68.5546875],
[28.65203063, 76.81640625]]).add_to(m)

line_to_hanoi = folium.PolyLine([[28.76765911, 77.60742188],
[27.83907609, 88.72558594],
[25.68113734, 97.3828125],
[21.24842224, 105.77636719]]).add_to(m)

plugins.PolyLineTextPath(line_to_new_dehli, "To New Delhi", offset=-5).add_to(m)
plugins.PolyLineTextPath(line_to_hanoi, "To Hanoi", offset=-5).add_to(m)

m.save(outfile='polyline_text.html')
4 changes: 3 additions & 1 deletion folium/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .heat_map import HeatMap
from .image_overlay import ImageOverlay
from .fullscreen import Fullscreen
from .polyline_text_path import PolyLineTextPath

__all__ = ['MarkerCluster',
'ScrollZoomToggler',
Expand All @@ -22,4 +23,5 @@
'TimestampedGeoJson',
'HeatMap',
'ImageOverlay',
'Fullscreen']
'Fullscreen',
'PolyLineTextPath']
83 changes: 83 additions & 0 deletions folium/plugins/polyline_text_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
"""
PolyLine Text Path
-----------

Shows a text along a PolyLine.

"""
from jinja2 import Template

from branca.element import JavascriptLink, Figure
from folium.features import MacroElement


class PolyLineTextPath(MacroElement):
"""Shows a text along a PolyLine."""
def __init__(self, polyline, text, repeat=False, center=False, below=False, offset=0, orientation=0, attributes={}):
"""Shows a text along a PolyLine.

Parameters
----------
polyline: folium.features.PolyLine object
The folium.features.PolyLine object to attach the text to.

text: string
The string to be attached to the polyline.

repeat: bool, default False
Specifies if the text should be repeated along the polyline.

center: bool, default False
Centers the text according to the polyline's bounding box

below: bool, default False
Show text below the path

offset: int, default 0
Set an offset to position text relative to the polyline.

orientation: int, default 0
Rotate text to a specified angle.

attributes: dictionary
Object containing the attributes applied to the text tag.
Check valid attributes here: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/text#Attributes
Example: {'fill': '#007DEF', 'font-weight': 'bold', 'font-size': '24'}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a note and the URL for the plugin page. (I guess https://github.com/makinacorpus/Leaflet.TextPath ?)


"""
super(PolyLineTextPath, self).__init__()
self._name = 'PolyLineTextPath'
self.polyline = polyline
self.text = text
self.repeat = bool(repeat)
self.center = bool(center)
self.below = bool(below)
self.orientation = orientation
self.offset = offset
self.attributes = attributes

self._template = Template(u"""
{% macro script(this, kwargs) %}
{{this.polyline.get_name()}}.setText("{{this.text}}", {
repeat: {{'true' if this.repeat else 'false'}},
center: {{'true' if this.center else 'false'}},
below: {{'true' if this.below else 'false'}},
offset: {{this.offset}},
orientation: {{this.orientation}},
attributes: {{this.attributes}}
});
{% endmacro %}
""") # noqa

def render(self, **kwargs):
super(PolyLineTextPath, self).render(**kwargs)

figure = self.get_root()
assert isinstance(figure, Figure), ("You cannot render this Element "
"if it's not in a Figure.")

figure.header.add_child(
JavascriptLink("https://rawgit.com/makinacorpus/Leaflet.TextPath/gh-pages/leaflet.textpath.js"), # noqa
name='polylinetextpath')
48 changes: 48 additions & 0 deletions tests/plugins/test_polyline_text_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
"""
Test Terminator
---------------
"""

from jinja2 import Template

import folium
from folium import plugins


def test_polyline_text_path():
m = folium.Map([20., 0.], zoom_start=3)

wind_locations = [[59.3556, -31.99219], [55.17887, -42.89062], [47.7541, -43.94531], [38.27269, -37.96875],
[27.05913, -41.13281], [16.29905, -36.5625], [8.40717, -30.23437], [1.05463, -22.5],
[-8.75479, -18.28125], [-21.61658, -20.03906], [-31.35364, -24.25781], [-39.90974, -30.9375],
[-43.83453, -41.13281], [-47.7541, -49.92187], [-50.95843, -54.14062], [-55.9738, -56.60156]]


wind_line = folium.PolyLine(wind_locations, weight=15, color='#8EE9FF')
attr = {'fill': '#007DEF', 'font-weight': 'bold', 'font-size': '24'}
wind_textpath = plugins.PolyLineTextPath(wind_line, ") ", repeat=True, offset=7, attributes=attr)

m.add_child(wind_line)
m.add_child(wind_textpath)
m._repr_html_()

out = m._parent.render()

# We verify that the script import is present.
script = '<script src="https://rawgit.com/makinacorpus/Leaflet.TextPath/gh-pages/leaflet.textpath.js"></script>' # noqa
assert script in out

# We verify that the script part is correct.
tmpl = Template("""
{{this.polyline.get_name()}}.setText("{{this.text}}", {
repeat: {{'true' if this.repeat else 'false'}},
center: {{'true' if this.center else 'false'}},
below: {{'true' if this.below else 'false'}},
offset: {{this.offset}},
orientation: {{this.orientation}},
attributes: {{this.attributes}}
});
""") # noqa

assert tmpl.render(this=wind_textpath) in out