-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_data.py
More file actions
65 lines (59 loc) · 2.79 KB
/
build_data.py
File metadata and controls
65 lines (59 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import csv
import os
class Station:
"""
The Station class contains four attributes: id, name, position, and links.
Position is a binary combination of longitude and latitude
and links are a list of stations adjacent to the Station object
"""
def __init__(self, id, name, position):
self.id = id
self.name = name
self.position = position
self.links = set()
def build_data():
"""
builds the 'map' by reading the data files
Returns:
station(dict[str, Station]): A mapping between station names and station objects of the name
underground_lines(dict[str, dict]): A mapping between underground lines name and a dictionary containing relevant
information about underground lines
"""
stations = {}
underground_lines = {}
rootdir = os.path.dirname(__file__)
r = csv.reader(open(os.path.join(rootdir, 'london/underground_stations.csv')))
next(r) # jump the first line
for record in r:
id = int(record[0])
lat = float(record[1])
lon = float(record[2])
name = record[3]
stations[id] = Station(id, name, (lat, lon))
r = csv.reader(open(os.path.join(rootdir, 'london/underground_routes.csv')))
next(r) # jump the first line
for id1, id2, lineNumber in r:
id1 = int(id1)
id2 = int(id2)
stations[id1].links.add(stations[id2])
stations[id2].links.add(stations[id1])
lineNumber = int(lineNumber)
if lineNumber not in underground_lines:
underground_lines[lineNumber] = {'lat': [stations[id1].position[0], stations[id2].position[0], None],
'lon': [stations[id1].position[1], stations[id2].position[1], None],
'stations': {stations[id1].name, stations[id2].name}}
else:
underground_lines[lineNumber]['lat'].extend([stations[id1].position[0], stations[id2].position[0], None])
underground_lines[lineNumber]['lon'].extend([stations[id1].position[1], stations[id2].position[1], None])
underground_lines[lineNumber]['stations'].add(stations[id1].name)
underground_lines[lineNumber]['stations'].add(stations[id2].name)
r = csv.reader(open(os.path.join(rootdir, 'london/underground_lines.csv')))
next(r) # jump the first line
for lineNumber, name, colour, stripe in r:
lineNumber = int(lineNumber)
underground_lines[lineNumber]['name'] = name
underground_lines[lineNumber]['colour'] = colour.upper()
underground_lines[lineNumber]['stripe'] = stripe
stations = {v.name: v for k, v in stations.items()}
underground_lines = {v['name']: v for k, v in underground_lines.items()}
return stations, underground_lines