Skip to content

Commit aa6260d

Browse files
ENH: support xyzservices (#1498)
* ENH: support xyzservices * lint * always test xyzservices Co-authored-by: Frank <[email protected]>
1 parent a6f29c6 commit aa6260d

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

docs/conf.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727

2828
# Add any Sphinx extension module names here, as strings. They can be extensions
2929
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
30-
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'nbsphinx']
30+
extensions = [
31+
'sphinx.ext.autodoc',
32+
'sphinx.ext.napoleon',
33+
'nbsphinx',
34+
'sphinx.ext.intersphinx',
35+
]
3136

3237
# Add any paths that contain templates here, relative to this directory.
3338
templates_path = ['_templates']
@@ -249,4 +254,11 @@
249254
# Ignore tile URLs
250255
linkcheck_ignore = [
251256
r"https://free.*",
252-
]
257+
]
258+
259+
intersphinx_mapping = {
260+
"xyzservices": (
261+
"https://xyzservices.readthedocs.io/en/latest/",
262+
"https://xyzservices.readthedocs.io/en/latest/objects.inv",
263+
),
264+
}

folium/folium.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class Map(JSCSSMixin, MacroElement):
8181
- "Mapbox" (Must pass API key)
8282
- "CartoDB" (positron and dark_matter)
8383
84-
You can pass a custom tileset to Folium by passing a Leaflet-style
84+
You can pass a custom tileset to Folium by passing a
85+
:class:`xyzservices.TileProvider` or a Leaflet-style
8586
URL to the tiles parameter: ``http://{s}.yourtiles.com/{z}/{x}/{y}.png``.
8687
8788
You can find a list of free tile providers here:
@@ -97,8 +98,9 @@ class Map(JSCSSMixin, MacroElement):
9798
Width of the map.
9899
height: pixel int or percentage string (default: '100%')
99100
Height of the map.
100-
tiles: str or TileLayer, default 'OpenStreetMap'
101+
tiles: str or TileLayer or :class:`xyzservices.TileProvider`, default 'OpenStreetMap'
101102
Map tileset to use. Can choose from a list of built-in tiles,
103+
pass a :class:`xyzservices.TileProvider`,
102104
pass a custom URL, pass a TileLayer object,
103105
or pass `None` to create a map without tiles.
104106
For more advanced tile layer options, use the `TileLayer` class.

folium/raster_layers.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class TileLayer(Layer):
2020
2121
Parameters
2222
----------
23-
tiles: str, default 'OpenStreetMap'
23+
tiles: str or :class:`xyzservices.TileProvider`, default 'OpenStreetMap'
2424
Map tileset to use. Can choose from this list of built-in tiles:
2525
- "OpenStreetMap"
2626
- "Stamen Terrain", "Stamen Toner", "Stamen Watercolor"
2727
- "CartoDB positron", "CartoDB dark_matter"
2828
29-
You can pass a custom tileset to Folium by passing a Leaflet-style
29+
You can pass a custom tileset to Folium by passing a
30+
:class:`xyzservices.TileProvider` or a Leaflet-style
3031
URL to the tiles parameter: ``http://{s}.yourtiles.com/{z}/{x}/{y}.png``.
3132
3233
You can find a list of free tile providers here:
@@ -80,6 +81,14 @@ def __init__(self, tiles='OpenStreetMap', min_zoom=0, max_zoom=18,
8081
control=True, show=True, no_wrap=False, subdomains='abc',
8182
tms=False, opacity=1, **kwargs):
8283

84+
# check for xyzservices.TileProvider without importing it
85+
if isinstance(tiles, dict):
86+
attr = attr if attr else tiles.html_attribution
87+
min_zoom = tiles.get("min_zoom", min_zoom)
88+
max_zoom = tiles.get("max_zoom", max_zoom)
89+
subdomains = tiles.get("subdomains", subdomains)
90+
tiles = tiles.build_url(fill_subdomain=False, scale_factor="{r}")
91+
8392
self.tile_name = (name if name is not None else
8493
''.join(tiles.lower().strip().split()))
8594
super(TileLayer, self).__init__(name=self.tile_name, overlay=overlay,

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ twine>=3.2.0
3333
vega_datasets
3434
vincent
3535
wheel
36+
xyzservices

tests/test_raster_layers.py

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from jinja2 import Template
1010

11+
import xyzservices
12+
1113

1214
def test_tile_layer():
1315
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
@@ -106,3 +108,20 @@ def test_image_overlay():
106108

107109
bounds = m.get_bounds()
108110
assert bounds == [[0, -180], [90, 180]], bounds
111+
112+
113+
def test_xyzservices():
114+
m = folium.Map([48., 5.], tiles=xyzservices.providers.Stamen.Toner, zoom_start=6)
115+
116+
folium.raster_layers.TileLayer(
117+
tiles=xyzservices.providers.Stamen.Terrain,
118+
).add_to(m)
119+
folium.LayerControl().add_to(m)
120+
121+
out = m._parent.render()
122+
assert xyzservices.providers.Stamen.Toner.build_url(
123+
fill_subdomain=False, scale_factor="{r}"
124+
) in out
125+
assert xyzservices.providers.Stamen.Terrain.build_url(
126+
fill_subdomain=False, scale_factor="{r}"
127+
) in out

0 commit comments

Comments
 (0)