{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples of plugins usage in folium" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.8.3+52.g2758dc7.dirty\n" ] } ], "source": [ "import os\n", "\n", "import numpy as np\n", "\n", "import folium\n", "from folium import plugins\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we show a few illustrations of folium's plugin extensions. These are available after importing `folium.plugins`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ScrollZoomToggler\n", "Adds a button to enable/disable zoom scrolling." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "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": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N = 100\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", " ]\n", ").T\n", "popups = [str(i) for i in range(N)] # Popups texts are simple numbers.\n", "\n", "m = folium.Map([45, 3], zoom_start=4)\n", "\n", "plugins.MarkerCluster(data, popups=popups).add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_1.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Terminator" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "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", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BoatMarker" ] }, { "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([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", "m.save(os.path.join('results', 'Plugins_3.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BeautifyIcon" ] }, { "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([45.5, -122], zoom_start=3)\n", "\n", "icon_plane = plugins.BeautifyIcon(\n", " icon='plane',\n", " border_color='#b3334f',\n", " text_color='#b3334f',\n", " icon_shape='triangle')\n", "\n", "icon_number = plugins.BeautifyIcon(\n", " border_color='#00ABDC',\n", " text_color='#00ABDC',\n", " number=10,\n", " inner_icon_style='margin-top:0;')\n", "\n", "folium.Marker(\n", " location=[46, -122],\n", " popup='Portland, OR',\n", " icon=icon_plane\n", ").add_to(m)\n", "\n", "folium.Marker(\n", " location=[50, -122],\n", " popup='Portland, OR',\n", " icon=icon_number\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_4.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fullscreen" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "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\n", ").add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_5.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timestamped GeoJSON" ] }, { "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(\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", "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", "plugins.TimestampedGeoJson({\n", " 'type': 'FeatureCollection',\n", " 'features': features,\n", "}, period='PT1M', add_last_point=True).add_to(m)\n", "\n", "m.save(os.path.join('results', 'Plugins_6.html'))\n", "m" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table = \"\"\"\\\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
FirstnameLastnameAge
JillSmith50
EveJackson94
\n", "\"\"\"\n", "\n", "points = [\n", " {\n", " 'time': '2017-06-02',\n", " 'popup': '

address1

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

address2

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

address3

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

address4

',\n", " 'coordinates': [-1.40625, 60.261617]},\n", " {\n", " 'time': '2017-10-02',\n", " 'popup': table,\n", " 'coordinates': [-1.516113, 53.800651]}\n", "]\n", "\n", "features = [\n", " {\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", "\n", "features.append(\n", " {\n", " 'type': 'Feature',\n", " 'geometry': {\n", " 'type': 'LineString',\n", " 'coordinates': [\n", " [-2.548828, 51.467697],\n", " [-0.087891, 51.536086],\n", " [-6.240234, 53.383328],\n", " [-1.40625, 60.261617],\n", " [-1.516113, 53.800651]\n", " ],\n", " },\n", " 'properties': {\n", " 'popup': 'Current address',\n", " 'times': [\n", " '2017-06-02',\n", " '2017-07-02',\n", " '2017-08-02',\n", " '2017-09-02',\n", " '2017-10-02'\n", " ],\n", " 'icon': 'circle',\n", " 'iconstyle': {\n", " 'fillColor': 'green',\n", " 'fillOpacity': 0.6,\n", " 'stroke': 'false',\n", " 'radius': 13\n", " },\n", " 'style': {'weight': 0},\n", " 'id': 'man'\n", " }\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", "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[0, 0], zoom_start=6)\n", "\n", "fg = folium.FeatureGroup(name='groups')\n", "m.add_child(fg)\n", "\n", "g1 = plugins.FeatureGroupSubGroup(fg, 'group1')\n", "m.add_child(g1)\n", "\n", "g2 = plugins.FeatureGroupSubGroup(fg, 'group2')\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", "folium.LayerControl(collapsed=False).add_to(m)\n", "m.save(os.path.join('results', 'Plugins_8.html'))\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": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[0, 0], zoom_start=6)\n", "\n", "mcg = folium.plugins.MarkerCluster(control=False)\n", "m.add_child(mcg)\n", "\n", "g1 = folium.plugins.FeatureGroupSubGroup(mcg, 'group1')\n", "m.add_child(g1)\n", "\n", "g2 = folium.plugins.FeatureGroupSubGroup(mcg, 'group2')\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", "folium.LayerControl(collapsed=False).add_to(m)\n", "m.save(os.path.join('results', 'Plugins_9.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimap\n", "\n", "Adds a locator minimap to a folium document." ] }, { "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(location=(30, 20), zoom_start=4)\n", "\n", "minimap = plugins.MiniMap()\n", "m.add_child(minimap)\n", "\n", "m.save(os.path.join('results', 'Plugins_10.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DualMap\n", "The DualMap plugin can be used to display two maps side by side, where panning and zooming is syncronized.\n", "\n", "The `DualMap` class can be used just like the normal `Map` class. The two sub-maps can be accessed with its `m1` and `m2` attributes." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = plugins.DualMap(location=(52.1, 5.1), tiles=None, zoom_start=8)\n", "\n", "folium.TileLayer('cartodbpositron').add_to(m.m2)\n", "folium.TileLayer('openstreetmap').add_to(m)\n", "\n", "fg_both = folium.FeatureGroup(name='markers_both').add_to(m)\n", "fg_1 = folium.FeatureGroup(name='markers_1').add_to(m.m1)\n", "fg_2 = folium.FeatureGroup(name='markers_2').add_to(m.m2)\n", "\n", "icon_red = folium.Icon(color='red')\n", "folium.Marker((52, 5), tooltip='both', icon=icon_red).add_to(fg_both)\n", "folium.Marker((52.4, 5), tooltip='left').add_to(fg_1)\n", "folium.Marker((52, 5.4), tooltip='right').add_to(fg_2)\n", "\n", "folium.LayerControl(collapsed=False).add_to(m)\n", "m.save(os.path.join('results', 'Plugins_11.html'))\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Locate control\n", "\n", "Adds a control button that when clicked, the user device geolocation is displayed.\n", "\n", "To work properly in production, the connection needs to be encrypted, otherwise browser will not\n", "allow users to share their location." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([41.97,2.81])\n", "\n", "plugins.LocateControl().add_to(m)\n", "\n", "# If used with Draw plugin, LocateControl needs to be added before it.\n", "# Draw().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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }