buses.py

changeset 114
b736478416d4
parent 109
88a5110b66ba
child 127
2bc0529d44a5
equal deleted inserted replaced
113:b8467d51f1b8 114:b736478416d4
8 8
9 def transform_trip_reference(reference): 9 def transform_trip_reference(reference):
10 return reference 10 return reference
11 11
12 class BusTrip: 12 class BusTrip:
13 def __init__(self, reference, route, service, length, block_id): 13 def __init__(self, reference, route, service, length, block_id, shape):
14 self.reference, self.route, self.service, self.block_id = reference, route, service, block_id 14 self.reference, self.route, self.service, self.block_id = reference, route, service, block_id
15 self.length = length 15 self.length = length
16 self.schedule = [] 16 self.schedule = []
17 self.name = transform_trip_reference(reference) 17 self.name = transform_trip_reference(reference)
18 self.shape = str(shape)
18 def __repr__(self): 19 def __repr__(self):
19 return 'all_trips[%r]' % self.name 20 return 'all_trips[%r]' % self.name
20 def contains_stop(self, stop): 21 def contains_stop(self, stop):
21 for halt in self.schedule: 22 for halt in self.schedule:
22 if halt.stop is stop: 23 if halt.stop is stop:
217 result = [] 218 result = []
218 for stop in self.stops: 219 for stop in self.stops:
219 result += stop.schedule(max_amount = max_amount) 220 result += stop.schedule(max_amount = max_amount)
220 result.sort(key = lambda schedule_entry: schedule_entry['time']) 221 result.sort(key = lambda schedule_entry: schedule_entry['time'])
221 return result[:max_amount] 222 return result[:max_amount]
223 def __lt__(self, other):
224 return (self.name and other.name) and (self.name < other.name) or (id(self) < id(other))
222 225
223 class CustomBusStopCluster(BusStopCluster): 226 class CustomBusStopCluster(BusStopCluster):
224 def __init__(self, *, name, stops): 227 def __init__(self, *, name, stops):
225 super().__init__() 228 super().__init__()
226 self.name = name 229 self.name = name
230 @property 233 @property
231 def url_name(self): 234 def url_name(self):
232 from urllib.request import quote 235 from urllib.request import quote
233 return 'custom?stops=' + ';'.join(stop.code for stop in self.stops) + '&name=' + quote(self.name) 236 return 'custom?stops=' + ';'.join(stop.code for stop in self.stops) + '&name=' + quote(self.name)
234 237
238 from collections import defaultdict
235 routes = {} 239 routes = {}
236 routes_per_id = {} 240 routes_per_id = {}
237 all_trips = {} 241 all_trips = {}
238 services = {} 242 services = {}
239 bus_stops = {} 243 bus_stops = {}
240 all_clusters = set() 244 all_clusters = set()
241 viimeinen_käyttöpäivä = None 245 viimeinen_käyttöpäivä = None
242 clusters_by_name = {} 246 clusters_by_name = {}
243 services_for_day = {} 247 services_for_day = {}
248 shapes = defaultdict(list)
244 249
245 def load_buses(gtfs_zip_path): 250 def load_buses(gtfs_zip_path):
246 global viimeinen_käyttöpäivä 251 global viimeinen_käyttöpäivä
247 from zipfile import ZipFile 252 from zipfile import ZipFile
248 with ZipFile(gtfs_zip_path) as gtfs_zip: 253 with ZipFile(gtfs_zip_path) as gtfs_zip:
272 print('Loading trips... ', file = stderr, end = '', flush = True) 277 print('Loading trips... ', file = stderr, end = '', flush = True)
273 shape_distances = {} 278 shape_distances = {}
274 try: 279 try:
275 with gtfs_zip.open('shapes.txt') as file: 280 with gtfs_zip.open('shapes.txt') as file:
276 for row in read_csv(map(bytes.decode, file)): 281 for row in read_csv(map(bytes.decode, file)):
282 list.append(shapes[row['shape_id']], (row['shape_pt_lat'], row['shape_pt_lon']))
277 shape_distances[row['shape_id']] = max(shape_distances.get(row['shape_id'], 0), float(row['shape_dist_traveled'])) 283 shape_distances[row['shape_id']] = max(shape_distances.get(row['shape_id'], 0), float(row['shape_dist_traveled']))
278 except KeyError: 284 except KeyError:
279 pass 285 pass
280 286
281 with gtfs_zip.open('trips.txt') as file: 287 with gtfs_zip.open('trips.txt') as file:
287 reference = row['trip_id'], 293 reference = row['trip_id'],
288 route = route, 294 route = route,
289 service = services[row['service_id']], 295 service = services[row['service_id']],
290 length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']), 296 length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']),
291 block_id = row.get('block_id') or row['service_id'], 297 block_id = row.get('block_id') or row['service_id'],
298 shape = row.get('shape_id')
292 ) 299 )
293 route.trips.add(trip) 300 route.trips.add(trip)
294 if trip.name in all_trips: 301 if trip.name in all_trips:
295 print('Trip %s already exists' % trip.name) 302 print('Trip %s already exists' % trip.name)
296 else: 303 else:

mercurial