Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/draw.py
1578 views
1
from branca.element import MacroElement
2
3
from folium.elements import JSCSSMixin
4
from folium.template import Template
5
6
7
class Draw(JSCSSMixin, MacroElement):
8
'''
9
Vector drawing and editing plugin for Leaflet.
10
11
Parameters
12
----------
13
export : bool, default False
14
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.
18
filename : string, default 'data.geojson'
19
Name of geojson file
20
position : {'topleft', 'toprigth', 'bottomleft', 'bottomright'}
21
Position of control.
22
See https://leafletjs.com/reference.html#control
23
show_geometry_on_click : bool, default True
24
When True, opens an alert with the geometry description on click.
25
draw_options : dict, optional
26
The options used to configure the draw toolbar. See
27
http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#drawoptions
28
edit_options : dict, optional
29
The options used to configure the edit toolbar. See
30
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions
31
on : dict, optional
32
Event handlers to attach to the created layer. Pass a mapping from the
33
names of the events to their `JsCode` handlers.
34
35
Examples
36
--------
37
>>> m = folium.Map()
38
>>> Draw(
39
... export=True,
40
... filename="my_data.geojson",
41
... show_geometry_on_click=False,
42
... position="topleft",
43
... draw_options={"polyline": {"allowIntersection": False}},
44
... edit_options={"poly": {"allowIntersection": False}},
45
... on={
46
... "click": JsCode(
47
... """
48
... function(event) {
49
... alert(JSON.stringify(this.toGeoJSON()));
50
... }
51
... """
52
... )
53
... },
54
... ).add_to(m)
55
56
For more info please check
57
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
58
59
'''
60
61
_template = Template(
62
"""
63
{% macro html(this, kwargs) %}
64
{% if this.export %}
65
<style>
66
#export {
67
position: absolute;
68
top: 5px;
69
right: 10px;
70
z-index: 999;
71
background: white;
72
color: black;
73
padding: 6px;
74
border-radius: 4px;
75
font-family: 'Helvetica Neue';
76
cursor: pointer;
77
font-size: 12px;
78
text-decoration: none;
79
top: 90px;
80
}
81
</style>
82
<a href='#' id='export'>Export</a>
83
{% endif %}
84
{% endmacro %}
85
86
{% macro script(this, kwargs) %}
87
var options = {
88
position: {{ this.position|tojson }},
89
draw: {{ this.draw_options|tojson }},
90
edit: {{ this.edit_options|tojson }},
91
}
92
{%- if this.feature_group %}
93
var drawnItems_{{ this.get_name() }} =
94
{{ this.feature_group.get_name() }};
95
{%- else %}
96
// FeatureGroup is to store editable layers.
97
var drawnItems_{{ this.get_name() }} =
98
new L.featureGroup().addTo(
99
{{ this._parent.get_name() }}
100
);
101
{%- endif %}
102
103
options.edit.featureGroup = drawnItems_{{ this.get_name() }};
104
var {{ this.get_name() }} = new L.Control.Draw(
105
options
106
).addTo( {{this._parent.get_name()}} );
107
{{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function(e) {
108
var layer = e.layer,
109
type = e.layerType;
110
var coords = JSON.stringify(layer.toGeoJSON());
111
{%- if this.show_geometry_on_click %}
112
layer.on('click', function() {
113
alert(coords);
114
console.log(coords);
115
});
116
{%- endif %}
117
118
{%- for event, handler in this.on.items() %}
119
layer.on(
120
"{{event}}",
121
{{handler}}
122
);
123
{%- endfor %}
124
drawnItems_{{ this.get_name() }}.addLayer(layer);
125
});
126
{{ this._parent.get_name() }}.on('draw:created', function(e) {
127
drawnItems_{{ this.get_name() }}.addLayer(e.layer);
128
});
129
130
{% if this.export %}
131
document.getElementById('export').onclick = function(e) {
132
var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
133
var convertedData = 'text/json;charset=utf-8,'
134
+ encodeURIComponent(JSON.stringify(data));
135
document.getElementById('export').setAttribute(
136
'href', 'data:' + convertedData
137
);
138
document.getElementById('export').setAttribute(
139
'download', {{ this.filename|tojson }}
140
);
141
}
142
{% endif %}
143
{% endmacro %}
144
"""
145
)
146
147
default_js = [
148
(
149
"leaflet_draw_js",
150
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js",
151
)
152
]
153
default_css = [
154
(
155
"leaflet_draw_css",
156
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css",
157
)
158
]
159
160
def __init__(
161
self,
162
export=False,
163
feature_group=None,
164
filename="data.geojson",
165
position="topleft",
166
show_geometry_on_click=True,
167
draw_options=None,
168
edit_options=None,
169
on=None,
170
):
171
super().__init__()
172
self._name = "DrawControl"
173
self.export = export
174
self.feature_group = feature_group
175
self.filename = filename
176
self.position = position
177
self.show_geometry_on_click = show_geometry_on_click
178
self.draw_options = draw_options or {}
179
self.edit_options = edit_options or {}
180
self.on = on or {}
181
182