In [1]:
import os
import folium

print(folium.__version__)


0.5.0+102.g9e2ee74

In [2]:
import pandas as pd


df = pd.DataFrame.from_csv(
    os.path.join('data', 'highlight_flight_trajectories.csv')
)


/home/filipe/miniconda3/envs/FOLIUM/lib/python3.6/site-packages/ipykernel_launcher.py:5: FutureWarning: from_csv is deprecated. Please use read_csv(...) instead. Note that some of the default arguments are different, so please refer to the documentation for from_csv when changing your function calls
  """

Let us take a glance at the data. Each row represents the trajectory of a flight, and the last column contains the coordinates of the flight path in GeoJSON format.


In [3]:
df


Out[3]:
dep dest geojson
0 Place_MontpellierMediterranee_Airport Place_BastiaPoretta_Airport {"type": "LineString", "coordinates": [[3.9613...
1 Place_Bristol___Lulsgate Place_TenerifeSur_ReinaSofia_Airport {"type": "LineString", "coordinates": [[-2.719...
2 Place_Valencia_Manises_Airport Place_Bucuresti_HenriCoanda_Airport {"type": "LineString", "coordinates": [[-0.481...

In [4]:
m = folium.Map(
    location=[40, 10],
    zoom_start=4,
    control_scale=True,
    prefer_canvas=True
)


def style_function(feature):
    return {
        'fillColor': '#ffaf00',
        'color': 'blue',
        'weight': 1.5,
        'dashArray': '5, 5'
    }


def highlight_function(feature):
    return {
        'fillColor': '#ffaf00',
        'color': 'green',
        'weight': 3,
        'dashArray': '5, 5'
    }


for index, row in df.iterrows():
    c = folium.GeoJson(
        row['geojson'],
        name=('{}{}'.format(row['dep'], row['dest'])),
        overlay=True,
        style_function=style_function,
        highlight_function=highlight_function
    )
    folium.Popup('{}\n{}'.format(row['dep'], row['dest'])).add_to(c)
    c.add_to(m)

folium.LayerControl().add_to(m)
m.save(os.path.join('results', 'Highlight_Function.html'))

m


Out[4]: