-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
74 lines (61 loc) · 2.38 KB
/
visualize.py
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
65
66
67
68
69
70
71
72
73
74
import os
import signal
import sys
import shutil
import http.server
import socketserver
from tools.conversion_tools import convert_calendars, convert_routes, convert_shapes, convert_trips
def signal_handler(sig, frame):
print('\n\nClosing visualizer, deleting visualizer files. Please wait a tiny bit.')
try:
shutil.rmtree('.visualizefiles')
os.remove('visualizer.html')
except:
print('No previous visualized files found on close. Weird.')
exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
gtfs_dir = ''
try:
in_dir = sys.argv[1]
except:
print("Error: must specify GTFS directory as arg")
exit(1)
try:
shutil.rmtree('.visualizefiles')
except:
print('No previous visualized files found. Generating vis files.')
os.mkdir('.visualizefiles')
calendar_file = os.path.join(in_dir, "calendar.txt")
calendar_dates_file = os.path.join(in_dir, "calendar_dates.txt")
cal_dir = os.path.join('.visualizefiles', 'service_jkeys_by_date')
os.mkdir(cal_dir)
convert_calendars(calendar_file, calendar_dates_file, cal_dir)
routes_file = os.path.join(in_dir, "routes.txt")
routes_dir = os.path.join('.visualizefiles', 'routes')
os.mkdir(routes_dir)
routes_obj = convert_routes(routes_file, routes_dir)
shapes_file = os.path.join(in_dir, "shapes.txt")
shapes_dir = os.path.join('.visualizefiles', 'shapes')
os.mkdir(shapes_dir)
convert_shapes(shapes_file, shapes_dir)
stops_file = os.path.join(in_dir, "stops.txt")
trips_file = os.path.join(in_dir, "trips.txt")
stop_times_file = os.path.join(in_dir, "stop_times.txt")
stops_dir = os.path.join('.visualizefiles', 'stops')
trips_dir = os.path.join('.visualizefiles', 'trips')
itin_dir = os.path.join('.visualizefiles', 'itineraries')
trips_hour_dir = os.path.join('.visualizefiles', 'trips_by_route_by_hour')
os.mkdir(stops_dir)
os.mkdir(trips_dir)
os.mkdir(itin_dir)
os.mkdir(trips_hour_dir)
convert_trips(stops_file, trips_file, stop_times_file, stops_dir, trips_dir, itin_dir, trips_hour_dir, routes_dir, routes_obj)
# start server
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print('go to localhost:8000/visualizer.html')
httpd.serve_forever()
# catch sigterm, sigint, and then kill server and remove .visualizefiles dir
# shutil.rmtree('.visualizefiles')