11# -*- coding: utf-8 -*-
22
3+
34from __future__ import (absolute_import , division , print_function )
45
6+ import json
7+
58from branca .element import CssLink , Element , Figure , JavascriptLink , MacroElement
69
710from jinja2 import Template
@@ -15,23 +18,43 @@ class Draw(MacroElement):
1518 ----------
1619 export : bool, default False
1720 Add a small button that exports the drawn shapes as a geojson file.
18-
21+ filename : string, default 'data.geojson'
22+ Name of geojson file
23+ position : string, default 'topleft'
24+ Position of control. It can be one of 'topleft', 'toprigth', 'bottomleft', 'bottomright'
25+ See https://leafletjs.com/reference-1.4.0.html#control
26+ draw_options : dict, optional
27+ The options used to configure the draw toolbar
28+ See http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#drawoptions
29+ edit_options : dict, optional
30+ The options used to configure the edit toolbar
31+ See https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html#editpolyoptions
1932 Examples
2033 --------
2134 >>> m = folium.Map()
22- >>> Draw(export=True).add_to(m)
35+ >>> Draw(
36+ ... export=True,
37+ ... filename='my_data.geojson',
38+ ... position='topleft',
39+ ... draw_options={'polyline': {'allowIntersection': False}},
40+ ... edit_options={'poly': {'allowIntersection': False}}
41+ ... ).add_to(m)
2342
2443 For more info please check
2544 https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html
2645
2746 """
2847 _template = Template (u"""
2948 {% macro script(this, kwargs) %}
49+ var options = {
50+ position: "{{this.position}}",
51+ draw: {{this.draw_options}},
52+ edit: {{this.edit_options}}
53+ }
3054 // FeatureGroup is to store editable layers.
3155 var drawnItems = new L.featureGroup().addTo({{this._parent.get_name()}});
32- var {{this.get_name()}} = new L.Control.Draw({
33- "edit": {"featureGroup": drawnItems}
34- }).addTo({{this._parent.get_name()}})
56+ options.edit.featureGroup = drawnItems
57+ var {{this.get_name()}} = new L.Control.Draw(options).addTo({{this._parent.get_name()}})
3558 {{this._parent.get_name()}}.on(L.Draw.Event.CREATED, function (event) {
3659 var layer = event.layer,
3760 type = event.layerType,
@@ -43,35 +66,40 @@ class Draw(MacroElement):
4366 });
4467 drawnItems.addLayer(layer);
4568 });
46-
47- {{this._parent.get_name()}}.on('draw:created', function(e) {
48- drawnItems.addLayer(e.layer);
49- });
50-
51- document.getElementById('export').onclick = function(e) {
52- var data = drawnItems.toGeoJSON();
53- var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
54- document.getElementById('export').setAttribute('href', 'data:' + convertedData);
55- document.getElementById('export').setAttribute('download','data.geojson');
56- }
69+ {{this._parent.get_name()}}.on('draw:created', function(e) {
70+ drawnItems.addLayer(e.layer);
71+ });
72+ document.getElementById('export').onclick = function(e) {
73+ var data = drawnItems.toGeoJSON();
74+ var convertedData = 'text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(data));
75+ document.getElementById('export').setAttribute('href', 'data:' + convertedData);
76+ document.getElementById('export').setAttribute(
77+ 'download',
78+ "{{this.filename}}"
79+ );
80+ }
5781 {% endmacro %}
5882 """ )
5983
60- def __init__ (self , export = False ):
84+ def __init__ (self , export = False , filename = 'data.geojson' ,
85+ position = 'topleft' , draw_options = None , edit_options = None ):
6186 super (Draw , self ).__init__ ()
6287 self ._name = 'DrawControl'
6388 self .export = export
89+ self .filename = filename
90+ self .position = position
91+ self .draw_options = json .dumps (draw_options or {})
92+ self .edit_options = json .dumps (edit_options or {})
6493
6594 def render (self , ** kwargs ):
66- super (Draw , self ).render ()
95+ super (Draw , self ).render (** kwargs )
6796
6897 figure = self .get_root ()
6998 assert isinstance (figure , Figure ), ('You cannot render this Element '
7099 'if it is not in a Figure.' )
71100
72101 figure .header .add_child (
73102 JavascriptLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js' )) # noqa
74-
75103 figure .header .add_child (
76104 CssLink ('https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css' )) # noqa
77105
0 commit comments