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
1601 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
"""
47
var {{ this.get_name() }} = (function(){
48
{{ this.callback }}
49
50
var data = {{ this.data|tojson }};
51
var cluster = L.markerClusterGroup({{ this.options|tojavascript }});
52
{%- if this.icon_create_function is not none %}
53
cluster.options.iconCreateFunction =
54
{{ this.icon_create_function.strip() }};
55
{%- endif %}
56
57
for (var i = 0; i < data.length; i++) {
58
var row = data[i];
59
var marker = callback(row);
60
marker.addTo(cluster);
61
}
62
63
cluster.addTo({{ this._parent.get_name() }});
64
return cluster;
65
})();
66
"""
67
)
68
expected = normalize(tmpl.render(this=mc))
69
assert expected in out
70
71
72
@pytest.mark.parametrize(
73
"case",
74
[
75
np.array([[0, 5, 1], [1, 6, 1], [2, 7, 0.5]]),
76
[[0, 5, "red"], (1, 6, "blue"), [2, 7, {"this": "also works"}]],
77
pd.DataFrame(
78
[[0, 5, "red"], [1, 6, "blue"], [2, 7, "something"]],
79
columns=["lat", "lng", "color"],
80
),
81
],
82
)
83
def test_fast_marker_cluster_data(case):
84
data = FastMarkerCluster(case).data
85
assert isinstance(data, list)
86
assert len(data) == 3
87
for i in range(len(data)):
88
assert isinstance(data[i], list)
89
assert len(data[i]) == 3
90
assert data[i][0] == float(i)
91
assert data[i][1] == float(i + 5)
92
93