-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_draw.py
186 lines (169 loc) · 6.9 KB
/
my_draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from branca.element import Element, Figure, MacroElement
from folium.elements import JSCSSMixin
from jinja2 import Template
class Draw(JSCSSMixin, MacroElement):
"""
Vector drawing and editing plugin for Leaflet.
Parameters
----------
export : bool, default False
Add a small button that exports the drawn shapes as a geojson file.
filename : string, default 'data.geojson'
Name of geojson file
position : {'topleft', 'toprigth', 'bottomleft', 'bottomright'}
Position of control.
See https://leafletjs.com/reference-1.6.0.html#control
draw_options : dict, optional
The options used to configure the draw toolbar. See
http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#drawoptions
edit_options : dict, optional
The options used to configure the edit toolbar. See
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions
Examples
--------
>>> m = folium.Map()
>>> Draw(
... export=True,
... filename='my_data.geojson',
... position='topleft',
... draw_options={'polyline': {'allowIntersection': False}},
... edit_options={'poly': {'allowIntersection': False}}
... ).add_to(m)
For more info please check
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
"""
_template = Template(u"""
{% macro script(this, kwargs) %}
var options = {
position: {{ this.position|tojson }},
draw: {{ this.draw_options|tojson }},
edit: {{ this.edit_options|tojson }},
}
console.log({{ this.edit_options|tojson }});
console.log(options)
options.edit.poly.icon = new L.DivIcon({
iconSize: new L.Point(16, 16),
className: 'leaflet-div-icon leaflet-editing-icon my-own-class'
})
console.log(options)
// FeatureGroup is to store editable layers.
var drawnItems = new L.featureGroup().addTo(
{{ this._parent.get_name() }}
);
options.edit.featureGroup = drawnItems;
var {{ this.get_name() }} = new L.Control.Draw(
options
).addTo( {{this._parent.get_name()}} );
//{{ this._parent.get_name() }}.on(L.Draw.Event.CREATED, function(e) {
// console.log('L.Draw.Event.CREATED', e)
// var layer = e.layer,
// type = e.layerType;
// var coords = JSON.stringify(layer.toGeoJSON());
// layer.on('click', function() {
// alert(coords);
// console.log(coords);
// });
// drawnItems.addLayer(layer);
// });
{{ this._parent.get_name() }}.on('draw:created', function(e) {
console.log('draw:created', e)
$.ajax({
url: '/end_create_route',
data: {
'latlngs': JSON.stringify(e.layer._latlngs),
},
type: 'POST',
success: function(response){
response_dict = JSON.parse(response);
console.log(response_dict);
if ("js_code" in response_dict) {
eval(response_dict["js_code"]);
}
},
error: function(error){
console.log(error);
}
});
});
{{ this._parent.get_name() }}.on('draw:editstop', function(e) {
console.log('draw:editstop', e, current_edit_line)
delete drawnItems._layers[drawnItems.getLayerId(current_edit_line)];
$.ajax({
url: '/endedit',
data: {
'route_name': current_route_name,
'latlngs': JSON.stringify(current_edit_line._latlngs),
},
type: 'POST',
success: function(response){
response_dict = JSON.parse(response);
console.log(response_dict);
if ("js_code" in response_dict) {
eval(response_dict["js_code"]);
}
},
error: function(error){
console.log(error);
}
});
});
{% if this.export %}
document.getElementById('export').onclick = function(e) {
var data = drawnItems.toGeoJSON();
var convertedData = 'text/json;charset=utf-8,'
+ encodeURIComponent(JSON.stringify(data));
document.getElementById('export').setAttribute(
'href', 'data:' + convertedData
);
document.getElementById('export').setAttribute(
'download', {{ this.filename|tojson }}
);
}
{% endif %}
{% endmacro %}
""")
default_js = [
('leaflet_draw_js',
'https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js')
]
default_css = [
('leaflet_draw_css',
'https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css')
]
def __init__(self, export=False, filename='data.geojson',
position='topleft', draw_options=None, edit_options=None):
super(Draw, self).__init__()
self._name = 'DrawControl'
self.export = export
self.filename = filename
self.position = position
self.draw_options = draw_options or {}
self.edit_options = edit_options or {}
def render(self, **kwargs):
super(Draw, self).render(**kwargs)
figure = self.get_root()
assert isinstance(figure, Figure), ('You cannot render this Element '
'if it is not in a Figure.')
export_style = """
<style>
#export {
position: absolute;
top: 5px;
right: 10px;
z-index: 999;
background: white;
color: black;
padding: 6px;
border-radius: 4px;
font-family: 'Helvetica Neue';
cursor: pointer;
font-size: 12px;
text-decoration: none;
top: 90px;
}
</style>
"""
export_button = """<a href='#' id='export'>Export</a>"""
if self.export:
figure.header.add_child(Element(export_style), name='export')
figure.html.add_child(Element(export_button), name='export_button')