{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.5.0+114.gd5c3342.dirty\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples of plugins usage in folium" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from folium import plugins" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we show a few illustrations of folium's plugin extensions.\n", "\n", "This is a development notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ScrollZoomToggler\n", "Adds a button to enable/disable zoom scrolling." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([45, 3], zoom_start=4)\n", "\n", "plugins.ScrollZoomToggler().add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_0.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MarkerCluster" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adds a MarkerCluster layer on the map." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "N = 100\n", "\n", "data = np.array(\n", " [\n", " np.random.uniform(low=35, high=60, size=N), # Random latitudes in Europe.\n", " np.random.uniform(low=-12, high=30, size=N), # Random longitudes in Europe.\n", " range(N), # Popups texts are simple numbers.\n", " ]\n", ").T\n", "\n", "m = folium.Map([45, 3], zoom_start=4)\n", "\n", "plugins.MarkerCluster(data).add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_1.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Terminator" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([45, 3], zoom_start=1)\n", "\n", "plugins.Terminator().add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_2.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Leaflet.boatmarker" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([30, 0], zoom_start=3)\n", "\n", "plugins.BoatMarker(\n", " location=(34, -43),\n", " heading=45,\n", " wind_heading=150,\n", " wind_speed=45,\n", " color='#8f8'\n", ").add_to(m)\n", "\n", "plugins.BoatMarker(\n", " location=(46, -30),\n", " heading=-20,\n", " wind_heading=46,\n", " wind_speed=25,\n", " color='#88f'\n", ").add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', 'Plugins_3.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Leaflet.BeautifyIcon" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium.plugins.beautify_icon import BeautifyIcon\n", "from folium import Marker\n", "m = folium.Map([45.5, -122], zoom_start=3)\n", "\n", "icon_plane = BeautifyIcon(\n", " icon='plane',\n", " border_color='#b3334f',\n", " text_color='#b3334f',\n", " icon_shape='triangle')\n", "\n", "icon_number = BeautifyIcon(\n", " border_color='#00ABDC',\n", " text_color='#00ABDC',\n", " number=10,\n", " inner_icon_style='margin-top:0;')\n", "\n", "Marker(\n", " location=[46, -122],\n", " popup='Portland, OR',\n", " icon=icon_plane\n", ").add_to(m)\n", "\n", "Marker(\n", " location=[50, -122],\n", " popup='Portland, OR',\n", " icon=icon_number\n", ").add_to(m)\n", "m.save(os.path.join('results', 'Plugins_4.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fullscreen" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[41.9, -97.3], zoom_start=4)\n", "\n", "plugins.Fullscreen(\n", " position='topright',\n", " title='Expand me',\n", " title_cancel='Exit me',\n", " force_separate_button=True).add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', 'Plugins_5.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timestamped GeoJSON" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium import plugins\n", "\n", "\n", "m = folium.Map(\n", " location=[35.68159659061569, 139.76451516151428],\n", " zoom_start=16\n", ")\n", "\n", "# Lon, Lat order.\n", "lines = [\n", " {\n", " 'coordinates': [\n", " [139.76451516151428, 35.68159659061569],\n", " [139.75964426994324, 35.682590062684206],\n", " ],\n", " 'dates': [\n", " '2017-06-02T00:00:00',\n", " '2017-06-02T00:10:00'\n", " ],\n", " 'color': 'red'\n", " },\n", " {\n", " 'coordinates': [\n", " [139.75964426994324, 35.682590062684206],\n", " [139.7575843334198, 35.679505030038506],\n", " ],\n", " 'dates': [\n", " '2017-06-02T00:10:00',\n", " '2017-06-02T00:20:00'\n", " ],\n", " 'color': 'blue'\n", " },\n", " {\n", " 'coordinates': [\n", " [139.7575843334198, 35.679505030038506],\n", " [139.76337790489197, 35.678040905014065],\n", " ],\n", " 'dates': [\n", " '2017-06-02T00:20:00',\n", " '2017-06-02T00:30:00'\n", " ],\n", " 'color': 'green',\n", " 'weight': 15,\n", " },\n", " {\n", " 'coordinates': [\n", " [139.76337790489197, 35.678040905014065],\n", " [139.76451516151428, 35.68159659061569],\n", " ],\n", " 'dates': [\n", " '2017-06-02T00:30:00',\n", " '2017-06-02T00:40:00'\n", " ],\n", " 'color': '#FFFFFF',\n", " },\n", "]\n", "\n", "\n", "features = [\n", " {\n", " 'type': 'Feature',\n", " 'geometry': {\n", " 'type': 'LineString',\n", " 'coordinates': line['coordinates'],\n", " },\n", " 'properties': {\n", " 'times': line['dates'],\n", " 'style': {\n", " 'color': line['color'],\n", " 'weight': line['weight'] if 'weight' in line else 5\n", " }\n", " }\n", " }\n", " for line in lines\n", "]\n", "\n", "\n", "plugins.TimestampedGeoJson({\n", " 'type': 'FeatureCollection',\n", " 'features': features,\n", "}, period='PT1M', add_last_point=True).add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', 'Plugins_6.html'))\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from folium import plugins\n", "\n", "\n", "points = [\n", " {\"time\":'2017-06-02',\n", " \"popup\":\"

address1

\",\n", " \"coordinates\":[-2.548828, 51.467697]},\n", " \n", " {\"time\":'2017-07-02',\n", " \"popup\":\"

address2

\",\n", " \"coordinates\":[-0.087891, 51.536086]},\n", " \n", " {\"time\":'2017-08-02',\n", " \"popup\":\"

address3

\",\n", " \"coordinates\":[-6.240234, 53.383328]},\n", " \n", " {\"time\":'2017-09-02',\n", " \"popup\":\"

address4

\",\n", " \"coordinates\":[-1.40625, 60.261617]},\n", " \n", " {\"time\":'2017-10-02',\n", " \"popup\":\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
FirstnameLastnameAge
JillSmith50
EveJackson94
\"\"\",\n", " \"coordinates\":[-1.516113, 53.800651]}\n", "]\n", "\n", "features = [{\n", " \"type\": \"Feature\",\n", " \"geometry\": {\n", " \"type\": \"Point\",\n", " \"coordinates\": point['coordinates'],\n", " },\n", " \"properties\": {\n", " \"time\": point['time'],\n", " \"popup\": point['popup'],\n", " \"id\":\"house\",\n", " 'icon':'marker',\n", " 'iconstyle':{\n", " 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png',\n", " 'iconSize': [20, 20]\n", " }\n", " }\n", " } for point in points]\n", "\n", "features.append({\n", " 'type': 'Feature',\n", " 'geometry': {\n", " 'type': 'LineString',\n", " 'coordinates': [[-2.548828, 51.467697], [-0.087891, 51.536086], \n", " [-6.240234, 53.383328], [-1.40625, 60.261617],\n", " [-1.516113, 53.800651]\n", " ],\n", " },\n", " 'properties': {\n", " 'popup':'Current address',\n", " 'times': ['2017-06-02', '2017-07-02',\n", " '2017-08-02', '2017-09-02',\n", " '2017-10-02'],\n", " 'icon':'circle',\n", " 'iconstyle':{\n", " 'fillColor': 'green',\n", " 'fillOpacity': 0.6,\n", " 'stroke': 'false',\n", " 'radius': 13\n", " },\n", " 'style': {'weight':0\n", " },\n", " 'id':'man'\n", " }\n", " })\n", "\n", "m = folium.Map(\n", " location=[56.096555, -3.64746],\n", " tiles = 'cartodbpositron',\n", " zoom_start=5\n", ")\n", "\n", "plugins.TimestampedGeoJson(\n", " {\n", " 'type': 'FeatureCollection',\n", " 'features': features\n", " },\n", " period='P1M',\n", " add_last_point=True,\n", " auto_play=False,\n", " loop=False,\n", " max_speed=1,\n", " loop_button=True,\n", " date_options='YYYY/MM/DD',\n", " time_slider_drag_update=True,\n", " duration='P2M'\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_7.html'))\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## FeatureGroupSubGroup\n", "\n", "### Sub categories\n", "\n", "Disable all markers in the category, or just one of the subgroup." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(\n", " location=[0, 0],\n", " zoom_start=6\n", ")\n", "\n", "fg = folium.FeatureGroup()\n", "m.add_child(fg)\n", "\n", "g1 = plugins.FeatureGroupSubGroup(fg, 'g1')\n", "m.add_child(g1)\n", "g2 = plugins.FeatureGroupSubGroup(fg, 'g2')\n", "m.add_child(g2)\n", "\n", "folium.Marker([-1,-1]).add_to(g1)\n", "folium.Marker([1,1]).add_to(g1)\n", "\n", "folium.Marker([-1,1]).add_to(g2)\n", "folium.Marker([1,-1]).add_to(g2)\n", "\n", "l = folium.LayerControl().add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Marker clusters across groups\n", "\n", "Create two subgroups, but cluster markers together." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(\n", " location=[0, 0],\n", " zoom_start=6\n", ")\n", "\n", "mcg = folium.plugins.MarkerCluster(control=False)\n", "m.add_child(mcg)\n", "\n", "g1 = folium.plugins.FeatureGroupSubGroup(mcg, 'g1')\n", "m.add_child(g1)\n", "g2 = folium.plugins.FeatureGroupSubGroup(mcg, 'g2')\n", "m.add_child(g2)\n", "\n", "folium.Marker([-1,-1]).add_to(g1)\n", "folium.Marker([1,1]).add_to(g1)\n", "\n", "folium.Marker([-1,1]).add_to(g2)\n", "folium.Marker([1,-1]).add_to(g2)\n", "\n", "l = folium.LayerControl().add_to(m)\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.5" } }, "nbformat": 4, "nbformat_minor": 1 }