Skip to content

Instantly share code, notes, and snippets.

@wheresalice
Last active September 30, 2019 04:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wheresalice/0f0cee89e2f4a37490387384a7554ade to your computer and use it in GitHub Desktop.
Save wheresalice/0f0cee89e2f4a37490387384a7554ade to your computer and use it in GitHub Desktop.
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