Skip to content

Commit 859d7b8

Browse files
author
Martin Journois
committed
Fix test_marker_cluster
1 parent a6a0c3c commit 859d7b8

2 files changed

Lines changed: 57 additions & 39 deletions

File tree

folium/plugins/marker_cluster.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,66 @@
55
66
Creates a MarkerCluster plugin to add on a folium map.
77
"""
8-
import json
8+
#import json
9+
from jinja2 import Template
910

10-
from .plugin import Plugin
11+
from folium.element import JavascriptLink, CssLink, MacroElement, Figure
12+
from folium.map import Popup, Icon, Marker
1113

12-
class MarkerCluster(Plugin):
13-
"""Adds a MarkerCluster layer on the map."""
14-
def __init__(self, data):
14+
class MarkerCluster(MacroElement):
15+
def __init__(self, locations, popups=None, icons=None):
1516
"""Creates a MarkerCluster plugin to append into a map with
16-
Map.add_plugin.
17+
Map.add_children.
1718
1819
Parameters
1920
----------
20-
data: list of list or array of shape (n,3).
21-
Data points of the form [[lat, lng, popup]].
21+
locations: list of list or array of shape (n,2).
22+
Data points of the form [[lat, lng]].
23+
24+
popups: list of length n.
25+
Popup for each marker.
26+
27+
icons: list of length n.
28+
Icon for each marker.
2229
"""
2330
super(MarkerCluster, self).__init__()
24-
self.plugin_name = 'MarkerCluster'
25-
self.data = [tuple(x) for x in data]
26-
27-
def render_header(self, nb):
28-
"""Generates the HTML part of the plugin."""
29-
return """
30-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css" />
31-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css" />
32-
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"></script>
33-
""" if nb==0 else ""
34-
35-
def render_js(self, nb):
36-
"""Generates the Javascript part of the plugin."""
37-
out = """
38-
var addressPoints = """+json.dumps(self.data)+""";
39-
40-
var markers = L.markerClusterGroup();
41-
42-
for (var i = 0; i < addressPoints.length; i++) {
43-
var a = addressPoints[i];
44-
var title = a[2];
45-
var marker = L.marker(new L.LatLng(a[0], a[1]), { title: title });
46-
marker.bindPopup(title);
47-
markers.addLayer(marker);
48-
}
49-
50-
map.addLayer(markers);
51-
"""
52-
return out
31+
self._name = 'MarkerCluster'
32+
33+
if popups is None:
34+
popups = [None]*len(locations)
35+
if icons is None:
36+
icons = [None]*len(locations)
37+
38+
for location, popup, icon in zip(locations,popups,icons):
39+
if popup is None or isinstance(popup, Popup):
40+
p = popup
41+
else:
42+
p = Popup(popup)
43+
if icon is None or isinstance(icon, Icon):
44+
i = icon
45+
else:
46+
i = Icon(icon)
47+
self.add_children(Marker(location, popup=p, icon=i))
48+
49+
self._template = Template(u"""
50+
{% macro script(this, kwargs) %}
51+
var {{this.get_name()}} = L.markerClusterGroup();
52+
{{this._parent.get_name()}}.addLayer({{this.get_name()}});
53+
{% endmacro %}
54+
""")
55+
def render(self,**kwargs):
56+
super(MarkerCluster,self).render(**kwargs)
57+
58+
figure = self.get_root()
59+
assert isinstance(figure,Figure), ("You cannot render this Element "
60+
"if it's not in a Figure.")
61+
62+
figure.header.add_children(\
63+
JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"),
64+
name='markerclusterjs')
65+
figure.header.add_children(\
66+
CssLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css"),
67+
name='markerclustercss')
68+
figure.header.add_children(\
69+
CssLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css"),
70+
name='markerclusterdefaultcss')

tests/test_plugins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def test_marker_cluster(self):
2828
range(N), # Popups.
2929
]).T
3030
mapa = folium.Map([45., 3.], zoom_start=4)
31-
mapa.add_plugin(plugins.MarkerCluster(data))
32-
mapa._build_map()
31+
mapa.add_children(plugins.MarkerCluster(data))
32+
mapa._repr_html_()
3333

3434
def test_terminator(self):
3535
mapa = folium.Map([45., 3.], zoom_start=1)

0 commit comments

Comments
 (0)