Skip to content

Add the GeoMan plugin #2124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions folium/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,6 +50,7 @@
"FloatImage",
"Fullscreen",
"Geocoder",
"GeoMan",
"GroupedLayerControl",
"HeatMap",
"HeatMapWithTime",
Expand Down
101 changes: 101 additions & 0 deletions folium/plugins/geoman.py
Original file line number Diff line number Diff line change
@@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a versioned URL here? I'm also on the fence when doing this for plugins.

BTW, I would recommend all new plugins to be their own package. However, this one does make sense to be here IMO.

Copy link
Collaborator Author

@hansthen hansthen Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference is for unversioned most of the time. It causes a lot of small annoyances every time something breaks. It avoids being unable to bring a plugin up to date because too much time has passed and the old versions are no longer supported.

What do you mean "their own package"? You mean like a separate github repository?

Copy link
Member

@ocefpaf ocefpaf Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. We took that approach to

  1. Decoupled the plugin development and release from folium
  2. Avoid the extra maintenance burden

)
]
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
)
29 changes: 29 additions & 0 deletions tests/plugins/test_geoman.py
Original file line number Diff line number Diff line change
@@ -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