Examples of plugins usage in folium


In [1]:
import os

import numpy as np

import folium
from folium import plugins

print(folium.__version__)


0.11.0+16.g585337f

In this notebook we show a few illustrations of folium's plugin extensions. These are available after importing folium.plugins.

ScrollZoomToggler

Adds a button to enable/disable zoom scrolling.


In [2]:
m = folium.Map([45, 3], zoom_start=4)

plugins.ScrollZoomToggler().add_to(m)

m.save(os.path.join('results', 'Plugins_0.html'))

m


Out[2]:
Make this Notebook Trusted to load map: File -> Trust Notebook

MarkerCluster

Adds a MarkerCluster layer on the map.


In [3]:
N = 100
data = np.array(
    [
        np.random.uniform(low=35, high=60, size=N),  # Random latitudes in Europe.
        np.random.uniform(low=-12, high=30, size=N),  # Random longitudes in Europe.
    ]
).T
popups = [str(i) for i in range(N)] # Popups texts are simple numbers.

m = folium.Map([45, 3], zoom_start=4)

plugins.MarkerCluster(data, popups=popups).add_to(m)

m.save(os.path.join('results', 'Plugins_1.html'))
m


Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Terminator


In [4]:
m = folium.Map([45, 3], zoom_start=1)

plugins.Terminator().add_to(m)

m.save(os.path.join('results', 'Plugins_2.html'))
m


Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook

BoatMarker


In [5]:
m = folium.Map([30, 0], zoom_start=3)

plugins.BoatMarker(
    location=(34, -43),
    heading=45,
    wind_heading=150,
    wind_speed=45,
    color='#8f8'
).add_to(m)

plugins.BoatMarker(
    location=(46, -30),
    heading=-20,
    wind_heading=46,
    wind_speed=25,
    color='#88f'
).add_to(m)

m.save(os.path.join('results', 'Plugins_3.html'))
m


Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook

BeautifyIcon


In [6]:
m = folium.Map([45.5, -122], zoom_start=3)

icon_plane = plugins.BeautifyIcon(
    icon='plane',
    border_color='#b3334f',
    text_color='#b3334f',
    icon_shape='triangle')

icon_number = plugins.BeautifyIcon(
    border_color='#00ABDC',
    text_color='#00ABDC',
    number=10,
    inner_icon_style='margin-top:0;')

folium.Marker(
    location=[46, -122],
    popup='Portland, OR',
    icon=icon_plane
).add_to(m)

folium.Marker(
    location=[50, -122],
    popup='Portland, OR',
    icon=icon_number
).add_to(m)

m.save(os.path.join('results', 'Plugins_4.html'))
m


Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Fullscreen


In [7]:
m = folium.Map(location=[41.9, -97.3], zoom_start=4)

plugins.Fullscreen(
    position='topright',
    title='Expand me',
    title_cancel='Exit me',
    force_separate_button=True
).add_to(m)

m.save(os.path.join('results', 'Plugins_5.html'))
m


Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Timestamped GeoJSON


In [8]:
m = folium.Map(
    location=[35.68159659061569, 139.76451516151428],
    zoom_start=16
)

# Lon, Lat order.
lines = [
    {
        'coordinates': [
            [139.76451516151428, 35.68159659061569],
            [139.75964426994324, 35.682590062684206],
        ],
        'dates': [
            '2017-06-02T00:00:00',
            '2017-06-02T00:10:00'
        ],
        'color': 'red'
    },
    {
        'coordinates': [
            [139.75964426994324, 35.682590062684206],
            [139.7575843334198, 35.679505030038506],
        ],
        'dates': [
            '2017-06-02T00:10:00',
            '2017-06-02T00:20:00'
        ],
        'color': 'blue'
    },
    {
        'coordinates': [
            [139.7575843334198, 35.679505030038506],
            [139.76337790489197, 35.678040905014065],
        ],
        'dates': [
            '2017-06-02T00:20:00',
            '2017-06-02T00:30:00'
        ],
        'color': 'green',
        'weight': 15,
    },
    {
        'coordinates': [
            [139.76337790489197, 35.678040905014065],
            [139.76451516151428, 35.68159659061569],
        ],
        'dates': [
            '2017-06-02T00:30:00',
            '2017-06-02T00:40:00'
        ],
        'color': '#FFFFFF',
    },
]

