Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/folium/plugins/draw.py
2530 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
{% macro html(this, kwargs) %}
63
{% if this.export %}
64
<style>
65
#export {
66
position: absolute;
67
top: 5px;
68
right: 10px;
69
z-index: 999;
70
background: white;
71
color: black;
72
padding: 6px;
73
border-radius: 4px;
74
font-family: 'Helvetica Neue';
75
cursor: pointer;
76
font-size: 12px;
77
text-decoration: none;
78
top: 90px;
79
}
80
</style>
81
<a href='#' id='export'>Export</a>
82
{% endif %}
83
{% endmacro %}
84
85
{% macro script(this, kwargs) %}
86
var options = {
87
position: {{ this.position|tojson }},
88
draw: {{ this.draw_options|tojson }},
89
edit: {{ this.edit_options|tojson }},
90
}
91
{%- if this.feature_group %}
92
var drawnItems_{{ this.get_name() }} =
93
{{ this.feature_group.get_name() }};
94
{%- else %}
95
// FeatureGroup is to store editable layers.
96
var drawnItems_{{ this.get_name() }} =
97
new L.featureGroup().addTo(
98
{{ this._parent.get_name() }}
99
);
100
{%- endif %}
101
102
options.edit.featureGroup = drawnItems_{{ this.get_name() }};
103
var {{ this.get_name() }} = new L.Control.Draw(
104
options
105
).addTo( {{this._parent.get_name()}} );
106
{{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function(e) {
107
var layer = e.layer,
108
type = e.layerType;
109
var coords = JSON.stringify(layer.toGeoJSON());
110
{%- if this.show_geometry_on_click %}
111
layer.on('click', function() {
112
alert(coords);
113
console.log(coords);
114
});
115
{%- endif %}
116
117
{%- for event, handler in this.on.items() %}
118
layer.on(
119
"{{event}}",
120
{{handler}}
121
);
122
{%- endfor %}
123
drawnItems_{{ this.get_name() }}.addLayer(layer);
124
});
125
{{ this._parent.get_name() }}.on('draw:created', function(e) {
126
drawnItems_{{ this.get_name() }}.addLayer(e.layer);
127
});
128
129
{% if this.export %}
130
document.getElementById('export').onclick = function(e) {
131
var data = drawnItems_{{ this.get_name() }}.toGeoJSON();
132
var convertedData = 'text/json;charset=utf-8,'
133
+ encodeURIComponent(JSON.stringify(data));
134
document.getElementById('export').setAttribute(
135
'href', 'data:' + convertedData
136
);
137
document.getElementById('export').setAttribute(
138
'download', {{ this.filename|tojson }}
139
);
140
}
141
{% endif %}
142
{% endmacro %}
143
""")
144
145
default_js = [
146
(
147
"leaflet_draw_js",
148
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js",
149
)
150
]
151
default_css = [
152
(
153
"leaflet_draw_css",
154
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css",
155
)
156
]
157
158
def __init__(
159
self,
160
export=False,
161
feature_group=None,
162
filename="data.geojson",
163
position="topleft",
164
show_geometry_on_click=True,
165
draw_options=None,
166
edit_options=None,
167
on=None,
168
):
169
super().__init__()
170
self._name = "DrawControl"
171
self.export = export
172
self.feature_group = feature_group
173
self.filename = filename
174
self.position = position
175
self.show_geometry_on_click = show_geometry_on_click
176
self.draw_options = draw_options or {}
177
self.edit_options = edit_options or {}
178
self.on = on or {}
179
180