Public Transport Access Points in the ​UK

In this project, we are going to visualise and analyse all the access points of public transport in Manchester. This project is focusing on spatial data using geopandas and shapely in python.

we will use the Counties and Unitary Authorities (December 2016) Super Generalised Clipped Boundaries in England and Wales and also national public transport access nodes datasets which they have been provided by the UK government.

checkout the below links to download datasets:

Dataset1

Dataset2

let’s importing and preparing the data!

import pandas as pd
import requests
from zipfile import ZipFile
from io import BytesIO

Next step is importing NapTAN data using the requests library.

r = requests.get('http://naptan.app.dft.gov.uk/DataRequest/Naptan.ashx?format=csv')

Load CSV from Zip File

zf = ZipFile(BytesIO(r.content))
df = pd.read_csv(zf.open('Stops.csv'), encoding = 'windows-1252')
df.head()

Extract ATCOCode, CommonName, StopType, Status and Latitude and Longitude from dataframe:

  • ATCOCode: which is the unique identifier for each station
  • CommonName: which is the text name for the station
  • StopType: which will tell us if it’s the bus stop or the rail platform or the railway entrance
  • Status: which will tell us if it’s an active station or currently suspended
df=df[['ATCOCode','CommonName','StopType','Status','Latitude','Longitude']]
df.head()

Let’s investigate StopType and Status. First, count each unique value in Status column

# count of each unique value in Status column
df.Status.value_counts()

We have 385918 active stations and 49602 stations has been deleted. In addition, 29 stations are currently suspended.

we just have to keep active Status.

df = df[df.Status == 'act']

let’s look at different the stop types

df.StopType.value_counts()

we just want Airport, Ferry Terminals, Railway Stations, Metro stations, Bus Stations and Bus Stops.

df = df[df.StopType.isin(['GAT','FER','RLY','MET','BST','BCT'])]

.

Converting the dataframe to the geodataframe

Let’s import libraries that we will need.

import geopandas as gpd
from shapely.geometry import Point

Next step is creating an ordered list of all the points in the dataframe.

geometry = [Point(xy) for xy in zip(df.Longitude, df.Latitude)]
geometry

Let’s drop Latitude and Longitude from orginal data because we won’t need it anymore.

df = df.drop(['Latitude','Longitude'],axis=1)
df.head()

Now we have to add points to geodataframe.

crs = {'init','epsg:4326'}
stops = gpd.GeoDataFrame(df,crs = crs, geometry = geometry)

.

Create simple plot using geopandas matplotlib interface

Let’s visualise all of the railway stations in the UK.

%matplotlib inline
fig, ax = plt.subplots(figsize=(8, 16))
stops[stops.StopType == 'RLY'].plot(color = 'red', ax = ax)

.

Manipulating Spatial Data

import shapfile for all of the counties in UK. shapefile needs several files (SHP, SHX, DBF) stored in the same directory – need to unzip data fully.

from tempfile import mkdtemp
from os.path import join
tempdir = mkdtemp()

Next step is to import counties shapefile

r = requests.get('http://geoportal1-ons.opendata.arcgis.com/datasets/687f346f5023410ba86615655ff33ca9_3.zip')
zf = ZipFile(BytesIO(r.content))
zf.extractall(path=tempdir)
counties = gpd.read_file(join(tempdir,'Counties_and_Unitary_Authorities_December_2016_Super_Generalised_Clipped_Boundaries_in_England_and_Wales.shp'))
counties.head()

let’s extract the columns that we need.

counties = counties[['ctyua16nm','geometry']]
counties.head()

.

Plotting Spatial Data in Matplotlib

Let’s visualising all of the airport in UK.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 16))
ax.set_aspect('equal')
counties.plot(ax = ax , color = 'Blue')
stops[stops.StopType == 'GAT'].plot(ax = ax, marker = 'o', color = 'red', markersize = 5)

Next step is joining stops with counties to add county/borough information.

stops_with_county = gpd.sjoin(stops,counties)
stops_with_county.head()

Let’s seeing UK boroughs which have most bus stops.

x = stops_with_county.groupby(['ctyua16nm','StopType']).size().to_frame('count').reset_index()
x[x.StopType == 'BCT'].sort_values(['count'], ascending = False)

Let’s visualising all of the Bus Stops, Rail Station, Airport and Metro in Manchester.

fig, ax = plt.subplots(1,figsize=(9, 20))
ax.set_aspect('equal')
counties.loc[counties.ctyua16nm == 'Manchester'].plot(ax = ax , color = 'grey',alpha = 0.4)
stops_with_county.loc[(stops_with_county.StopType == 'BCT')&(stops_with_county.ctyua16nm == 'Manchester')].plot(ax = ax, marker = 'o', color = 'red',markersize = 5,label = 'Bus Stop')
ax.legend()
stops_with_county.loc[(stops_with_county.StopType == 'RLY')&(stops_with_county.ctyua16nm == 'Manchester')].plot(ax = ax,marker = '^',color = 'blue',markersize = 25,label = "rail")
ax.legend()
stops_with_county.loc[(stops_with_county.StopType == 'GAT')&(stops_with_county.ctyua16nm == 'Manchester')].plot(ax = ax,marker = '*',color = 'yellow',markersize = 50,label = "airport")
ax.legend()
stops_with_county.loc[(stops_with_county.StopType == 'MET')&(stops_with_county.ctyua16nm == 'Manchester')].plot(ax = ax,marker = '+',color = 'green',markersize = 70,label = "Metro")
ax.legend()
ax.set_title('Manchester Public Transportation Access Point')
fig
19230cookie-checkPublic Transport Access Points in the ​UK

Leave a Reply

Your email address will not be published. Required fields are marked *