Skip to content

Commit 8d5dd69

Browse files
author
Martin Journois
committed
Fix test_boat_marker
1 parent 75cac11 commit 8d5dd69

2 files changed

Lines changed: 27 additions & 28 deletions

File tree

folium/plugins/boat_marker.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
Creates a marker shaped like a boat. Optionally you can append a wind direction.
77
"""
88
import json
9+
from jinja2 import Template
910

10-
from .plugin import Plugin
11+
from folium.element import JavascriptLink, MacroElement, Figure
1112

12-
class BoatMarker(Plugin):
13+
class BoatMarker(MacroElement):
1314
"""Adds a BoatMarker layer on the map."""
1415
def __init__(self, position=None, heading=0, wind_heading=None, wind_speed=0, **kwargs):
1516
"""Creates a BoatMarker plugin to append into a map with
@@ -32,30 +33,28 @@ def __init__(self, position=None, heading=0, wind_heading=None, wind_speed=0, **
3233
Speed of the wind in knots.
3334
"""
3435
super(BoatMarker, self).__init__()
35-
self.plugin_name = 'BoatMarker'
36+
self._name = 'BoatMarker'
3637
self.position = None if position is None else tuple(position)
3738
self.heading = heading
3839
self.wind_heading = wind_heading
3940
self.wind_speed = wind_speed
40-
self.kwargs = kwargs.copy()
41-
42-
def render_header(self, nb):
43-
"""Generates the HTML part of the plugin."""
44-
return """
45-
<script src="https://thomasbrueggemann.github.io/leaflet.boatmarker/js/leaflet.boatmarker.min.js"></script>
46-
""" if nb==0 else ""
47-
48-
def render_js(self, nb):
49-
"""Generates the Javascript part of the plugin."""
50-
kwargs_str = "{%s}" % ",".join(["%s : %s" % (key,json.dumps(val)) for (key,val) in self.kwargs.items()])
51-
position_str = "map.getCenter()" if self.position is None else "[%.12f,%.12f]"%self.position
52-
out = 'var boatMarker_%s = L.boatMarker(%s, %s).addTo(map);' % (nb,position_str,kwargs_str)
53-
54-
if self.wind_heading is None:
55-
out += "boatMarker_%s.setHeading(%s);" % (nb,int(self.heading))
56-
else:
57-
out += "boatMarker_%s.setHeadingWind(%s, %s, %s);"%(nb,int(self.heading),
58-
int(self.wind_speed),
59-
int(self.wind_heading),
60-
)
61-
return out
41+
self.kwargs = json.dumps(kwargs)
42+
43+
self._template = Template(u"""
44+
{% macro script(this, kwargs) %}
45+
var {{this.get_name()}} = L.boatMarker(
46+
[{{this.position[0]}},{{this.position[1]}}],
47+
{{this.kwargs}}).addTo({{this._parent.get_name()}});
48+
{{this.get_name()}}.setHeadingWind({{this.heading}}, {{this.wind_speed}}, {{this.wind_heading}});
49+
{% endmacro %}
50+
""")
51+
def render(self,**kwargs):
52+
super(BoatMarker,self).render(**kwargs)
53+
54+
figure = self.get_root()
55+
assert isinstance(figure,Figure), ("You cannot render this Element "
56+
"if it's not in a Figure.")
57+
58+
figure.header.add_children(\
59+
JavascriptLink("https://thomasbrueggemann.github.io/leaflet.boatmarker/js/leaflet.boatmarker.min.js"),
60+
name='markerclusterjs')

tests/test_plugins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ def test_terminator(self):
3939

4040
def test_boat_marker(self):
4141
mapa = folium.Map([30., 0.], zoom_start=3)
42-
mapa.add_plugin(plugins.BoatMarker((34, -43),
42+
mapa.add_children(plugins.BoatMarker((34, -43),
4343
heading=45,
4444
wind_heading=150,
4545
wind_speed=45,
4646
color="#8f8"))
47-
mapa.add_plugin(plugins.BoatMarker((46, -30),
47+
mapa.add_children(plugins.BoatMarker((46, -30),
4848
heading=-20,
4949
wind_heading=46,
5050
wind_speed=25,
5151
color="#88f"))
52-
mapa._build_map()
52+
mapa._repr_html_()
5353

5454
def test_layer(self):
5555
mapa = folium.Map([48., 5.], zoom_start=6)

0 commit comments

Comments
 (0)