{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples of plugins usage in folium" ] }, { "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": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import folium\n", "from folium import plugins\n", "\n", "m = folium.Map([45, 3], zoom_start=4)\n", "\n", "plugins.ScrollZoomToggler().add_to(m)\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": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Terminator" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([45, 3], zoom_start=1)\n", "\n", "plugins.Terminator().add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BoatMarker" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([30, 0], zoom_start=3)\n", "\n", "plugins.BoatMarker(\n", " location=(34, -43), heading=45, wind_heading=150, wind_speed=45, color=\"#8f8\"\n", ").add_to(m)\n", "\n", "plugins.BoatMarker(\n", " location=(46, -30), heading=-20, wind_heading=46, wind_speed=25, color=\"#88f\"\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BeautifyIcon" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([45.5, -122], zoom_start=3)\n", "\n", "icon_plane = plugins.BeautifyIcon(\n", " icon=\"plane\", border_color=\"#b3334f\", text_color=\"#b3334f\", icon_shape=\"triangle\"\n", ")\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", "\n", "folium.Marker(location=[46, -122], popup=\"Portland, OR\", icon=icon_plane).add_to(m)\n", "\n", "folium.Marker(location=[50, -122], popup=\"Portland, OR\", icon=icon_number).add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fullscreen" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 6, "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Timestamped GeoJSON" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map(location=[35.68159659061569, 139.76451516151428], zoom_start=16)\n", "\n", "# Lon, Lat order.\n", "lines = [\n", " {\n", " \"coordinates\": [\n", " [139.76451516151428, 35.68159659061569],\n", " [139.75964426994324, 35.682590062684206],\n", " ],\n", " \"dates\": [\"2017-06-02T00:00:00\", \"2017-06-02T00:10:00\"],\n", " \"color\": \"red\",\n", " },\n", " {\n", " \"coordinates\": [\n", " [139.75964426994324, 35.682590062684206],\n", " [139.7575843334198, 35.679505030038506],\n", " ],\n", " \"dates\": [\"2017-06-02T00:10:00\", \"2017-06-02T00:20:00\"],\n", " \"color\": \"blue\",\n", " },\n", " {\n", " \"coordinates\": [\n", " [139.7575843334198, 35.679505030038506],\n", " [139.76337790489197, 35.678040905014065],\n", " ],\n", " \"dates\": [\"2017-06-02T00:20:00\", \"2017-06-02T00:30:00\"],\n", " \"color\": \"green\",\n", " \"weight\": 15,\n", " },\n", " {\n", " \"coordinates\": [\n", " [139.76337790489197, 35.678040905014065],\n", " [139.76451516151428, 35.68159659061569],\n", " ],\n", " \"dates\": [\"2017-06-02T00:30:00\", \"2017-06-02T00:40:00\"],\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", " {\n", " \"type\": \"FeatureCollection\",\n", " \"features\": features,\n", " },\n", " period=\"PT1M\",\n", " add_last_point=True,\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 8, "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\", \"popup\": table, \"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\": \"https://leafletjs.com/examples/geojson/baseball-marker.png\",\n", " \"iconSize\": [20, 20],\n", " },\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", " {\"type\": \"FeatureCollection\", \"features\": features},\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" ] }, { "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 9, "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", "\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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 10, "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", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimap\n", "\n", "Adds a locator minimap to a folium document." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 11, "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" ] }, { "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 synchronized.\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": 12, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 12, "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", "\n", "\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", "For list of all possible keyword options see:\n", "https://github.com/domoritz/leaflet-locatecontrol#possible-options\n", "\n", "To work properly in production, the connection needs to be encrypted (HTTPS), otherwise browser will not\n", "allow users to share their location." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([41.97, 2.81])\n", "\n", "plugins.LocateControl().add_to(m)\n", "\n", "# If you want get the user device position after load the map, set auto_start=True\n", "plugins.LocateControl(auto_start=True).add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SemiCircle\n", "This can be used to display a semicircle or sector on a map. Whilst called SemiCircle it is not limited to 180 degree angles and can be used to display a sector of any angle. \n", "The semicircle is defined with a location (the central point, if it was a full circle), a radius and will either have a direction and an arc **or** a start angle and a stop angle. " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map([45, 3], zoom_start=5)\n", "\n", "plugins.SemiCircle(\n", " (45, 3),\n", " radius=400000,\n", " start_angle=50,\n", " stop_angle=200,\n", " color=\"green\",\n", " fill_color=\"green\",\n", " opacity=0,\n", " popup=\"start angle - 50 degrees, stop angle - 200 degrees\",\n", ").add_to(m)\n", "\n", "plugins.SemiCircle(\n", " (46.5, 9.5),\n", " radius=200000,\n", " direction=360,\n", " arc=90,\n", " color=\"red\",\n", " fill_color=\"red\",\n", " opacity=0,\n", " popup=\"Direction - 0 degrees, arc 90 degrees\",\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geocoder\n", "\n", "Adds a search box to the map to search for geographic features like cities, countries, etc. You can search with names or addresses.\n", "\n", "Uses the Nomatim service from OpenStreetMap. Please respect their usage policy: https://operations.osmfoundation.org/policies/nominatim/" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = folium.Map()\n", "\n", "plugins.Geocoder().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.9.0" } }, "nbformat": 4, "nbformat_minor": 2 }