Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_vectorgrid_protobuf.py
1601 views
1
"""
2
Test VectorGridProtobuf
3
---------------
4
"""
5
6
import json
7
8
import folium
9
from folium.plugins import VectorGridProtobuf
10
from folium.utilities import normalize
11
12
13
def test_vectorgrid():
14
m = folium.Map(location=(30, 20), zoom_start=4)
15
url = "https://free-{s}.tilehosting.com/data/v3/{z}/{x}/{y}.pbf?token={token}"
16
vc = VectorGridProtobuf(url, "test").add_to(m)
17
out = normalize(m._parent.render())
18
19
expected = normalize(VectorGridProtobuf._template.render(this=vc))
20
assert expected in out
21
22
script = f'<script src="{VectorGridProtobuf.default_js[0][1]}"></script>'
23
assert script in out
24
assert url in out
25
assert "L.vectorGrid.protobuf" in out
26
27
28
def test_vectorgrid_str_options():
29
m = folium.Map(location=(30, 20), zoom_start=4)
30
url = "https://free-{s}.tilehosting.com/data/v3/{z}/{x}/{y}.pbf?token={token}"
31
options = """{
32
"subdomain": "test",
33
"token": "test_token",
34
"vectorTileLayerStyles": {
35
"all": {
36
"fill": true,
37
"weight": 1,
38
"fillColor": "green",
39
"color": "black",
40
"fillOpacity": 0.6,
41
"opacity": 0.6
42
}
43
}
44
}"""
45
46
vc = VectorGridProtobuf(url, "test", options)
47
m.add_child(vc)
48
49
dict_options = json.loads(options)
50
51
out = normalize(m._parent.render())
52
script = f'<script src="{VectorGridProtobuf.default_js[0][1]}"></script>'
53
54
assert script in out
55
assert url in out
56
assert "L.vectorGrid.protobuf" in out
57
assert '"token": "test_token"' in out
58
assert '"subdomain": "test"' in out
59
60
for k, v in dict_options["vectorTileLayerStyles"]["all"].items():
61
if type(v) == bool:
62
assert f'"{k}": {str(v).lower()}' in out
63
continue
64
if type(v) == str:
65
assert f'"{k}": "{v}"' in out
66
continue
67
68
assert f'"{k}": {v}' in out
69
70
71
def test_vectorgrid_dict_options():
72
m = folium.Map(location=(30, 20), zoom_start=4)
73
url = "https://free-{s}.tilehosting.com/data/v3/{z}/{x}/{y}.pbf?token={token}"
74
options = {
75
"subdomain": "test",
76
"token": "test_token",
77
"vectorTileLayerStyles": {
78
"all": {
79
"fill": True,
80
"weight": 1,
81
"fillColor": "grey",
82
"color": "purple",
83
"fillOpacity": 0.3,
84
"opacity": 0.6,
85
}
86
},
87
}
88
89
vc = VectorGridProtobuf(url, "test", options)
90
m.add_child(vc)
91
92
out = normalize(m._parent.render())
93
script = f'<script src="{VectorGridProtobuf.default_js[0][1]}"></script>'
94
95
assert script in out
96
assert url in out
97
assert "L.vectorGrid.protobuf" in out
98
assert '"token": "test_token"' in out
99
assert '"subdomain": "test"' in out
100
101
for k, v in options["vectorTileLayerStyles"]["all"].items():
102
if type(v) == bool:
103
assert f'"{k}": {str(v).lower()}' in out
104
continue
105
if type(v) == str:
106
assert f'"{k}": "{v}"' in out
107
continue
108
109
assert f'"{k}": {v}' in out
110
111