Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_fast_marker_cluster.py
2538 views
1
"""
2
Test FastMarkerCluster
3
----------------------
4
"""
5
6
import numpy as np
7
import pandas as pd
8
import pytest
9
10
import folium
11
from folium.plugins import FastMarkerCluster
12
from folium.template import Template
13
from folium.utilities import normalize
14
15
16
def test_fast_marker_cluster():
17
n = 100
18
np.random.seed(seed=26082009)
19
data = np.array(
20
[
21
np.random.uniform(low=35, high=60, size=n),
22
np.random.uniform(low=-12, high=30, size=n),
23
]
24
).T
25
m = folium.Map([45.0, 3.0], zoom_start=4)
26
mc = FastMarkerCluster(data).add_to(m)
27
28
out = normalize(m._parent.render())
29
30
# We verify that imports
31
assert (
32
'<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/leaflet.markercluster.js"></script>' # noqa
33
in out
34
) # noqa
35
assert (
36
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/MarkerCluster.css"/>' # noqa
37
in out
38
) # noqa
39
assert (
40
'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.1.0/MarkerCluster.Default.css"/>' # noqa
41
in out
42
) # noqa
43
44
# Verify the script part is okay.
45
tmpl = Template("""
46
var {{ this.get_name() }} = (function(){
47
{{ this.callback }}
48
49
var data = {{ this.data|tojson }};
50
var cluster = L.markerClusterGroup({{ this.options|tojavascript }});
51
{%- if this.icon_create_function is not none %}
52
cluster.options.iconCreateFunction =
53
{{ this.icon_create_function.strip() }};
54
{%- endif %}
55
56
for (var i = 0; i < data.length; i++) {
57
var row = data[i];
58
var marker = callback(row);
59
marker.addTo(cluster);
60
}
61
62
cluster.addTo({{ this._parent.get_name() }});
63
return cluster;
64
})();
65
""")
66
expected = normalize(tmpl.render(this=mc))
67
assert expected in out
68
69
70
@pytest.mark.parametrize(
71
"case",
72
[
73
np.array([[0, 5, 1], [1, 6, 1], [2, 7, 0.5]]),
74
[[0, 5, "red"], (1, 6, "blue"), [2, 7, {"this": "also works"}]],
75
pd.DataFrame(
76
[[0, 5, "red"], [1, 6, "blue"], [2, 7, "something"]],
77
columns=["lat", "lng", "color"],
78
),
79
],
80
)
81
def test_fast_marker_cluster_data(case):
82
data = FastMarkerCluster(case).data
83
assert isinstance(data, list)
84
assert len(data) == 3
85
for i in range(len(data)):
86
assert isinstance(data[i], list)
87
assert len(data[i]) == 3
88
assert data[i][0] == float(i)
89
assert data[i][1] == float(i + 5)
90
91