From f716052f8ebca1e36330956e3292636f33452e1e Mon Sep 17 00:00:00 2001 From: prusswan Date: Mon, 25 Dec 2023 02:55:18 +0800 Subject: [PATCH 1/5] expanding Geocoder plugin to support other built-in providers supported by leaflet-control-geocoder --- folium/plugins/geocoder.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/folium/plugins/geocoder.py b/folium/plugins/geocoder.py index 418dcb9d0..82b5702a9 100644 --- a/folium/plugins/geocoder.py +++ b/folium/plugins/geocoder.py @@ -19,6 +19,10 @@ class Geocoder(JSCSSMixin, MacroElement): Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'. add_marker: bool, default True If True, adds a marker on the found location. + geocode_provider: str, default 'nominatim' + Defaults to "nominatim", see https://github.com/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers. + geocode_provider_options: dict, default {} + For use with specific providers that may require api keys or other parameters. For all options see https://github.com/perliedman/leaflet-control-geocoder @@ -27,8 +31,19 @@ class Geocoder(JSCSSMixin, MacroElement): _template = Template( """ {% macro script(this, kwargs) %} + + var geocoderOpts_{{ this.get_name() }} = {{ this.options|tojson }}; + + // note: geocoder name should start with lowercase + var geocoderName_{{ this.get_name() }} = geocoderOpts_{{ this.get_name() }}["geocodeProvider"]; + + var customGeocoder_{{ this.get_name() }} = L.Control.Geocoder[ geocoderName_{{ this.get_name() }} ]( + geocoderOpts_{{ this.get_name() }}['geocodeProviderOptions'] + ); + geocoderOpts_{{ this.get_name() }}["geocoder"] = customGeocoder_{{ this.get_name() }}; + L.Control.geocoder( - {{ this.options|tojson }} + geocoderOpts_{{ this.get_name() }} ).on('markgeocode', function(e) { {{ this._parent.get_name() }}.setView(e.geocode.center, 11); }).addTo({{ this._parent.get_name() }}); @@ -50,12 +65,22 @@ class Geocoder(JSCSSMixin, MacroElement): ) ] - def __init__(self, collapsed=False, position="topright", add_marker=True, **kwargs): + def __init__( + self, + collapsed: bool = False, + position: str = "topright", + add_marker: bool = True, + geocode_provider: str = "nominatim", + geocode_provider_options: dict = {}, + **kwargs + ): super().__init__() self._name = "Geocoder" self.options = parse_options( collapsed=collapsed, position=position, - defaultMarkGeocode=add_marker, + default_mark_geocode=add_marker, + geocode_provider=geocode_provider, + geocode_provider_options=geocode_provider_options, **kwargs ) From 5c7d7a0b6d4c8515cb94b66c167255db9cd9d1cd Mon Sep 17 00:00:00 2001 From: prusswan Date: Sat, 6 Jan 2024 08:55:17 +0800 Subject: [PATCH 2/5] Added geocode_zoom option for setting zoom level used to display the geocode result --- folium/plugins/geocoder.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/folium/plugins/geocoder.py b/folium/plugins/geocoder.py index 82b5702a9..96ed4fc6d 100644 --- a/folium/plugins/geocoder.py +++ b/folium/plugins/geocoder.py @@ -19,6 +19,8 @@ class Geocoder(JSCSSMixin, MacroElement): Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'. add_marker: bool, default True If True, adds a marker on the found location. + geocode_zoom: int, default 11 + Set zoom level used for displaying the geocode result, note that this only has an effect when add_marker is set to False. Set this to None to preserve the current map zoom level. geocode_provider: str, default 'nominatim' Defaults to "nominatim", see https://github.com/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers. geocode_provider_options: dict, default {} @@ -45,7 +47,8 @@ class Geocoder(JSCSSMixin, MacroElement): L.Control.geocoder( geocoderOpts_{{ this.get_name() }} ).on('markgeocode', function(e) { - {{ this._parent.get_name() }}.setView(e.geocode.center, 11); + var zoom = geocoderOpts_{{ this.get_name() }}['geocodeZoom'] || {{ this._parent.get_name() }}.getZoom(); + {{ this._parent.get_name() }}.setView(e.geocode.center, zoom); }).addTo({{ this._parent.get_name() }}); {% endmacro %} @@ -70,6 +73,7 @@ def __init__( collapsed: bool = False, position: str = "topright", add_marker: bool = True, + geocode_zoom: int | None = 11, geocode_provider: str = "nominatim", geocode_provider_options: dict = {}, **kwargs @@ -80,6 +84,7 @@ def __init__( collapsed=collapsed, position=position, default_mark_geocode=add_marker, + geocode_zoom=geocode_zoom, geocode_provider=geocode_provider, geocode_provider_options=geocode_provider_options, **kwargs From 31340d3d24295bf97e178409e8a39ba902a6c751 Mon Sep 17 00:00:00 2001 From: prusswan Date: Wed, 10 Jan 2024 19:30:43 +0800 Subject: [PATCH 3/5] fixed docstring and type notation for geocode_zoom --- folium/plugins/geocoder.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/folium/plugins/geocoder.py b/folium/plugins/geocoder.py index 96ed4fc6d..f53dee52d 100644 --- a/folium/plugins/geocoder.py +++ b/folium/plugins/geocoder.py @@ -4,6 +4,8 @@ from folium.elements import JSCSSMixin from folium.utilities import parse_options +from typing import Optional + class Geocoder(JSCSSMixin, MacroElement): """A simple geocoder for Leaflet that by default uses OSM/Nominatim. @@ -19,7 +21,7 @@ class Geocoder(JSCSSMixin, MacroElement): Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'. add_marker: bool, default True If True, adds a marker on the found location. - geocode_zoom: int, default 11 + geocode_zoom: int, default 11, optional Set zoom level used for displaying the geocode result, note that this only has an effect when add_marker is set to False. Set this to None to preserve the current map zoom level. geocode_provider: str, default 'nominatim' Defaults to "nominatim", see https://github.com/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers. @@ -73,7 +75,7 @@ def __init__( collapsed: bool = False, position: str = "topright", add_marker: bool = True, - geocode_zoom: int | None = 11, + geocode_zoom: Optional[int] = 11, geocode_provider: str = "nominatim", geocode_provider_options: dict = {}, **kwargs From 781cfe2ed5f3c1a1c1f6053846f3fa359999f934 Mon Sep 17 00:00:00 2001 From: prusswan Date: Wed, 10 Jan 2024 19:37:32 +0800 Subject: [PATCH 4/5] dropped geocode_ prefix in arguments --- folium/plugins/geocoder.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/folium/plugins/geocoder.py b/folium/plugins/geocoder.py index f53dee52d..4cd889e6b 100644 --- a/folium/plugins/geocoder.py +++ b/folium/plugins/geocoder.py @@ -21,11 +21,11 @@ class Geocoder(JSCSSMixin, MacroElement): Choose from 'topleft', 'topright', 'bottomleft' or 'bottomright'. add_marker: bool, default True If True, adds a marker on the found location. - geocode_zoom: int, default 11, optional + zoom: int, default 11, optional Set zoom level used for displaying the geocode result, note that this only has an effect when add_marker is set to False. Set this to None to preserve the current map zoom level. - geocode_provider: str, default 'nominatim' + provider: str, default 'nominatim' Defaults to "nominatim", see https://github.com/perliedman/leaflet-control-geocoder/tree/2.4.0/src/geocoders for other built-in providers. - geocode_provider_options: dict, default {} + provider_options: dict, default {} For use with specific providers that may require api keys or other parameters. For all options see https://github.com/perliedman/leaflet-control-geocoder @@ -39,17 +39,17 @@ class Geocoder(JSCSSMixin, MacroElement): var geocoderOpts_{{ this.get_name() }} = {{ this.options|tojson }}; // note: geocoder name should start with lowercase - var geocoderName_{{ this.get_name() }} = geocoderOpts_{{ this.get_name() }}["geocodeProvider"]; + var geocoderName_{{ this.get_name() }} = geocoderOpts_{{ this.get_name() }}["provider"]; var customGeocoder_{{ this.get_name() }} = L.Control.Geocoder[ geocoderName_{{ this.get_name() }} ]( - geocoderOpts_{{ this.get_name() }}['geocodeProviderOptions'] + geocoderOpts_{{ this.get_name() }}['providerOptions'] ); geocoderOpts_{{ this.get_name() }}["geocoder"] = customGeocoder_{{ this.get_name() }}; L.Control.geocoder( geocoderOpts_{{ this.get_name() }} ).on('markgeocode', function(e) { - var zoom = geocoderOpts_{{ this.get_name() }}['geocodeZoom'] || {{ this._parent.get_name() }}.getZoom(); + var zoom = geocoderOpts_{{ this.get_name() }}['zoom'] || {{ this._parent.get_name() }}.getZoom(); {{ this._parent.get_name() }}.setView(e.geocode.center, zoom); }).addTo({{ this._parent.get_name() }}); @@ -75,9 +75,9 @@ def __init__( collapsed: bool = False, position: str = "topright", add_marker: bool = True, - geocode_zoom: Optional[int] = 11, - geocode_provider: str = "nominatim", - geocode_provider_options: dict = {}, + zoom: Optional[int] = 11, + provider: str = "nominatim", + provider_options: dict = {}, **kwargs ): super().__init__() @@ -86,8 +86,8 @@ def __init__( collapsed=collapsed, position=position, default_mark_geocode=add_marker, - geocode_zoom=geocode_zoom, - geocode_provider=geocode_provider, - geocode_provider_options=geocode_provider_options, + zoom=zoom, + provider=provider, + provider_options=provider_options, **kwargs ) From 93753925488df78df7d5b0353b333a14380f4f19 Mon Sep 17 00:00:00 2001 From: prusswan Date: Wed, 10 Jan 2024 20:16:03 +0800 Subject: [PATCH 5/5] fixed import block (ruff --fix) --- folium/plugins/geocoder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/folium/plugins/geocoder.py b/folium/plugins/geocoder.py index 4cd889e6b..3d267a05d 100644 --- a/folium/plugins/geocoder.py +++ b/folium/plugins/geocoder.py @@ -1,11 +1,11 @@ +from typing import Optional + from branca.element import MacroElement from jinja2 import Template from folium.elements import JSCSSMixin from folium.utilities import parse_options -from typing import Optional - class Geocoder(JSCSSMixin, MacroElement): """A simple geocoder for Leaflet that by default uses OSM/Nominatim.