features = [
    {
        'type': 'Feature',
        'geometry': {
            'type': 'LineString',
            'coordinates': line['coordinates'],
        },
        'properties': {
            'times': line['dates'],
            'style': {
                'color': line['color'],
                'weight': line['weight'] if 'weight' in line else 5
            }
        }
    }
    for line in lines
]

plugins.TimestampedGeoJson({
    'type': 'FeatureCollection',
    'features': features,
}, period='PT1M', add_last_point=True).add_to(m)

m.save(os.path.join('results', 'Plugins_6.html'))
m


Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook

In [9]:
table = """\
<table style=\'width:100%\'>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>
"""

points = [
    {
        'time': '2017-06-02',
        'popup': '<h1>address1</h1>',
        'coordinates': [-2.548828, 51.467697]
    },
    {
        'time': '2017-07-02',
        'popup': '<h2 style=\'color:blue;\'>address2<h2>',
        'coordinates': [-0.087891, 51.536086]
    },
    {
        'time': '2017-08-02',
        'popup': '<h2 style=\'color:orange;\'>address3<h2>',
        'coordinates': [-6.240234, 53.383328]
    },
    {
        'time': '2017-09-02',
        'popup': '<h2 style=\'color:green;\'>address4<h2>',
        'coordinates': [-1.40625, 60.261617]},
    {
        'time': '2017-10-02',
        'popup': table,
        'coordinates': [-1.516113, 53.800651]}
]

features = [
    {
        'type': 'Feature',
        'geometry': {
            'type': 'Point',
            'coordinates': point['coordinates'],
        },
        'properties': {
            'time': point['time'],
            'popup': point['popup'],
            'id': 'house',
            'icon': 'marker',
            'iconstyle': {
                'iconUrl': 'https://leafletjs.com/examples/geojson/baseball-marker.png',
                'iconSize': [20, 20]
            }
        }
    } for point in points
]

features.append(
    {
        'type': 'Feature',
        'geometry': {
            'type': 'LineString',
            'coordinates': [
                [-2.548828, 51.467697],
                [-0.087891, 51.536086],
                [-6.240234, 53.383328],
                [-1.40625, 60.261617],
                [-1.516113, 53.800651]
            ],
        },
        'properties': {
            'popup': 'Current address',
            'times': [
                '2017-06-02',
                '2017-07-02',
                '2017-08-02',
                '2017-09-02',
                '2017-10-02'
            ],
            'icon': 'circle',
            'iconstyle': {
                'fillColor': 'green',
                'fillOpacity': 0.6,
                'stroke': 'false',
                'radius': 13
            },
            'style': {'weight': 0},
            'id': 'man'
        }
    }
)

m = folium.Map(
    location=[56.096555, -3.64746],
    tiles='cartodbpositron',
    zoom_start=5,
)

plugins.TimestampedGeoJson(
    {
        'type': 'FeatureCollection',
        'features': features
    },
    period='P1M',
    add_last_point=True,
    auto_play=False,
    loop=False,
    max_speed=1,
    loop_button=True,
    date_options='YYYY/MM/DD',
    time_slider_drag_update=True,
    duration='P2M'
).add_to(m)

m.save(os.path.join('results', 'Plugins_7.html'))
m


Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook

FeatureGroupSubGroup

Sub categories

Disable all markers in the category, or just one of the subgroup.


In [10]:
m = folium.Map(location=[0, 0], zoom_start=6)

fg = folium.FeatureGroup(name='groups')
m.add_child(fg)

g1 = plugins.FeatureGroupSubGroup(fg, 'group1')
m.add_child(g1)

g2 = plugins.FeatureGroupSubGroup(fg, 'group2')
m.add_child(g2)

folium.Marker([-1, -1]).add_to(g1)
folium.Marker([1, 1]).add_to(g1)

