Draw subway and tramway with folium

This blog talks about how to draw public transport lines with "folium" module in Python.

A map can clearly present information in terms of geography. Recently I learnt how to draw public transport lines with folium module in Python. In this blog, I will talk about how to do that with the following points:

  • Datasets
  • Draw transport lines with “folium”

Datasets

Before drawing maps, I’ll talk about the datasets which are used for this blog. I downloaded Île-de-France public transport network datasets.

import pandas as pd

idf_transport_geo = pd.read_json('data/transport/idf_ligne-transport.geojson')
idf_station_df = pd.read_csv('data/transport/idf-emplacement-des-gares.csv', sep=';', decimal='.')

20201207-idf-transport-geo

20201207-idf-station-df

idf_station_df['lat'], idf_station_df['lng'] = zip(*idf_station_df['Geo Point'].apply(lambda x: x.split(',')))
idf_station_df['lat'] = idf_station_df['lat'].astype(float)
idf_station_df['lng'] = idf_station_df['lng'].astype(float)
idf_station_df = idf_station_df[idf_station_df['mode_'].isin(['Metro', 'RER'])]

To get stations’ coordinates (latitude, longitude), we split the Geo Point by ,, convert the values to float, and filter only the subway and RER(Réseau Express Régional, English: Regional Express Network).

Draw transport lines with “folium”

import folium

idf_map = folium.Map(location=[48.868, 2.365], zoom_start=12, tiles='Cartodb dark_matter')
for line_segment in idf_transport_geo['features']:
    gjson = folium.features.GeoJson(line_segment['geometry'],
                                    style_function = lambda x: {'color': '#00C4B3',
                                                                'weight': 1.5})
    idf_map.add_child(gjson)

for i, v in idf_station_df.iterrows():
    folium.CircleMarker(location=[v['lat'], v['lng']],
                        radius=1.2,
                        color='#00C4B3',
                        fill_color='#00C4B3',
                        fill=True).add_to(idf_map)

idf_map

I applied geometry which belongs to idf_transport_geo['features'] with folium.features.GeoJson to draw the transport lines, and add station points with folium.CircleMarker, then we get the following map.

20201207-idf-map

Easter egg

I also visualise transport lines of many other cities:

20201207-public-transport-lines

If you are curious about the scripts, you will find them here.

Reference