diff --git a/CHANGES.txt b/CHANGES.txt index 751519e86..d33ce1dca 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,17 @@ +0.5.0 +~~~~~ + +- Added `Draw` plugin (ocefpaf #720) +- Better handling of URL input (ocefpaf #717) +- Versioned docs! Visit http://python-visualization.github.io/folium/docs-v{{version}} + or simply http://python-visualization.github.io/folium/ for the latest version. + +Bug Fixes + +- Fix `VideoOverlay` import (ocefpaf #719) +- Fix `choropleth` docstring (lsetiawan #713) +- Fix `choropleth` name in `LayerControl` (ocefpaf #493) + 0.4.0 ~~~~~ - Optional `iconCreateFunction` for `MarkerCluster` to customize the icons (odovad #701) diff --git a/examples/Draw.ipynb b/examples/Draw.ipynb new file mode 100644 index 000000000..246afd733 --- /dev/null +++ b/examples/Draw.ipynb @@ -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": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "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 +} diff --git a/folium/plugins/__init__.py b/folium/plugins/__init__.py index ca34224a4..7da2b0276 100644 --- a/folium/plugins/__init__.py +++ b/folium/plugins/__init__.py @@ -11,6 +11,7 @@ from __future__ import (absolute_import, division, print_function) from folium.plugins.boat_marker import BoatMarker +from folium.plugins.draw import Draw from folium.plugins.fast_marker_cluster import FastMarkerCluster from folium.plugins.float_image import FloatImage from folium.plugins.fullscreen import Fullscreen @@ -28,6 +29,7 @@ __all__ = [ 'BoatMarker', + 'Draw', 'FastMarkerCluster', 'FloatImage', 'Fullscreen', diff --git a/folium/plugins/draw.py b/folium/plugins/draw.py new file mode 100644 index 000000000..c833c090a --- /dev/null +++ b/folium/plugins/draw.py @@ -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