GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - rosedillon/rail-codes: Convert UK railway station codes in Python: TIPLOC, CRS (three-letter code), NLC and STANOX, from Network Rail CORPUS.

BranchesTags
Open more actions menu

Latest commit

 

History

3 Commits
3 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
scripts
scripts
 
 
src/rail_codes
src/rail_codes
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

rail-codes

PyPI Python CI Licence: GPL-3.0

Convert between the codes Britain's railway uses for the same place.

A station has up to five. The timetable calls Leeds LEEDS (its TIPLOC), passengers and most open data call it LDS (its CRS code), the fares feed calls it 8487 (its NLC), train movement messages call it 17132 (its STANOX), and European systems use 84870 (its UIC code). Any project that joins two rail datasets has to translate between them.

Network Rail's CORPUS extract lists them all, but it has two gaps that lose real trains. Platform TIPLOCs such as WATRLMN (Waterloo main) and VICTRIE (Victoria eastern) have no CRS in it, so every call there drops out of a join. And some stations have a second CRS for a separate set of platforms: the timetable books Southeastern high-speed trains at SPX, while passenger counts and station usage figures only know St Pancras as STP. This package ships CORPUS with both gaps filled.

from rail_codes import crs_for_tiploc, find, station_crs

crs_for_tiploc("WATRLMN")  # 'WAT'
station_crs("SPX")  # 'STP'
find("LDS")[0].stanox  # '17132'

Install

Python 3.11 or later.

pip install rail-codes

It has no dependencies. The data is about 650 kB and loads in a tenth of a second.

Command line

Give it any code and it prints the others:

$ rail-codes LDS WATRLMN SPX 2246
TIPLOC   CRS  NLC     STANOX  UIC    NAME
LEEDS    LDS  848700  17132   84870  LEEDS
WATRLMN  WAT  559801  87212   55984  WATERLOO MAIN
STPANCI  SPX  154000  63631   15400  ST PANCRAS
LVRPLSH  LIV  224600  36151   22460  LIVERPOOL LIME STREET

A CRS code lists every TIPLOC at the station:

$ rail-codes CLJ
TIPLOC   CRS  NLC     STANOX  UIC    NAME
CLPHMJN  CLJ  559500  87219   55950  CLAPHAM JUNCTION LONDON
CLPHMJ1  CLJ  559518  87219   55954  CLAPHAM JUNCTION PLATS 0-2
CLPHMJC  CLJ  559569  87219   55956  CLAPHAM JUNCTION (C)
CLPHMJW  CLJ  559572  87219   55957  CLAPHAM JN (WINDSOR)
CLPHMJM  CLJ  559591  87219   55958  CLAPHAM JN (MAIN)

Only the first of those has a CRS in CORPUS. Add --json for machine-readable output. An unknown code is reported and the exit status is 1.

Platform TIPLOCs

Large stations have one TIPLOC per group of platforms, and CORPUS gives a CRS to only one of them. The timetable books trains at the others, so code that maps TIPLOCs through CORPUS alone loses Waterloo's main line, Victoria's eastern side, most of Clapham Junction and both halves of London Bridge. On a national timetable that is every South Western Railway train into London.

The 25 TIPLOCs in platform_tiplocs.csv are the ones the May 2026 timetable books at a station that CORPUS leaves without a CRS. find and crs_for_tiploc include them.

Second codes

second_codes.csv lists 29 codes that belong to another station's platforms:

Code Station Platforms
SPX STP St Pancras International, high-speed platforms
SPL STP St Pancras low level (Thameslink)
PDX PAD Paddington, Elizabeth line
LVL LIV Liverpool Lime Street low level
GCL GLC Glasgow Central low level
...

station_crs maps a second code to its station and passes anything else through, so it is safe to apply to a whole column:

calls["crs"] = calls["crs"].map(station_crs)

Whether to merge them depends on the job. Counting trains at St Pancras wants SPX and STP together; telling Eurostar's platforms from Thameslink's wants them apart. tiploc_to_crs() gives codes as published and leaves that choice to you.

HEZ (Heworth) and NCZ (Newcastle) are not on the list. They are Tyne and Wear Metro platforms, which most rail datasets leave out.

NLCs

CORPUS gives 6-digit NLCs. The fares feed uses 4 digits, which are the first four of the station's xx xx 00 entry: Liverpool Lime Street is 224600 in CORPUS and 2246 in the fares feed. Location.nlc4 gives that form, and find accepts either. The other NLCs under the same prefix (155501 to 155598 for St Pancras) are offices, sidings and accounting codes, and have no 4-digit form.

Python API

Name
find(code) every Location with that TIPLOC, CRS, NLC or STANOX
crs_for_tiploc(tiploc) the CRS, or None
tiplocs_for_crs(crs) every TIPLOC at a station
tiploc_to_crs() the whole TIPLOC to CRS map, as a dict
station_crs(crs) a second code's station, or the code itself
second_codes() the second-code table, as a dict
locations() every CORPUS entry
Location tiploc, crs, nlc, nlc4, stanox, uic, name, short_name

Updating the data

CORPUS is republished every few months. Download CORPUSExtract.json from the Network Rail Open Data feeds (a free account) and run:

python scripts/build_data.py CORPUSExtract.json

To refresh the platform TIPLOCs as well, pass a calls table from cif-timetable as a second argument. The second-code table is maintained by hand.

Related packages

Part of a set of Python packages for Great Britain's open rail data:

  • bplan-geography, minimum running times from Network Rail's BPLAN extract
  • cif-timetable, calling points and trains per route from the Network Rail CIF timetable
  • hsp-client, how late UK trains actually ran, from the National Rail HSP API
  • rail-loading, UK train operators' passenger loading data in one schema
  • rsp-fares-feed, every fare between two stations, from the National Rail fares feed
  • tag-databook, DfT TAG Data Book appraisal values without opening Excel
  • track-distance, station distances along the rails, from Network Rail's track model
  • orr-station-usage, entries and exits at every station by year, from ORR tables 1410 and 1415

Development

git clone https://github.com/rosedillon/rail-codes
cd rail-codes
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest
ruff check . && ruff format --check . && mypy src

Licence

The code is GPL-3.0. See LICENSE.

The bundled CORPUS data contains information of Network Rail Infrastructure Limited licensed under the Open Government Licence v3.0.

About

Convert UK railway station codes in Python: TIPLOC, CRS (three-letter code), NLC and STANOX, from Network Rail CORPUS.

pypi.org/project/rail-codes/

Topics

corpuscrsgreat-britainlookupnetwork-railopen-datapythonrailrailwaystanoxstation-codestiploctransporttransport-dataukuk-rail

Resources

Readme
GPL-3.0 license
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.