Path: blob/main/tests/test_raster_layers.py
1593 views
"""1Test raster_layers2------------------34"""56import pytest7import xyzservices89import folium10from folium.template import Template11from folium.utilities import normalize121314def test_tile_layer():15m = folium.Map([48.0, 5.0], zoom_start=6)16layer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"1718folium.raster_layers.TileLayer(19tiles=layer, name="OpenStreetMap", attr="attribution"20).add_to(m)2122folium.raster_layers.TileLayer(23tiles=layer, name="OpenStreetMap2", attr="attribution2", overlay=True24).add_to(m)2526folium.LayerControl().add_to(m)27m._repr_html_()2829bounds = m.get_bounds()30assert bounds == [[None, None], [None, None]], bounds313233def _is_working_zoom_level(zoom, tiles, session):34"""Check if the zoom level works for the given tileset."""35url = tiles.format(s="a", x=0, y=0, z=zoom)36response = session.get(url, timeout=5)37if response.status_code < 400:38return True39return False404142def test_custom_tile_subdomains():43"""Test custom tile subdomains."""44url = "http://{s}.custom_tiles.org/{z}/{x}/{y}.png"45m = folium.Map()46folium.TileLayer(47tiles=url, name="subdomains2", attr="attribution", subdomains="mytilesubdomain"48).add_to(m)49out = m._parent.render()50assert "mytilesubdomain" in out515253def test_wms():54m = folium.Map([40, -100], zoom_start=4)55url = "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi"56w = folium.raster_layers.WmsTileLayer(57url=url,58name="test",59fmt="image/png",60layers="nexrad-n0r-900913",61attr="Weather data © 2012 IEM Nexrad",62transparent=True,63cql_filter="something",64)65w.add_to(m)66html = m.get_root().render()6768# verify this special case wasn't converted to lowerCamelCase69assert '"cql_filter": "something",' in html70assert "cqlFilter" not in html7172bounds = m.get_bounds()73assert bounds == [[None, None], [None, None]], bounds747576def test_image_overlay():77"""Test image overlay."""78data = [79[[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0]],80[[1, 1, 0, 0.5], [0, 0, 1, 1], [0, 0, 1, 1]],81]8283m = folium.Map()84io = folium.raster_layers.ImageOverlay(85data, [[0, -180], [90, 180]], mercator_project=True86)87io.add_to(m)88m._repr_html_()8990out = m._parent.render()9192# Verify the URL generation.93url = (94"data:image/png;base64,"95"iVBORw0KGgoAAAANSUhEUgAAAAMAAAACCAYAAACddGYaAAA"96"AF0lEQVR42mP4z8AARFDw/z/DeiA5H4QBV60H6ABl9ZIAAAAASUVORK5CYII="97)98assert io.url == url99100# Verify the script part is okay.101tmpl = Template(102"""103var {{this.get_name()}} = L.imageOverlay(104"{{ this.url }}",105{{ this.bounds }},106{{ this.options }}107);108{{ this.get_name() }}.addTo({{this._parent.get_name()}});109"""110)111assert normalize(tmpl.render(this=io)) in normalize(out)112113bounds = m.get_bounds()114assert bounds == [[0, -180], [90, 180]], bounds115116117@pytest.mark.parametrize(118"tiles", ["CartoDB DarkMatter", xyzservices.providers.CartoDB.DarkMatter]119)120def test_xyzservices(tiles):121m = folium.Map([48.0, 5.0], tiles=tiles, zoom_start=6)122123folium.raster_layers.TileLayer(124tiles=xyzservices.providers.CartoDB.Positron,125).add_to(m)126folium.LayerControl().add_to(m)127128out = m._parent.render()129assert (130xyzservices.providers.CartoDB.DarkMatter.build_url(131fill_subdomain=False, scale_factor="{r}"132)133in out134)135assert (136xyzservices.providers.CartoDB.Positron.build_url(137fill_subdomain=False, scale_factor="{r}"138)139in out140)141142143