From f990008d90fa09e60c33d29d61c7dbebc134b6ab Mon Sep 17 00:00:00 2001 From: rraymondgh <42769112+rraymondgh@users.noreply.github.com> Date: Thu, 27 Jan 2022 07:22:43 +0000 Subject: [PATCH] ENH: expose folium.map kwds as map_kwds in `explore()` (#2315) Co-authored-by: Martin Fleischmann --- geopandas/explore.py | 16 +++++++++++++++- geopandas/tests/test_explore.py | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/geopandas/explore.py b/geopandas/explore.py index 12d4b41..7efab95 100644 --- a/geopandas/explore.py +++ b/geopandas/explore.py @@ -57,6 +57,7 @@ def _explore( tooltip_kwds={}, popup_kwds={}, legend_kwds={}, + map_kwds={}, **kwargs, ): """Interactive map based on GeoPandas and folium/leaflet.js @@ -225,6 +226,10 @@ def _explore( Applies if ``colorbar=False``. max_labels : int, default 10 Maximum number of colorbar tick labels (requires branca>=0.5.0) + map_kwds : dict (default {}) + Additional keywords to be passed to folium :class:`~folium.folium.Map`, + e.g. ``dragging``, or ``scrollWheelZoom``. + **kwargs : dict Additional options to be passed on to the folium object. @@ -302,7 +307,16 @@ GON (((180.00000 -16.06713, 180.00000... fit = False # get a subset of kwargs to be passed to folium.Map - map_kwds = {i: kwargs[i] for i in kwargs.keys() if i in _MAP_KWARGS} + for i in _MAP_KWARGS: + if i in map_kwds: + raise ValueError( + f"'{i}' cannot be specified in 'map_kwds'. " + f"Use the '{i}={map_kwds[i]}' argument instead." + ) + map_kwds = { + **map_kwds, + **{i: kwargs[i] for i in kwargs.keys() if i in _MAP_KWARGS}, + } if HAS_XYZSERVICES: # match provider name string to xyzservices.TileProvider diff --git a/geopandas/tests/test_explore.py b/geopandas/tests/test_explore.py index b95d096..73917a3 100644 --- a/geopandas/tests/test_explore.py +++ b/geopandas/tests/test_explore.py @@ -797,3 +797,22 @@ class TestExplore: gdf["centroid"] = gdf.centroid gdf.explore() + + def test_map_kwds(self): + def check(): + out_str = self._fetch_map_string(m) + assert "zoomControl:false" in out_str + assert "dragging:false" in out_str + assert "scrollWheelZoom:false" in out_str + + # check that folium and leaflet Map() parameters can be passed + m = self.world.explore( + zoom_control=False, map_kwds=dict(dragging=False, scrollWheelZoom=False) + ) + check() + with pytest.raises( + ValueError, match="'zoom_control' cannot be specified in 'map_kwds'" + ): + self.world.explore( + map_kwds=dict(dragging=False, scrollWheelZoom=False, zoom_control=False) + )