Skip to content

add options to draw control #1035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions folium/plugins/draw.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-


from __future__ import (absolute_import, division, print_function)

import json

from branca.element import CssLink, Element, Figure, JavascriptLink, MacroElement

from jinja2 import Template
Expand All @@ -15,23 +18,43 @@ class Draw(MacroElement):
----------
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 : string, default 'topleft'
Position of control. It can be one of 'topleft', 'toprigth', 'bottomleft', 'bottomright'
See https://leafletjs.com/reference-1.4.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).add_to(m)
>>> 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}}",
draw: {{this.draw_options}},
edit: {{this.edit_options}}
}
// FeatureGroup is to store editable layers.
var drawnItems = new L.featureGroup().addTo({{this._parent.get_name()}});
var {{this.get_name()}} = new L.Control.Draw({
"edit": {"featureGroup": drawnItems}
}).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 (event) {
var layer = event.layer,
type = event.layerType,
Expand All @@ -43,35 +66,40 @@ class Draw(MacroElement):
});
drawnItems.addLayer(layer);
});

{{this._parent.get_name()}}.on('draw:created', function(e) {
drawnItems.addLayer(e.layer);
});

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','data.geojson');
}
{{this._parent.get_name()}}.on('draw:created', function(e) {
drawnItems.addLayer(e.layer);
});
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}}"
);
}
{% endmacro %}
""")

def __init__(self, export=False):
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 = json.dumps(draw_options or {})
self.edit_options = json.dumps(edit_options or {})

def render(self, **kwargs):
super(Draw, self).render()
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.')

figure.header.add_child(
JavascriptLink('https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js')) # noqa

figure.header.add_child(
CssLink('https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.css')) # noqa

Expand Down