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