Search code examples
pythongeopandasfolium

Switching off all Folium tiles


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.


Solution

    • 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
    import xyzservices.providers as xyz
    import geopandas as gpd
    import matplotlib.colors as colors
    import folium
    
    gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))
    gdf["Hemisphere"] = gdf["geometry"].apply(lambda x: "Norte" if x.y > 0 else "Sur")
    
    # create a map
    m = gdf.explore(
        column="Hemisphere",
        name="Cities",
        cmap=colors.ListedColormap(["#D94325", "#5CD925"]),
        tiles=None,
    )
    
    
    def filter_provider(p):
        if p.requires_token():
            return False
        if (
            "Stadia" in p.name
            or "CyclOSM" in p.name
            or "NASAGIBS" in p.name
            or "BlackAndWhite" in p.name
        ):
            return False
        if hasattr(p, "variant"):
            return False
        if hasattr(p, "ext"):
            return False
        if hasattr(p, "status") and p.status == "broken":
            return False
        return True
    
    
    # empty tile layer
    folium.TileLayer("", name="None", attr="blank").add_to(m)
    
    # add multiple candidate base layers / tiles to folium map
    for name, args in xyz.filter(function=filter_provider).flatten().items():
        folium.TileLayer(args["url"], name=name, attr=args["attribution"]).add_to(m)
    
    # add control to be able to select base map
    m.add_child(folium.map.LayerControl())
    

    enter image description here