folium.Marker([-1, 1]).add_to(g2)
folium.Marker([1, -1]).add_to(g2)

folium.LayerControl(collapsed=False).add_to(m)
m.save(os.path.join('results', 'Plugins_8.html'))
m


Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Marker clusters across groups

Create two subgroups, but cluster markers together.


In [11]:
m = folium.Map(location=[0, 0], zoom_start=6)

mcg = folium.plugins.MarkerCluster(control=False)
m.add_child(mcg)

g1 = folium.plugins.FeatureGroupSubGroup(mcg, 'group1')
m.add_child(g1)

g2 = folium.plugins.FeatureGroupSubGroup(mcg, 'group2')
m.add_child(g2)

folium.Marker([-1, -1]).add_to(g1)
folium.Marker([1, 1]).add_to(g1)

folium.Marker([-1, 1]).add_to(g2)
folium.Marker([1, -1]).add_to(g2)

folium.LayerControl(collapsed=False).add_to(m)
m.save(os.path.join('results', 'Plugins_9.html'))
m


Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Minimap

Adds a locator minimap to a folium document.


In [12]:
m = folium.Map(location=(30, 20), zoom_start=4)

minimap = plugins.MiniMap()
m.add_child(minimap)

m.save(os.path.join('results', 'Plugins_10.html'))
m


Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook

DualMap

The DualMap plugin can be used to display two maps side by side, where panning and zooming is syncronized.

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.


In [13]:
m = plugins.DualMap(location=(52.1, 5.1), tiles=None, zoom_start=8)

folium.TileLayer('cartodbpositron').add_to(m.m2)
folium.TileLayer('openstreetmap').add_to(m)

fg_both = folium.FeatureGroup(name='markers_both').add_to(m)
fg_1 = folium.FeatureGroup(name='markers_1').add_to(m.m1)
fg_2 = folium.FeatureGroup(name='markers_2').add_to(m.m2)

icon_red = folium.Icon(color='red')
folium.Marker((52, 5), tooltip='both', icon=icon_red).add_to(fg_both)
folium.Marker((52.4, 5), tooltip='left').add_to(fg_1)
folium.Marker((52, 5.4), tooltip='right').add_to(fg_2)

folium.LayerControl(collapsed=False).add_to(m)
m.save(os.path.join('results', 'Plugins_11.html'))
m


Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Locate control

Adds a control button that when clicked, the user device geolocation is displayed. For list of all possible keyword options see: https://github.com/domoritz/leaflet-locatecontrol#possible-options

To work properly in production, the connection needs to be encrypted (HTTPS), otherwise browser will not allow users to share their location.


In [14]:
m = folium.Map([41.97,2.81])

plugins.LocateControl().add_to(m)

# If you want get the user device positon after load the map, set auto_start=True
plugins.LocateControl(auto_start=True).add_to(m)

m


Out[14]:
Make this Notebook Trusted to load map: File -> Trust Notebook

SemiCircle

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. 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.


In [15]:
m = folium.Map([45, 3], zoom_start=5)

plugins.SemiCircle((45, 3), radius=400000, start_angle=50, stop_angle=200,
                   color='green', fill_color='green', opacity=0,
                   popup='start angle - 50 degrees, stop angle - 200 degrees'
                  ).add_to(m)

plugins.SemiCircle((46.5, 9.5), radius=200000,  direction=360, arc=90,
                   color='red', fill_color='red', opacity=0,
                   popup='Direction - 0 degrees, arc 90 degrees'
                  ).add_to(m)

m


Out[15]:
Make this Notebook Trusted to load map: File -> Trust Notebook

Geocoder

Adds a search box to the map to search for geographic features like cities, countries, etc. You can search with names or addresses.

Uses the Nomatim service from OpenStreetMap. Please respect their usage policy: https://operations.osmfoundation.org/policies/nominatim/


In [16]:
m = folium.Map()

plugins.Geocoder().add_to(m)

m


Out[16]:
Make this Notebook Trusted to load map: File -> Trust Notebook