-
Notifications
You must be signed in to change notification settings - Fork 1
/
ptv2gtfs.py
executable file
·363 lines (303 loc) · 12 KB
/
ptv2gtfs.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python2
# PTV DB to GTFS converter for Victorian Public Transport
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sqlite3
import re
import datetime
from optparse import OptionParser
import transitfeed
SETTINGS = {
'train': {
'file': 'Metlink-Train.sql',
'name': 'Metro Trains',
'prefix': 'train',
'system': 'Subway',
'state': 'VIC',
},
'tram': {
'file': 'Metlink-Tram.sql',
'name': 'Yarra Trams',
'prefix': 'tram',
'system': 'Tram',
'state': 'VIC',
},
'bus': {
'file': 'Metlink-Bus.sql',
'name': 'Melbourne Bus',
'prefix': 'bus',
'system': 'Bus',
'state': 'VIC',
},
'vline': {
'file': 'Metlink-VLine.sql',
'name': 'V/Line',
'prefix': 'vline',
'system': 'regional',
'state': 'VIC',
}
}
def process_routes(cur, config, schedule):
# Routes
print "..processing routes.."
cur.execute("select * from " + config['prefix'] + "_lines")
for row in cur:
# Start with a sensible default of using the route_long_name.
# You don't want to wait hours and hours to find out that
# neither route_long_name or route_short_name has been set.
route_long_name = row["line_name"].strip()
route_short_name = None
route_description = None
# Train
if config['system'] == 'Subway':
route_long_name = row['line_name'].strip()
route_type = config['system']
# Tram
elif config['system'] == 'Tram':
# 1 - East Coburb - South Melbourne
temp_name = row['line_name'].split(' - ')
route_short_name = temp_name[0]
route_long_name = temp_name[1]
if len(temp_name) == 3:
route_long_name = '%s to %s' % (route_long_name, temp_name[2])
# V/Line
elif config['system'] == 'V/Line':
# Echuca-Moama - Melbourne via Shepparton
m = re.match('(.*\s-\s.*) via (.*)', row["line_name"].strip())
if m:
short_name = m.groups()[0].replace(" - ", " to ")
route_name = '%s Line' % short_name
long_name = m.group(0).replace(" - ", " to ")
# Bus
elif config['system'] == 'Bus':
# 123 - Here - There
# 843-845-849-861 - Dandenong - Endeavour Hills via Doveton
m = re.match('([\d{3}?-]+) - (.*)', row["line_name"].strip())
if m:
route_short_name = m.groups()[0]
route_long_name = m.groups()[1]
else:
m = re.match('(.*) \(Routes? (.*)\)', row["line_name"].strip())
if m:
route_short_name = m.groups()[1]
route_long_name = m.groups()[0]
else:
m = re.match('(.*) combined - (.*)', row["line_name"].strip())
if m:
route_short_name = m.groups()[0]
route_long_name = m.groups()[1]
else:
# Geelong City - Newtown via Aberdeen St (Anticlockwise Circular - Route 36)
m = re.match('.* - Route (.*)\)', row["line_name"].strip())
if m:
route_short_name = m.groups()[0]
route_long_name = row["line_name"].strip()
route_id = str(row['line_id'])
# Add our route
route = transitfeed.Route(
short_name = route_short_name,
long_name = route_long_name,
route_type = config['system'],
route_id = route_id
)
schedule.AddRouteObject(route)
print "..done processing routes."
def process_stops(cur, config, schedule):
print "..processing stops.."
# Stops
cur.execute("select * from "+ config['prefix'] + "_locations")
for row in cur:
stop_name = None
stop_desc = None
stop_code = None
# Train
if config['system'] == 'Subway':
# Custom name for train stations
stop_name = '%s Station' % row['location_name'].strip()
elif config['system'] == 'Tram':
# Custom name for tram
# Match both:
m = re.match('(.*)\s#(\d+)', row['location_name'].strip())
# Street Name/Other Street #123
if m:
stop_code = m.groups()[1]
stop_name = m.groups()[0]
else:
# 7D-Street Name/Other Street
m = re.match('(\w+)-(.*)', row["location_name"].strip())
try:
stop_code = m.groups()[0]
stop_name = m.groups()[1]
except:
print ("Warning: tram stop entry in DB "\
"seems to be missing stop code:- entering "\
"with name as '%s'" % row["location_name"].strip())
stop_name = row['location_name'].strip()
else:
stop_name = row['location_name'].strip()
stop_id = str(row['location_id'])
lat = row["latitude"]
lng = row["longitude"]
stop = transitfeed.Stop(
stop_id = stop_id,
name = stop_name,
stop_code = stop_code,
lat = lat,
lng = lng,
)
schedule.AddStopObject(stop)
print "..done processing stops."
def process_stoptimes(cur, config, schedule):
print "..processing stoptimes.."
timetables = []
# Lookup which times we have
cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '" + config['prefix'] + "_stops%' ORDER BY name;")
for row in cur:
# Table name is train_stops_monthur, so get the last part
name = row['name'].split('_')[-1]
timetables.append(name)
for timetable in timetables:
print("Processing timetable: %s" % timetable)
service_period = transitfeed.ServicePeriod(id=timetable)
# TODO: Don't hardcode these dates
service_period.SetStartDate('20140101')
service_period.SetEndDate('20141231')
# Set the day of week times
if timetable == 'monthur':
service_period.SetDayOfWeekHasService(0)
service_period.SetDayOfWeekHasService(1)
service_period.SetDayOfWeekHasService(2)
service_period.SetDayOfWeekHasService(3)
elif timetable == 'fri':
service_period.SetDayOfWeekHasService(4)
elif timetable == 'monfri':
service_period.SetWeekdayService()
elif timetable == 'sat':
service_period.SetDayOfWeekHasService(5)
elif timetable == 'sun':
service_period.SetDayOfWeekHasService(6)
else:
print("Error: Timetable %s not defined" % timetable)
schedule.AddServicePeriodObject(service_period, validate=False)
process_stoptime(cur, config, schedule, service_period, timetable)
print "..done processing stoptimes."
PROGRESS_INCS = 20
def process_stoptime(cur, config, schedule, service_period, table):
# Lookup our directions first
directions = {}
cur.execute("select * from "+ config['prefix'] + "_direction")
for row in cur:
direction_id = row["direction_id"]
name = row['direction_name']
directions[direction_id] = name
# Save these for helping us work out services that run past midnight
last_trip_id = 0
last_time = 0
print "..Getting stops database entries.."
cur.execute("select * from "+ config['prefix'] + "_stops_" + table)
rows = cur.fetchall()
num_stop_times = len(rows)
print "..got %d stop times database entries.." % num_stop_times
inc_divisor = num_stop_times / float(PROGRESS_INCS)
next_inc_period = 1
print "..starting processing each stop time entry.."
for ii, row in enumerate(rows):
route_id = str(row['line_id'])
stop_id = str(row['stop_id'])
trip_id = str(row['run_id'])
headsign = directions[int(row["direction"])] # Get the direction from the other table
time = row["time"]
# Now use of Get() API of transitfeed to save search time.
route = schedule.GetRoute(route_id)
stop = schedule.GetStop(stop_id)
# If the next time is past midnight, add more time to take it past 24 hours,
# because GTFS requires it
if time < last_time:
if last_trip_id == trip_id:
time += 86400
last_time = time
last_trip_id = trip_id
try:
trip = schedule.GetTrip(trip_id)
except KeyError:
# If we don't have an existing trip, create a new one and add
trip = route.AddTrip(
schedule,
headsign = headsign,
trip_id = trip_id,
service_period = service_period
)
# We know the stops times are in row order, so we'll
# just make up the sequence here
stop_seq = trip.GetCountStopTimes()
# Not sure what we should do about this
problems = None
stop_time = transitfeed.StopTime(
problems,
stop,
pickup_type = 0, # Regularly scheduled pickup
drop_off_type = 0, # Regularly scheduled drop off
shape_dist_traveled = None,
arrival_secs = time,
departure_secs = time,
stop_time = time,
stop_sequence = stop_seq
)
trip.AddStopTimeObject(stop_time)
if (ii+1) / inc_divisor > next_inc_period:
print "....processed %d stops (%.1f%% of total).." % \
(ii+1, next_inc_period/float(PROGRESS_INCS)*100)
next_inc_period += 1
print "..done processing each stop time entry.."
def process_data(inputdb, config, output):
# Open SqliteDB
con = sqlite3.connect(inputdb)
con.row_factory = sqlite3.Row
cur = con.cursor()
# Create our schedule
schedule = transitfeed.Schedule()
# Agency
schedule.AddAgency(config['name'], "http://ptv.vic.gov.au", "Australia/Melbourne")
process_routes(cur, config, schedule)
process_stops(cur, config, schedule)
process_stoptimes(cur, config, schedule)
accumulator = transitfeed.SimpleProblemAccumulator()
problemReporter = transitfeed.ProblemReporter(accumulator)
print "Validating result..."
schedule.Validate(problems=problemReporter)
print "...done."
print "Writing to file %s ..." % output
schedule.WriteGoogleTransitFeed(output)
print "...done."
if __name__ == "__main__":
usage = "usage: %prog --file <input db> --service <service> --output <output file>"
parser = OptionParser(usage)
parser.add_option('-f', '--file',
dest='inputdb',
help='PTV SQLite3 database file.')
parser.add_option('-s', '--service',
dest='service',
help='Should be train, tram or bus.')
parser.add_option('-o', '--output',
dest='output',
help='Path of output file. Should end in .zip')
(options, args) = parser.parse_args()
if not options.inputdb:
parser.error("You must supply the input database filename.")
if not options.service:
parser.error("You must supply the service type (e.g. train, tram, bus).")
if not options.output:
parser.error("You must supply the output filename.")
config = SETTINGS[options.service]
process_data(options.inputdb, config, options.output)