-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatch.py
334 lines (251 loc) · 9.61 KB
/
match.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
import csv
from transitfeed import Loader, util
from time import gmtime
from pytz import timezone
from datetime import datetime, date
import os
from shapely.geometry import LineString, Point
import simplejson as json
import bisect
def get_trip_instances( points, header ):
# chops a list of points up into a list of trip instances
# this assumes the trip is ordered by time and trip instance
tripinst_ix = header.index("tripInst")
tripinst_id = None
for i, point in enumerate( points ):
curtripinst_id = point[tripinst_ix]
if curtripinst_id!=tripinst_id:
if tripinst_id is not None:
yield tripinst_id, tripinst
tripinst = []
tripinst_id = point[tripinst_ix]
tripinst.append( point )
yield tripinst_id, tripinst
def get_tripinst_date( tripinst, time_ix, tz ):
tripStart = datetime.fromtimestamp( float(tripinst[0][time_ix])/1000.0, tz )
return tripStart.date()
def parse_gtfs_date( datestr ):
year = datestr[0:4]
month = datestr[4:6]
day = datestr[6:]
return date(int(year),int(month),int(day))
class Trip(object):
def __init__(self, trip_id, direction_id, service_id):
self.trip_id = trip_id
self.direction_id = direction_id
self.service_id = service_id
self.stop_times = []
def add_stoptime(self, trip_id, arrival_time, departure_time, stop_id, stop_sequence):
self.stop_times.append( (trip_id, arrival_time, departure_time, stop_id, stop_sequence) )
def _sort(self):
self.stop_times.sort( key=lambda x:x[4] )
def _to_points( self, stops, time=True ):
for trip_id,arrival_time,departure_time,stop_id,stop_sequence in self.stop_times:
lat, lon = stops[stop_id]
if time:
yield lon, lat, arrival_time
else:
yield lon, lat
def get_shape( self, stops, time=True ):
return list( self._to_points( stops, time ) )
def midway_time( self ):
start_time = self.stop_times[0][2]
end_time = self.stop_times[-1][1]
return (start_time+end_time)/2.0
def __float__(self):
return float(self.midway_time())
def __lt__( self, b ):
return float(self) < b
def __gt__( self, b ):
return float(self) > b
def __eq__( self, b ):
return float(self) == b
def __ne__( self, b ):
return float(self) != b
def __ge__( self, b ):
return float(self) >= b
def __le__( self, b ):
return float(self) <= b
def __repr__(self):
first_st = self.stop_times[0]
last_st = self.stop_times[-1]
return "<Trip id:%s dir:%s sid:%s %s@%s->%s@%s>"%(self.trip_id, self.direction_id, self.service_id, first_st[3],first_st[2],last_st[3],last_st[1])
class TripGroup(object):
# A TripGroup is a set of trips that are equivalent at arrival time i.e. if you're waiting
# at a stop for a vehicle implementing one trip but a vehicle serving a different trip shows up
# it'll work.
# TripGroups are generally all the trips in a GTFS with the same route_id, service_id, and direction_id.
def __init__(self, direction_id, service_id):
self.direction_id = direction_id
self.service_id = service_id
self.trips = []
self.rep_shape_cache = None
self.sorted = False
def add_trip( self, trip ):
self.sorted = False
self.trips.append( trip )
def _sort(self):
self.trips.sort( key=lambda x:x.midway_time() )
self.sorted = True
@property
def rep_trip(self):
"""get a representitive trip"""
return self.trips[0]
def cache_rep_shape(self,stops):
self.rep_shape_cache = self.get_rep_shape(stops)
@property
def rep_shape(self):
if self.rep_shape_cache is None:
raise Exception("rep_shape not in cache; run cache_rep_shape() first")
return self.rep_shape_cache
def get_rep_shape(self, stops):
return LineString( self.rep_trip.get_shape(stops) )
def same_direction(self, points):
"""returns True if the string of points is traveling in the same direction as the tripgroup"""
start_point = Point( points[0] )
end_point = Point( points[-1] )
return self.rep_shape.project( start_point ) < self.rep_shape.project( end_point )
def nearest(self, tt):
# find the position in the trip list closest to it
ix = bisect.bisect( self.trips, tt )
if ix==0:
return self.trips[0]
if ix==len(self.trips):
return self.trips[-1]
left = abs( float(self.trips[ix-1])-tt )
right = abs( float(self.trips[ix])-tt )
if left<right:
return self.trips[ix-1]
else:
return self.trips[ix]
def __repr__(self):
return "<TripGroup dir:%s sid:%s>"%(self.direction_id, self.service_id)
def compile_trips( gtfs, gtfs_dir, route_short_name, verbose=True ):
# get the route object from the gtfs file corresponding to route_short_name
routes = dict( [(x.route_short_name, x) for x in gtfs.GetRouteList()] )
route = routes[ route_short_name ]
# generate a Trip object for each gtfs trip...
trips = {}
for gtfs_trip in route.trips:
trip = Trip( gtfs_trip.trip_id, gtfs_trip.direction_id, gtfs_trip.service_id )
trips[trip.trip_id]=trip
# fill out the trips with stop_times
fn = os.path.join( gtfs_dir, "stop_times.txt" )
fp = open( fn )
rd = csv.reader( fp )
header = rd.next()
trip_id_ix = header.index("trip_id")
arrival_time_ix = header.index("arrival_time")
departure_time_ix = header.index("departure_time")
stop_id_ix = header.index("stop_id")
stop_sequence_ix = header.index("stop_sequence")
for i, row in enumerate( rd ):
if i%1000==0:
print "\r%d"%i,; sys.stdout.flush()
trip_id = row[trip_id_ix]
if trip_id not in trips:
continue
arrival_time = util.TimeToSecondsSinceMidnight( row[arrival_time_ix] )
departure_time = util.TimeToSecondsSinceMidnight( row[departure_time_ix] )
stop_id = row[stop_id_ix]
stop_sequence = int( row[stop_sequence_ix] )
trips[trip_id].add_stoptime( trip_id,arrival_time,departure_time,stop_id,stop_sequence )
for trip in trips.values():
trip._sort()
return trips.values()
def tripinst_to_points( tripinst, lat_ix, lon_ix, time_ix, tz, day_cutoff=4*3600 ):
for point in tripinst:
lat = float(point[lat_ix])
lon = float(point[lon_ix])
dt = datetime.fromtimestamp( float(point[time_ix])/1000.0, tz )
secs_since_midnight = dt.hour*3600+dt.minute*60+dt.second+dt.microsecond/1000000.0
if secs_since_midnight < day_cutoff:
secs_since_midnight += 3600*24
yield (lon,lat,secs_since_midnight)
def main(fn_in, gtfs_dir, route_id, fn_out):
ll = Loader( gtfs_dir, load_stop_times=False )
sched = ll.Load()
# compile stop_id->(lat,lon) for convenience
stops = dict( [(stop.stop_id, (stop.stop_lat, stop.stop_lon)) for stop in sched.GetStopList()] )
# sift through the huge stop_times.txt file looking for stop_times that have one of those trip_ids,
# and group them by trip_id
print "reading gtfs stop_times into trips..."
trips = compile_trips( sched, gtfs_dir, route_id )
print "done"
print "filing trips away into affinity groups..."
tripgroups = {}
for trip in trips:
sig = (trip.direction_id, trip.service_id)
if sig not in tripgroups:
tripgroups[sig] = TripGroup( trip.direction_id, trip.service_id )
tripgroups[sig].add_trip( trip )
for tripgroup in tripgroups.values():
tripgroup._sort()
tripgroup.cache_rep_shape(stops)
print "done"
print "indexing tripgroups by service id..."
serviceid_tripgroups = {}
for (direction_id, service_id), tripgroup in tripgroups.items():
if service_id not in serviceid_tripgroups:
serviceid_tripgroups[service_id] = []
serviceid_tripgroups[service_id].append( tripgroup )
print "done"
tzname = sched.GetDefaultAgency().agency_timezone
tz = timezone( tzname )
rd = csv.reader( open(fn_in) )
header = rd.next()
time_ix = header.index("time")
lat_ix = header.index("lat")
lon_ix = header.index("lon")
start_date, end_date = [parse_gtfs_date(x) for x in sched.GetDateRange()]
# dict of date->[service periods]
serviceperiods = dict( sched.GetServicePeriodsActiveEachDate( start_date, end_date ) )
print "matching trip instances with trips..."
fpout = open( fn_out, "w" )
for i, (tripinst_id, tripinst) in enumerate( get_trip_instances(rd,header) ):
if i%100==0:
print "\r%d"%(i+1,),; sys.stdout.flush()
tripinst_date = get_tripinst_date( tripinst, time_ix, tz )
service_periods = serviceperiods.get( tripinst_date )
if service_periods is None:
continue
tripinst_shape = list( tripinst_to_points( tripinst, lat_ix, lon_ix, time_ix, tz ) )
# print "tripinst shape %s"%tripinst_shape
#pick out the tripinst midpoint
start_time = tripinst_shape[0][2]
end_time = tripinst_shape[-1][2]
# print "start time %s"%start_time
# print "end time %s"%end_time
mid_time = (end_time+start_time)/2.0
matched_trips = []
# for each service period running on the day of tripinst
for service_period in service_periods:
# print "checking service period ", service_period.service_id
# find the tripgroup running on that service period
for tripgroup in serviceid_tripgroups.get(service_period.service_id,[]):
# print "checking with trip group ", tripgroup
# print "same direction?"
if tripgroup.same_direction( tripinst_shape ):
# print "yes"
# print "tripgroup candidate trips..."
# for trip in tripgroup.trips:
# print trip
# print "looking for nearest trip to %s"%mid_time
nearest_trip = tripgroup.nearest( mid_time )
# print "nearest trip: ", nearest_trip
matched_trips.append( nearest_trip )
if len(matched_trips)==0:
continue
matched_trip = min(matched_trips, key=lambda x:abs(float(x)-mid_time))
fpout.write( "%s,%s\n"%(tripinst_id, matched_trip.trip_id) ); fpout.flush()
print "done"
if __name__=='__main__':
import sys
if len(sys.argv)<5:
print "usage: python cmd.py chained_csv_fn gtfs_dir route_id fn_out"
exit()
chained_csv_fn = sys.argv[1]
gtfs_dir = sys.argv[2]
route_id = sys.argv[3]
fn_out = sys.argv[4]
main( chained_csv_fn, gtfs_dir, route_id, fn_out )