I’m wondering if it’s possible to switch off all Folium tiles that have been added to a Folium Map object. By default one tile should be selected, but could be displayed a blank background?
It’s an option that I thinkg could be useful to enhance the visualization of the entities that have been laid over the tiles.
Advertisement
Answer
- you can add tile layers to folium maps
- below code adds a blank tile layer then on my system 26 other candidate base maps
- finally
folium.map.LayerControl()
allows layers to be selected including blank layer
JavaScript
x
46
46
1
import xyzservices.providers as xyz
2
import geopandas as gpd
3
import matplotlib.colors as colors
4
import folium
5
6
gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
7
gdf["Hemisphere"] = gdf["geometry"].apply(lambda x: "Norte" if x.y > 0 else "Sur")
8
9
# create a map
10
m = gdf.explore(
11
column="Hemisphere",
12
name="Cities",
13
cmap=colors.ListedColormap(["#D94325", "#5CD925"]),
14
tiles=None,
15
)
16
17
18
def filter_provider(p):
19
if p.requires_token():
20
return False
21
if (
22
"Stadia" in p.name
23
or "CyclOSM" in p.name
24
or "NASAGIBS" in p.name
25
or "BlackAndWhite" in p.name
26
):
27
return False
28
if hasattr(p, "variant"):
29
return False
30
if hasattr(p, "ext"):
31
return False
32
if hasattr(p, "status") and p.status == "broken":
33
return False
34
return True
35
36
37
# empty tile layer
38
folium.TileLayer("", name="None", attr="blank").add_to(m)
39
40
# add multiple candidate base layers / tiles to folium map
41
for name, args in xyz.filter(function=filter_provider).flatten().items():
42
folium.TileLayer(args["url"], name=name, attr=args["attribution"]).add_to(m)
43
44
# add control to be able to select base map
45
m.add_child(folium.map.LayerControl())
46