Search code examples
leafletgeopandasfolium

How to disable zoom and pan in a geopandas map created with explore()


I've created a map with the explore() function in geopandas, but I want to fix the zoom level and disable mouse pan.

I've tried the following

merged1.explore(
     column="department", # make choropleth based on "department" column
     tooltip="decision_type", # show "decision_type" value in tooltip (on hover)
     popup=True, # show all values in popup (on click)
     tiles="CartoDB positron", # use "CartoDB positron" tiles
     cmap="Set1", # use "Set1" matplotlib colormap
     style_kwds=dict(color="black"), # use black outline
     zoom_control=False,
    )

zoom_control=False disables the zoom buttons, but I haven't been able to disable zoom on scroll or pan. I tried adding dragging=False and scroll_wheel_zoom=False to the arguments, but it didn't work.

How do I pass the arguments to the folium object and/or leafletjs to achieve my goal?


Solution

  • If you take a look as explore.py within geopandas package, you will find there is a constant list of kwargs that are passed to folium / leaflet

    Extending this list with parameters defined in leaflet https://leafletjs.com/reference-1.6.0.html#map that you want to use allows you to pass them from explore() call.

    Below shows panning and zooming has been fully disabled.

    import geopandas as gpd
    import geopandas.explore
    
    geopandas.explore._MAP_KWARGS += ["dragging", "scrollWheelZoom"]
    
    world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
    world.loc[world["continent"].eq("Europe") & ~world["iso_a3"].isin(["RUS","-99"])].explore(
        # m=m,
        column="gdp_md_est",  # make choropleth based on "department" column
        tooltip="name",  # show "decision_type" value in tooltip (on hover)
        popup=True,  # show all values in popup (on click)
        tiles="CartoDB positron",  # use "CartoDB positron" tiles
        cmap="Set1",  # use "Set1" matplotlib colormap
        style_kwds=dict(color="black"),  # use black outline
        zoom_control=False,
        dragging=False,
        scrollWheelZoom=False
    )