Skip to content

Commit 888acf2

Browse files
committed
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.
1 parent dc84732 commit 888acf2

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

folium/plugins/draw.py

Lines changed: 31 additions & 16 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,26 +53,36 @@ 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
6070
).addTo( {{this._parent.get_name()}} );
61-
{{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function(e) {
62-
var layer = e.layer,
63-
type = e.layerType;
64-
var coords = JSON.stringify(layer.toGeoJSON());
65-
{%- if this.show_geometry_on_click %}
66-
layer.on('click', function() {
67-
alert(coords);
68-
console.log(coords);
69-
});
70-
{%- endif %}
71-
drawnItems_{{ this.get_name() }}.addLayer(layer);
72-
});
71+
{{ this._parent.get_name() }}.on(
72+
L.Draw.Event.CREATED,
73+
function(e) {
74+
var layer = e.layer,
75+
type = e.layerType;
76+
var coords = JSON.stringify(layer.toGeoJSON());
77+
{%- if this.show_geometry_on_click %}
78+
layer.on('click', function() {
79+
alert(coords);
80+
console.log(coords);
81+
});
82+
{%- endif %}
83+
drawnItems_{{ this.get_name() }}.addLayer(layer);
84+
}
85+
);
7386
{{ this._parent.get_name() }}.on('draw:created', function(e) {
7487
drawnItems_{{ this.get_name() }}.addLayer(e.layer);
7588
});
@@ -106,6 +119,7 @@ class Draw(JSCSSMixin, MacroElement):
106119
def __init__(
107120
self,
108121
export=False,
122+
feature_group=None,
109123
filename="data.geojson",
110124
position="topleft",
111125
show_geometry_on_click=True,
@@ -115,6 +129,7 @@ def __init__(
115129
super().__init__()
116130
self._name = "DrawControl"
117131
self.export = export
132+
self.feature_group = feature_group
118133
self.filename = filename
119134
self.position = position
120135
self.show_geometry_on_click = show_geometry_on_click

0 commit comments

Comments
 (0)