From 800736af9e9a5652338a58df822928ef89d924e8 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Mon, 7 Apr 2025 16:36:44 +0200 Subject: [PATCH] Add the GeoMan plugin The GeoMan plugin is like the Draw plugin, with extra functionality, such as support for Polygons with holes. --- folium/plugins/__init__.py | 2 + folium/plugins/geoman.py | 101 +++++++++++++++++++++++++++++++++++ tests/plugins/test_geoman.py | 29 ++++++++++ 3 files changed, 132 insertions(+) create mode 100644 folium/plugins/geoman.py create mode 100644 tests/plugins/test_geoman.py diff --git a/folium/plugins/__init__.py b/folium/plugins/__init__.py index 59e89b883c..7888f2ef42 100644 --- a/folium/plugins/__init__.py +++ b/folium/plugins/__init__.py @@ -11,6 +11,7 @@ from folium.plugins.float_image import FloatImage from folium.plugins.fullscreen import Fullscreen from folium.plugins.geocoder import Geocoder +from folium.plugins.geoman import GeoMan from folium.plugins.groupedlayercontrol import GroupedLayerControl from folium.plugins.heat_map import HeatMap from folium.plugins.heat_map_withtime import HeatMapWithTime @@ -49,6 +50,7 @@ "FloatImage", "Fullscreen", "Geocoder", + "GeoMan", "GroupedLayerControl", "HeatMap", "HeatMapWithTime", diff --git a/folium/plugins/geoman.py b/folium/plugins/geoman.py new file mode 100644 index 0000000000..c975d98777 --- /dev/null +++ b/folium/plugins/geoman.py @@ -0,0 +1,101 @@ +from branca.element import MacroElement + +from folium.elements import JSCSSMixin +from folium.template import Template +from folium.utilities import remove_empty + + +class GeoMan(JSCSSMixin, MacroElement): + """ + An Open Source Leaflet Plugin for editing polygons + + Examples + -------- + >>> m = folium.Map() + >>> Geoman().add_to(m) + + For more info please check + https://github.com/geoman-io/leaflet-geoman/ + + """ + + _template = Template( + """ + {% macro script(this, kwargs) %} + {%- if this.feature_group %} + var drawnItems_{{ this.get_name() }} = + {{ this.feature_group.get_name() }}; + {%- else %} + // FeatureGroup is to store editable layers. + var drawnItems_{{ this.get_name() }} = + new L.featureGroup().addTo( + {{ this._parent.get_name() }} + ); + {%- endif %} + /* The global varianble below is needed to prevent streamlit-folium + from barfing :-( + */ + var drawnItems = drawnItems_{{ this.get_name() }}; + + {{this._parent.get_name()}}.pm.addControls( + {{this.options|tojavascript}} + ) + drawnItems_{{ this.get_name() }}.eachLayer(function(layer){ + L.PM.reInitLayer(layer); + {%- for event, handler in this.on.items() %} + layer.on( + "{{event}}", + {{handler}} + ); + {%- endfor %} + }); + + {{ this._parent.get_name() }}.on("pm:create", function(e) { + var layer = e.layer, + type = e.layerType; + + {%- for event, handler in this.on.items() %} + layer.on( + "{{event}}", + {{handler}} + ); + {%- endfor %} + drawnItems_{{ this.get_name() }}.addLayer(layer); + }); + {{ this._parent.get_name() }}.on("pm:remove", function(e) { + var layer = e.layer, + type = e.layerType; + drawnItems_{{ this.get_name() }}.removeLayer(layer); + }); + + {% endmacro %} + """ + ) + + default_js = [ + ( + "leaflet_geoman_js", + "https://unpkg.com/@geoman-io/leaflet-geoman-free@latest/dist/leaflet-geoman.js", + ) + ] + default_css = [ + ( + "leaflet_geoman_css", + "https://unpkg.com/@geoman-io/leaflet-geoman-free@latest/dist/leaflet-geoman.css", + ) + ] + + def __init__( + self, + position="topleft", + feature_group=None, + on=None, + **kwargs, + ): + super().__init__() + self._name = "GeoMan" + self.feature_group = feature_group + self.on = on or {} + self.options = remove_empty( + position=position, layer_group=feature_group, **kwargs + ) diff --git a/tests/plugins/test_geoman.py b/tests/plugins/test_geoman.py new file mode 100644 index 0000000000..fe6f5d30fc --- /dev/null +++ b/tests/plugins/test_geoman.py @@ -0,0 +1,29 @@ +""" +Test GeoMan +---------------- + +""" + +import folium +from folium import plugins +from folium.template import Template +from folium.utilities import normalize + + +def test_geoman(): + m = folium.Map([47, 3], zoom_start=1) + fs = plugins.GeoMan().add_to(m) + + out = normalize(m._parent.render()) + + # verify that the GeoMan plugin was added to + # the map + tmpl = Template( + """ + {{this._parent.get_name()}}.pm.addControls( + {{this.options|tojavascript}} + ) + """ + ) + + assert normalize(tmpl.render(this=fs)) in out