Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_marker_cluster.py
2542 views
1
"""
2
Test MarkerCluster
3
------------------
4
"""
5
6
import numpy as np
7
8
import folium
9
from folium import plugins
10
from folium.template import Template
11
from folium.utilities import normalize
12
13
14
def test_marker_cluster():
15
N = 100
16
np.random.seed(seed=26082009)
17
data = np.array(
18
[
19
np.random.uniform(low=35, high=60, size=N), # Random latitudes.
20
np.random.uniform(low=-12, high=30, size=N), # Random longitudes.
21
]
22
).T
23
m = folium.Map([45.0, 3.0], zoom_start=4)
24
mc = plugins.MarkerCluster(data).add_to(m)
25
26
tmpl_for_expected = Template("""
27
var {{this.get_name()}} = L.markerClusterGroup(
28
{{ this.options|tojavascript }}
29
);
30
{%- if this.icon_create_function is not none %}
31
{{ this.get_name() }}.options.iconCreateFunction =
32
{{ this.icon_create_function.strip() }};
33
{%- endif %}
34
35
{% for marker in this._children.values() %}
36
var {{marker.get_name()}} = L.marker(
37
{{ marker.location|tojson }}, {}
38
).addTo({{this.get_name()}});
39
{% endfor %}
40
41
{{ this.get_name() }}.addTo({{ this._parent.get_name() }});
42
""")
43
expected = normalize(tmpl_for_expected.render(this=mc))
44
45
out = normalize(m._parent.render())
46
47
# We verify that imports
48
assert (
49
'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/leaflet.markercluster.js"></script>' # noqa
50
in out
51
) # noqa
52
assert (
53
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/MarkerCluster.css"/>' # noqa
54
in out
55
) # noqa
56
assert (
57
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/MarkerCluster.Default.css"/>' # noqa
58
in out
59
) # noqa
60
61
assert expected in out
62
63
bounds = m.get_bounds()
64
np.testing.assert_allclose(
65
bounds,
66
[
67
[35.147332572663785, -11.520684337300109],
68
[59.839718052359274, 29.94931046497927],
69
],
70
)
71
72