In [1]:
import os
import folium

print(folium.__version__)


0.5.0+114.gd5c3342.dirty

Examples of plugins usage in folium


In [2]:
from folium import plugins

In this notebook we show a few illustrations of folium's plugin extensions.

This is a development notebook

ScrollZoomToggler

Adds a button to enable/disable zoom scrolling.


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

plugins.ScrollZoomToggler().add_to(m)

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

m


Out[3]:

MarkerCluster

Adds a MarkerCluster layer on the map.


In [4]:
import numpy as np


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.
        range(N),  # Popups texts are simple numbers.
    ]
).T

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

plugins.MarkerCluster(data).add_to(m)

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

m


Out[4]:

Terminator


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

plugins.Terminator().add_to(m)

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

m


Out[5]:

Leaflet.boatmarker


In [6]:
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[6]:

Leaflet.BeautifyIcon


In [7]:
from folium.plugins.beautify_icon import BeautifyIcon
from folium import Marker
m = folium.Map([45.5, -122], zoom_start=3)

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

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

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

Marker(
    location=[50, -122],
    popup='Portland, OR',
    icon=icon_number
).add_to(m)
m.save(os.path.join('results', 'Plugins_4.html'))

m


Out[7]:

Fullscreen


In [8]:
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[8]:

Timestamped GeoJSON


In [9]:
from folium import plugins


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[9]:

In [10]:
from folium import plugins


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 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>""",
     "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': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.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[10]:

FeatureGroupSubGroup

Sub categories

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


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

fg = folium.FeatureGroup()
m.add_child(fg)

g1 = plugins.FeatureGroupSubGroup(fg, 'g1')
m.add_child(g1)
g2 = plugins.FeatureGroupSubGroup(fg, 'g2')
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)

l = folium.LayerControl().add_to(m)

m


Out[11]:

Marker clusters across groups

Create two subgroups, but cluster markers together.


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

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

g1 = folium.plugins.FeatureGroupSubGroup(mcg, 'g1')
m.add_child(g1)
g2 = folium.plugins.FeatureGroupSubGroup(mcg, 'g2')
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)

l = folium.LayerControl().add_to(m)

m


Out[12]: