This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfs2trekker.py
executable file
·67 lines (55 loc) · 2.08 KB
/
gtfs2trekker.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
#!/usr/bin/python
import gtfs
import sys
import struct
import time
import sqlalchemy
from optparse import OptionParser
import logging
usage = "usage: %prog [options] <gtfs db>"
parser = OptionParser(usage)
parser.add_option("-v", action="store_true", dest="verbose",
help='Verbose (turn on debugging messages)')
parser.add_option("-i", action="store_true", dest="id_as_code",
help='Use stop id as stop code')
(options, args) = parser.parse_args()
if len(args) < 1:
parser.error("incorrect number of arguments")
exit(1)
logger = logging.getLogger(sys.argv[0])
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
logger.addHandler(ch)
if options.verbose:
logger.setLevel(logging.DEBUG)
logger.debug("Loading schedule")
sched = gtfs.Schedule(args[0])
logger.debug("Creating route index")
routemap = {}
for route in sched.routes:
routemap[route.route_id] = route.route_short_name
logger.debug("Creating stop index")
for trip in sched.trips:
for stop_time in trip.stop_times:
stop_id = stop_time.stop_id
if not routemap.get(stop_id):
routemap[stop_id] = set([])
routemap[stop_id].add(routemap[trip.route_id])
logger.debug("Writing stops")
for stop in sched.stops:
routes_for_stop = ''
if routemap.get(stop.stop_id):
routes_for_stop = ' '.join(routemap[stop.stop_id])
routes_for_stop = routes_for_stop[0:100].replace(',','\,')
stop_name = stop.stop_name
if stop.stop_code:
stop_name = (stop.stop_code + ': ' + stop.stop_name)
elif options.id_as_code:
stop_name = (stop.stop_id + ': ' + stop.stop_name)
stop_name = stop_name[0:40].replace(',','\,')
# FIXME: For type, we hardcode to bus station (0037), but trekker supports
# rail (0036) and a general "transit" category which I guess we should
# use for ferries or whatever else
print "%s,%s,%s,0,0037,%s,,,,,,,,1," % (stop.stop_lon, stop.stop_lat,
stop_name,
routes_for_stop)