Skip to content

Instantly share code, notes, and snippets.

@wheresalice
Last active Sep 30, 2019
Embed
What would you like to do?
Produce a CSV with Tiploc code, Longitude, and Latitude using Naptan data
.DEFAULT_GOAL := tiploc_locations.csv
naptan.zip:
wget -O naptan.zip naptan.app.dft.gov.uk/DataRequest/Naptan.ashx?format=csv
Stops.csv: naptan.zip
unzip -o naptan.zip
tiploc_locations.csv: Stops.csv
python run.py
import csv
def load_rail_references():
stations = []
with open('RailReferences.csv', 'r') as csv_file:
reader = csv.DictReader(csv_file)
for line in reader:
stations.append([line['AtcoCode'], line['TiplocCode']])
return stations
def load_stops():
stops = {}
with open('Stops.csv', encoding='latin-1') as csv_file:
reader = csv.DictReader(csv_file)
for line in reader:
if line['StopType'] == 'RLY':
stops[line['ATCOCode']] = [line['Longitude'], line['Latitude']]
return stops
# print(load_rail_references())
# print(load_stops())
if __name__ == '__main__':
stations = load_rail_references()
stops = load_stops()
geo_stops = []
for station in stations:
try:
geo_stops.append([station[1], stops[station[0]][0], stops[station[0]][1]])
except:
print(f"{station[1]} not found")
with open('tiploc_locations.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Tiploc', 'Longitude', 'Latitude'])
for station in geo_stops:
writer.writerow(station)
# print(geo_stops)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment