Skip to content

Commit

Permalink
Add feature_group parameter to Draw plugin (#2001)
Browse files Browse the repository at this point in the history
* Add feature_group parameter to Draw plugin

This can be used to pass existing layer objects into
the Draw plugin, which the user can then edit.

* Update after review comments
  • Loading branch information
hansthen authored Oct 18, 2024
1 parent ebfa447 commit 806f697
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions folium/plugins/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Draw(JSCSSMixin, MacroElement):
----------
export : bool, default False
Add a small button that exports the drawn shapes as a geojson file.
feature_group : FeatureGroup, optional
The FeatureGroup object that will hold the editable figures. This can
be used to initialize the Draw plugin with predefined Layer objects.
filename : string, default 'data.geojson'
Name of geojson file
position : {'topleft', 'toprigth', 'bottomleft', 'bottomright'}
Expand Down Expand Up @@ -50,10 +53,17 @@ class Draw(JSCSSMixin, MacroElement):
draw: {{ this.draw_options|tojson }},
edit: {{ this.edit_options|tojson }},
}
// FeatureGroup is to store editable layers.
var drawnItems_{{ this.get_name() }} = new L.featureGroup().addTo(
{{ this._parent.get_name() }}
);
{%- 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 %}
options.edit.featureGroup = drawnItems_{{ this.get_name() }};
var {{ this.get_name() }} = new L.Control.Draw(
options
Expand All @@ -69,7 +79,7 @@ class Draw(JSCSSMixin, MacroElement):
});
{%- endif %}
drawnItems_{{ this.get_name() }}.addLayer(layer);
});
});
{{ this._parent.get_name() }}.on('draw:created', function(e) {
drawnItems_{{ this.get_name() }}.addLayer(e.layer);
});
Expand Down Expand Up @@ -106,6 +116,7 @@ class Draw(JSCSSMixin, MacroElement):
def __init__(
self,
export=False,
feature_group=None,
filename="data.geojson",
position="topleft",
show_geometry_on_click=True,
Expand All @@ -115,6 +126,7 @@ def __init__(
super().__init__()
self._name = "DrawControl"
self.export = export
self.feature_group = feature_group
self.filename = filename
self.position = position
self.show_geometry_on_click = show_geometry_on_click
Expand Down

0 comments on commit 806f697

Please sign in to comment.