Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_overlapping_marker_spiderfier.py
1601 views
1
"""
2
Test OverlappingMarkerSpiderfier
3
--------------------------------
4
"""
5
6
import numpy as np
7
8
from folium.folium import Map
9
from folium.map import Marker
10
from folium.plugins.overlapping_marker_spiderfier import OverlappingMarkerSpiderfier
11
12
13
def test_oms_js_inclusion():
14
"""
15
Test that the OverlappingMarkerSpiderfier JavaScript library is included in the map.
16
"""
17
m = Map([45.05, 3.05], zoom_start=14)
18
OverlappingMarkerSpiderfier().add_to(m)
19
20
rendered_map = m._parent.render()
21
assert (
22
'<script src="https://cdnjs.cloudflare.com/ajax/libs/OverlappingMarkerSpiderfier-Leaflet/0.2.6/oms.min.js"></script>'
23
in rendered_map
24
), "OverlappingMarkerSpiderfier JS file is missing in the rendered output."
25
26
27
def test_marker_addition():
28
"""
29
Test that markers are correctly added to the map.
30
"""
31
N = 10
32
np.random.seed(seed=26082009)
33
data = np.array(
34
[
35
np.random.uniform(low=45.0, high=45.1, size=N),
36
np.random.uniform(low=3.0, high=3.1, size=N),
37
]
38
).T
39
40
m = Map([45.05, 3.05], zoom_start=14)
41
markers = [
42
Marker(
43
location=loc,
44
popup=f"Marker {i}",
45
)
46
for i, loc in enumerate(data)
47
]
48
49
for marker in markers:
50
marker.add_to(m)
51
52
assert (
53
len(m._children) == len(markers) + 1
54
), f"Expected {len(markers)} markers, found {len(m._children) - 1}."
55
56
57
def test_map_bounds():
58
"""
59
Test that the map bounds correctly encompass all added markers.
60
"""
61
N = 10
62
np.random.seed(seed=26082009)
63
data = np.array(
64
[
65
np.random.uniform(low=45.0, high=45.1, size=N),
66
np.random.uniform(low=3.0, high=3.1, size=N),
67
]
68
).T
69
70
m = Map([45.05, 3.05], zoom_start=14)
71
markers = [
72
Marker(
73
location=loc,
74
popup=f"Marker {i}",
75
)
76
for i, loc in enumerate(data)
77
]
78
79
for marker in markers:
80
marker.add_to(m)
81
82
bounds = m.get_bounds()
83
assert bounds is not None, "Map bounds should not be None"
84
85
min_lat, min_lon = data.min(axis=0)
86
max_lat, max_lon = data.max(axis=0)
87
88
assert (
89
bounds[0][0] <= min_lat
90
), "Map bounds do not correctly include the minimum latitude."
91
assert (
92
bounds[0][1] <= min_lon
93
), "Map bounds do not correctly include the minimum longitude."
94
assert (
95
bounds[1][0] >= max_lat
96
), "Map bounds do not correctly include the maximum latitude."
97
assert (
98
bounds[1][1] >= max_lon
99
), "Map bounds do not correctly include the maximum longitude."
100
101
102
def test_overlapping_marker_spiderfier_integration():
103
"""
104
Test that OverlappingMarkerSpiderfier integrates correctly with the map.
105
"""
106
m = Map([45.05, 3.05], zoom_start=14)
107
oms = OverlappingMarkerSpiderfier(
108
keep_spiderfied=True,
109
nearby_distance=20,
110
)
111
oms.add_to(m)
112
113
assert (
114
oms.get_name() in m._children
115
), "OverlappingMarkerSpiderfier is not correctly added to the map."
116
117