Load in python libraries¶

In [14]:
import pandas as pd
import geopandas as gpd
import sqlalchemy
import folium

Load data¶

In [15]:
lsoa_gdf = gpd.read_file("data/lsoa_2011_bsol.shp")
sites = pd.read_csv("data/ae_sites.csv")
travel_times = pd.read_csv("data/travel_matrix_minutes.csv")

Limit travel times to BSOL A&E sites¶

In [16]:
# Bring through only travel times for BSOL A&E sites
travel_times_bsol_ae = travel_times.loc[
    :, # keep all rows
    ['LSOA'] + sites.postcode.to_list() # bring through LSOA and all site postcodes
].set_index('LSOA') # ignore LSOA

Calculate shortest travel time¶

In [17]:
### Calculate shortest travel time by LSOA
travel_times_bsol_ae['shortest_distance'] = (
    travel_times_bsol_ae.min(axis=1) # across the row
)

Add travel times to shapefile¶

In [18]:
travel_time_gdf = lsoa_gdf.merge(
    travel_times_bsol_ae,
    left_on = "LSOA11NM",
    right_on = "LSOA",
    how="left"
)

Map travel times¶

In [ ]:
# Create base map and zoom level
travel_times_map = folium.Map(
    location=[
        travel_time_gdf.geometry.centroid.y.mean(),
        travel_time_gdf.geometry.centroid.x.mean()
    ],
    zoom_start=11,
    tiles='cartodbpositron'
    )

# Add heat map
folium.Choropleth(
    geo_data=travel_time_gdf, # dataframe with geometry in it
    data=travel_time_gdf, # dataframe with data in - may be the same dataframe or a different one
    columns=['LSOA11C', 'shortest_distance'], # [key (field for geometry), field to plot]
    key_on='feature.properties.LSOA11C',
    fill_color="YlOrRd",
    bins=[0,5,10,15,20,25,30],
    legend_name='Travel Time (mins)'
    ).add_to(travel_times_map)

# Add markers
for i in range(0, len(sites)):
    folium.Marker(
        location=[sites.iloc[i]['lat'], sites.iloc[i]['long']],
        popup=sites.iloc[i]['site_name']
    ).add_to(travel_times_map)

# Define tooltip
folium.GeoJson(
    travel_time_gdf,
    name='geojson',
    popup=folium.GeoJsonPopup(
        fields=['LSOA11C', 'shortest_distance'], aliases=['LSOA Code', 'Travel Time (mins)']
    ),
    style_function=lambda x: { # 
        'fillColor': '#ffffff',
        'color': '#000000',
        'weight': 0,
        'fillOpacity': 0
    }
).add_to(travel_times_map)

# Add a title
title_html = '''
             <div style="position: fixed; 
                         bottom: 10px; 
                         left: 10px; 
                         z-index: 1000; ">
             <h3 style="margin: 0; font-family: sans-serif; color: black;">BSOL Travel Times Map</h3>
             </div>
             '''

travel_times_map.get_root().html.add_child(folium.Element(title_html))
In [20]:
travel_times_map

# travel_times_map.save("travel_times_map.html") # create html output
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook