-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #720 from ocefpaf/Draw
add Draw plugin
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0.4.0+15.ga36370e\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import os\n", | ||
"import folium\n", | ||
"\n", | ||
"\n", | ||
"print(folium.__version__)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVMgPSBmYWxzZTsgTF9OT19UT1VDSCA9IGZhbHNlOyBMX0RJU0FCTEVfM0QgPSBmYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS4yLjAvZGl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mbGV0LmRyYXcvdjAuNC4xMi9kaXN0L2xlYWZsZXQuZHJhdy5jc3MiIC8+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9jZG4ucmF3Z2l0LmNvbS9MZWFmbGV0L0xlYWZsZXQuZHJhdy92MC40LjEyL2Rpc3QvbGVhZmxldC5kcmF3LmpzIj48L3NjcmlwdD4KICAgIDxsaW5rIHJlbD0ic3R5bGVzaGVldCIgaHJlZj0iaHR0cHM6Ly9jZG4ucmF3Z2l0LmNvbS9MZWFmbGV0L0xlYWZsZXQuZHJhdy92MC40LjEyL2Rpc3QvbGVhZmxldC5kcmF3LmNzcyIgLz4KPC9oZWFkPgo8Ym9keT4gICAgCiAgICAKICAgICAgICAgICAgPGRpdiBjbGFzcz0iZm9saXVtLW1hcCIgaWQ9Im1hcF8yMmY5ZWE4ZWU5OTQ0MmRkODVmOGZhMDA4YWIxYWQ1MCIgPjwvZGl2PgogICAgICAgIAo8L2JvZHk+CjxzY3JpcHQ+ICAgIAogICAgCgogICAgICAgICAgICAKICAgICAgICAgICAgICAgIHZhciBib3VuZHMgPSBudWxsOwogICAgICAgICAgICAKCiAgICAgICAgICAgIHZhciBtYXBfMjJmOWVhOGVlOTk0NDJkZDg1ZjhmYTAwOGFiMWFkNTAgPSBMLm1hcCgKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICdtYXBfMjJmOWVhOGVlOTk0NDJkZDg1ZjhmYTAwOGFiMWFkNTAnLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAge2NlbnRlcjogWzAsMF0sCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB6b29tOiAxLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbWF4Qm91bmRzOiBib3VuZHMsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsYXllcnM6IFtdLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgd29ybGRDb3B5SnVtcDogZmFsc2UsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjcnM6IEwuQ1JTLkVQU0czODU3CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICAKICAgICAgICAKICAgIAogICAgICAgICAgICB2YXIgdGlsZV9sYXllcl9mMzI0NWFjMjAyZWE0MTI3YmNjMjBlYTFjYjJlOTJmMSA9IEwudGlsZUxheWVyKAogICAgICAgICAgICAgICAgJ2h0dHBzOi8ve3N9LnRpbGUub3BlbnN0cmVldG1hcC5vcmcve3p9L3t4fS97eX0ucG5nJywKICAgICAgICAgICAgICAgIHsKICAiYXR0cmlidXRpb24iOiBudWxsLAogICJkZXRlY3RSZXRpbmEiOiBmYWxzZSwKICAibWF4Wm9vbSI6IDE4LAogICJtaW5ab29tIjogMSwKICAibm9XcmFwIjogZmFsc2UsCiAgInN1YmRvbWFpbnMiOiAiYWJjIgp9CiAgICAgICAgICAgICAgICApLmFkZFRvKG1hcF8yMmY5ZWE4ZWU5OTQ0MmRkODVmOGZhMDA4YWIxYWQ1MCk7CiAgICAgICAgCiAgICAKICAgICAgICAgICAgLy8gRmVhdHVyZUdyb3VwIGlzIHRvIHN0b3JlIGVkaXRhYmxlIGxheWVycy4KICAgICAgICAgICAgdmFyIGRyYXduSXRlbXMgPSBuZXcgTC5mZWF0dXJlR3JvdXAoKS5hZGRUbyhtYXBfMjJmOWVhOGVlOTk0NDJkZDg1ZjhmYTAwOGFiMWFkNTApOwogICAgICAgICAgICB2YXIgZHJhd19jb250cm9sXzYyNjI1OTA3NmMxODQzMzJhYjBiYmIxZmU4MGJlNzE3ID0gbmV3IEwuQ29udHJvbC5EcmF3KHsKICAgICAgICAgICAgICAgICJlZGl0IjogeyJmZWF0dXJlR3JvdXAiOiBkcmF3bkl0ZW1zfQogICAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgIG1hcF8yMmY5ZWE4ZWU5OTQ0MmRkODVmOGZhMDA4YWIxYWQ1MC5hZGRDb250cm9sKGRyYXdfY29udHJvbF82MjYyNTkwNzZjMTg0MzMyYWIwYmJiMWZlODBiZTcxNyk7CiAgICAgICAgICAgIG1hcF8yMmY5ZWE4ZWU5OTQ0MmRkODVmOGZhMDA4YWIxYWQ1MC5vbihMLkRyYXcuRXZlbnQuQ1JFQVRFRCwgZnVuY3Rpb24gKGV2ZW50KSB7CiAgICAgICAgICAgICAgdmFyIGxheWVyID0gZXZlbnQubGF5ZXIsCiAgICAgICAgICAgICAgICAgIHR5cGUgPSBldmVudC5sYXllclR5cGUsCiAgICAgICAgICAgICAgICAgIGNvb3JkczsKICAgICAgICAgICAgICB2YXIgY29vcmRzID0gSlNPTi5zdHJpbmdpZnkobGF5ZXIudG9HZW9KU09OKCkpOwogICAgICAgICAgICAgIGxheWVyLm9uKCdjbGljaycsIGZ1bmN0aW9uKCkgewogICAgICAgICAgICAgICAgYWxlcnQoY29vcmRzKTsKICAgICAgICAgICAgICAgIGNvbnNvbGUubG9nKGNvb3Jkcyk7CiAgICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgICAgZHJhd25JdGVtcy5hZGRMYXllcihsYXllcik7CiAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgCjwvc2NyaXB0Pg==\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>" | ||
], | ||
"text/plain": [ | ||
"<folium.folium.Map at 0x7f10abc787f0>" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from folium.plugins import Draw\n", | ||
"\n", | ||
"m = folium.Map()\n", | ||
"\n", | ||
"draw = Draw()\n", | ||
"\n", | ||
"draw.add_to(m)\n", | ||
"\n", | ||
"m.save(os.path.join('results', 'Draw.html'))\n", | ||
"\n", | ||
"m" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import (absolute_import, division, print_function) | ||
|
||
from branca.element import CssLink, Figure, JavascriptLink, MacroElement | ||
|
||
from jinja2 import Template | ||
|
||
|
||
class Draw(MacroElement): | ||
""" | ||
Vector drawing and editing plugin for Leaflet. | ||
Examples | ||
-------- | ||
>>> m = folium.Map() | ||
>>> Draw().draw.add_to(m) | ||
For more info please check | ||
https://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html | ||
""" | ||
def __init__(self): | ||
super(Draw, self).__init__() | ||
self._name = 'DrawControl' | ||
|
||
self._template = Template(u""" | ||
{% macro script(this, kwargs) %} | ||
// 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} | ||
}); | ||
{{this._parent.get_name()}}.addControl({{this.get_name()}}); | ||
{{this._parent.get_name()}}.on(L.Draw.Event.CREATED, function (event) { | ||
var layer = event.layer, | ||
type = event.layerType, | ||
coords; | ||
var coords = JSON.stringify(layer.toGeoJSON()); | ||
layer.on('click', function() { | ||
alert(coords); | ||
console.log(coords); | ||
}); | ||
drawnItems.addLayer(layer); | ||
}); | ||
{% endmacro %} | ||
""") | ||
|
||
def render(self, **kwargs): | ||
super(Draw, self).render() | ||
|
||
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://cdn.rawgit.com/Leaflet/Leaflet.draw/v0.4.12/dist/leaflet.draw.js')) # noqa | ||
|
||
figure.header.add_child( | ||
CssLink('https://cdn.rawgit.com/Leaflet/Leaflet.draw/v0.4.12/dist/leaflet.draw.css')) # noqa |