Skip to content

Commit 806f697

Browse files
authored
Add feature_group parameter to Draw plugin (#2001)
* 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
1 parent ebfa447 commit 806f697

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

folium/plugins/draw.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class Draw(JSCSSMixin, MacroElement):
1212
----------
1313
export : bool, default False
1414
Add a small button that exports the drawn shapes as a geojson file.
15+
feature_group : FeatureGroup, optional
16+
The FeatureGroup object that will hold the editable figures. This can
17+
be used to initialize the Draw plugin with predefined Layer objects.
1518
filename : string, default 'data.geojson'
1619
Name of geojson file
1720
position : {'topleft', 'toprigth', 'bottomleft', 'bottomright'}
@@ -50,10 +53,17 @@ class Draw(JSCSSMixin, MacroElement):
5053
draw: {{ this.draw_options|tojson }},
5154
edit: {{ this.edit_options|tojson }},
5255
}
53-
// FeatureGroup is to store editable layers.
54-
var drawnItems_{{ this.get_name() }} = new L.featureGroup().addTo(
55-
{{ this._parent.get_name() }}
56-
);
56+
{%- if this.feature_group %}
57+
var drawnItems_{{ this.get_name() }} =
58+
{{ this.feature_group.get_name() }};
59+
{%- else %}
60+
// FeatureGroup is to store editable layers.
61+
var drawnItems_{{ this.get_name() }} =
62+
new L.featureGroup().addTo(
63+
{{ this._parent.get_name() }}
64+
);
65+
{%- endif %}
66+
5767
options.edit.featureGroup = drawnItems_{{ this.get_name() }};
5868
var {{ this.get_name() }} = new L.Control.Draw(
5969
options
@@ -69,7 +79,7 @@ class Draw(JSCSSMixin, MacroElement):
6979
});
7080
{%- endif %}
7181
drawnItems_{{ this.get_name() }}.addLayer(layer);
72-
});
82+
});
7383
{{ this._parent.get_name() }}.on('draw:created', function(e) {
7484
drawnItems_{{ this.get_name() }}.addLayer(e.layer);
7585
});
@@ -106,6 +116,7 @@ class Draw(JSCSSMixin, MacroElement):
106116
def __init__(
107117
self,
108118
export=False,
119+
feature_group=None,
109120
filename="data.geojson",
110121
position="topleft",
111122
show_geometry_on_click=True,
@@ -115,6 +126,7 @@ def __init__(
115126
super().__init__()
116127
self._name = "DrawControl"
117128
self.export = export
129+
self.feature_group = feature_group
118130
self.filename = filename
119131
self.position = position
120132
self.show_geometry_on_click = show_geometry_on_click

0 commit comments

Comments
 (0)