Skip to content

Commit d3765c5

Browse files
ocefpafConengmo
authored andcommitted
Antpath plugin and vector path options refactor (#1016)
* add AntPath plugin * refactor parse options and don't validated the kw * add test utility to compare rendered maps https://github.com/rubenspgcavalcante/leaflet-ant-path
1 parent f509031 commit d3765c5

File tree

7 files changed

+386
-216
lines changed

7 files changed

+386
-216
lines changed

examples/PolyLineTextPath_AntPath.ipynb

Lines changed: 218 additions & 0 deletions
Large diffs are not rendered by default.

examples/Polyline_text_path.ipynb

Lines changed: 0 additions & 183 deletions
This file was deleted.

folium/plugins/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from __future__ import (absolute_import, division, print_function)
1212

13+
from folium.plugins.antpath import AntPath
1314
from folium.plugins.beautify_icon import BeautifyIcon
1415
from folium.plugins.boat_marker import BoatMarker
1516
from folium.plugins.draw import Draw
@@ -32,6 +33,7 @@
3233
from folium.plugins.timestamped_wmstilelayer import TimestampedWmsTileLayers
3334

3435
__all__ = [
36+
'AntPath',
3537
'BeautifyIcon',
3638
'BoatMarker',
3739
'Draw',

folium/plugins/antpath.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import (absolute_import, division, print_function)
4+
5+
import json
6+
7+
from branca.element import Figure, JavascriptLink
8+
9+
from folium import Marker
10+
from folium.vector_layers import path_options
11+
12+
from jinja2 import Template
13+
14+
15+
class AntPath(Marker):
16+
"""
17+
Class for drawing AntPath polyline overlays on a map.
18+
19+
See :func:`folium.vector_layers.path_options` for the `Path` options.
20+
21+
Parameters
22+
----------
23+
locations: list of points (latitude, longitude)
24+
Latitude and Longitude of line (Northing, Easting)
25+
popup: str or folium.Popup, default None
26+
Input text or visualization for object displayed when clicking.
27+
tooltip: str or folium.Tooltip, optional
28+
Display a text when hovering over the object.
29+
**kwargs:
30+
Polyline and AntPath options. See their Github page for the
31+
available parameters.
32+
33+
https://github.com/rubenspgcavalcante/leaflet-ant-path/
34+
35+
"""
36+
_template = Template(u"""
37+
{% macro script(this, kwargs) %}
38+
{{this.get_name()}} = L.polyline.antPath(
39+
{{this.location}},
40+
{{ this.options }}
41+
)
42+
.addTo({{this._parent.get_name()}});
43+
{% endmacro %}
44+
""") # noqa
45+
46+
def __init__(self, locations, popup=None, tooltip=None, **kwargs):
47+
super(AntPath, self).__init__(
48+
location=locations,
49+
popup=popup,
50+
tooltip=tooltip,
51+
)
52+
53+
self._name = 'AntPath'
54+
# Polyline + AntPath defaults.
55+
options = path_options(line=True, **kwargs)
56+
options.update({
57+
'paused': kwargs.pop('paused', False),
58+
'reverse': kwargs.pop('reverse', False),
59+
'hardwareAcceleration': kwargs.pop('hardware_acceleration', False),
60+
'delay': kwargs.pop('delay', 400),
61+
'dashArray': kwargs.pop('dash_array', [10, 20]),
62+
'weight': kwargs.pop('weight', 5),
63+
'opacity': kwargs.pop('opacity', 0.5),
64+
'color': kwargs.pop('color', '#0000FF'),
65+
'pulseColor': kwargs.pop('pulse_color', '#FFFFFF'),
66+
})
67+
self.options = json.dumps(options, sort_keys=True, indent=2)
68+
69+
def render(self, **kwargs):
70+
super(AntPath, self).render()
71+
72+
figure = self.get_root()
73+
assert isinstance(figure, Figure), ('You cannot render this Element '
74+
'if it is not in a Figure.')
75+
76+
figure.header.add_child(
77+
JavascriptLink('https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet-ant-path.min.js'), # noqa
78+
name='antpath',
79+
)

folium/utilities.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ def iter_points(x):
389389
return []
390390

391391

392+
def compare_rendered(obj1, obj2):
393+
"""
394+
Return True/False if the normalized rendered version of
395+
two folium map objects are the equal or not.
396+
397+
"""
398+
return _normalize(obj1) == _normalize(obj2)
399+
400+
401+
def _normalize(rendered):
402+
"""Return the input string as a list of stripped lines."""
403+
return [line.strip() for line in rendered.splitlines() if line.strip()]
404+
405+
392406
@contextmanager
393407
def _tmp_html(data):
394408
"""Yields the path of a temporary HTML file containing data."""

0 commit comments

Comments
 (0)