Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_heat_map.py
2542 views
1
"""
2
Test HeatMap
3
------------
4
"""
5
6
import numpy as np
7
import pytest
8
9
import folium
10
from folium.plugins import HeatMap
11
from folium.template import Template
12
from folium.utilities import normalize
13
14
15
def test_heat_map():
16
np.random.seed(3141592)
17
data = np.random.normal(size=(100, 2)) * np.array([[1, 1]]) + np.array([[48, 5]])
18
m = folium.Map([48.0, 5.0], zoom_start=6)
19
hm = HeatMap(data)
20
m.add_child(hm)
21
m._repr_html_()
22
23
out = normalize(m._parent.render())
24
25
# We verify that the script import is present.
26
script = '<script src="https://cdn.jsdelivr.net/gh/python-visualization/folium@main/folium/templates/leaflet_heat.min.js"></script>' # noqa
27
assert script in out
28
29
# We verify that the script part is correct.
30
tmpl = Template("""
31
var {{this.get_name()}} = L.heatLayer(
32
{{this.data}},
33
{
34
minOpacity: {{this.min_opacity}},
35
maxZoom: {{this.max_zoom}},
36
radius: {{this.radius}},
37
blur: {{this.blur}},
38
gradient: {{this.gradient}}
39
})
40
.addTo({{this._parent.get_name()}});
41
""")
42
43
assert tmpl.render(this=hm)
44
45
bounds = m.get_bounds()
46
np.testing.assert_allclose(
47
bounds,
48
[
49
[46.218566840847025, 3.0302801394447734],
50
[50.75345011431167, 7.132453997672826],
51
],
52
)
53
54
55
def test_heatmap_data():
56
data = HeatMap(np.array([[3, 4, 1], [5, 6, 1], [7, 8, 0.5]])).data
57
assert isinstance(data, list)
58
assert len(data) == 3
59
for i in range(len(data)):
60
assert isinstance(data[i], list)
61
assert len(data[i]) == 3
62
63
64
def test_heat_map_exception():
65
with pytest.raises(ValueError):
66
HeatMap(np.array([[4, 5, 1], [3, 6, np.nan]]))
67
with pytest.raises(Exception):
68
HeatMap(np.array([3, 4, 5]))
69
70