/
test_raster_layers.py
135 lines (108 loc) · 3.54 KB
/
test_raster_layers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Test raster_layers
------------------
"""
import xyzservices
from jinja2 import Template
import folium
from folium.utilities import normalize
def test_tile_layer():
m = folium.Map([48.0, 5.0], tiles="stamentoner", zoom_start=6)
layer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
folium.raster_layers.TileLayer(
tiles=layer, name="OpenStreetMap", attr="attribution"
).add_to(m)
folium.raster_layers.TileLayer(
tiles=layer, name="OpenStreetMap2", attr="attribution2", overlay=True
).add_to(m)
folium.LayerControl().add_to(m)
m._repr_html_()
bounds = m.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
def _is_working_zoom_level(zoom, tiles, session):
"""Check if the zoom level works for the given tileset."""
url = tiles.format(s="a", x=0, y=0, z=zoom)
response = session.get(url, timeout=5)
if response.status_code < 400:
return True
return False
def test_custom_tile_subdomains():
"""Test custom tile subdomains."""
url = "http://{s}.custom_tiles.org/{z}/{x}/{y}.png"
m = folium.Map()
folium.TileLayer(
tiles=url, name="subdomains2", attr="attribution", subdomains="mytilesubdomain"
).add_to(m)
out = m._parent.render()
assert "mytilesubdomain" in out
def test_wms():
m = folium.Map([40, -100], zoom_start=4)
url = "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi"
w = folium.raster_layers.WmsTileLayer(
url=url,
name="test",
fmt="image/png",
layers="nexrad-n0r-900913",
attr="Weather data © 2012 IEM Nexrad",
transparent=True,
cql_filter="something",
)
w.add_to(m)
html = m.get_root().render()
# verify this special case wasn't converted to lowerCamelCase
assert '"cql_filter": "something",' in html
assert "cqlFilter" not in html
bounds = m.get_bounds()
assert bounds == [[None, None], [None, None]], bounds
def test_image_overlay():
"""Test image overlay."""
data = [
[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[1, 1, 0, 0.5], [0, 0, 1, 1], [0, 0, 1, 1]],
]
m = folium.Map()
io = folium.raster_layers.ImageOverlay(
data, [[0, -180], [90, 180]], mercator_project=True
)
io.add_to(m)
m._repr_html_()
out = m._parent.render()
# Verify the URL generation.
url = (
"data:image/png;base64,"
"iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAYAAACddGYaAAA"
"AF0lEQVR42mP4z8AARFDw/z/DeiA5H4QBV60H6ABl9ZIAAAAASUVORK5CYII="
)
assert io.url == url
# Verify the script part is okay.
tmpl = Template(
"""
var {{this.get_name()}} = L.imageOverlay(
"{{ this.url }}",
{{ this.bounds }},
{{ this.options }}
).addTo({{this._parent.get_name()}});
"""
)
assert normalize(tmpl.render(this=io)) in normalize(out)
bounds = m.get_bounds()
assert bounds == [[0, -180], [90, 180]], bounds
def test_xyzservices():
m = folium.Map([48.0, 5.0], tiles=xyzservices.providers.Stamen.Toner, zoom_start=6)
folium.raster_layers.TileLayer(
tiles=xyzservices.providers.Stamen.Terrain,
).add_to(m)
folium.LayerControl().add_to(m)
out = m._parent.render()
assert (
xyzservices.providers.Stamen.Toner.build_url(
fill_subdomain=False, scale_factor="{r}"
)
in out
)
assert (
xyzservices.providers.Stamen.Terrain.build_url(
fill_subdomain=False, scale_factor="{r}"
)
in out
)