buses.py

changeset 134
4ac0f2e2ec4e
parent 127
2bc0529d44a5
child 136
27324a81beca
equal deleted inserted replaced
133:091a48867bae 134:4ac0f2e2ec4e
3 from sys import stderr 3 from sys import stderr
4 from datetime import date, time, datetime, timedelta 4 from datetime import date, time, datetime, timedelta
5 from copy import copy 5 from copy import copy
6 from misc import * 6 from misc import *
7 from geometry import * 7 from geometry import *
8 import uuid
9 8
10 def transform_trip_reference(reference): 9 def transform_trip_reference(reference):
11 return reference 10 return reference
12 11
13 class BusTrip: 12 class BusTrip:
151 return 'ferry-terminal' 150 return 'ferry-terminal'
152 else: 151 else:
153 return 'bus-stop' 152 return 'bus-stop'
154 153
155 class BusHalt: 154 class BusHalt:
156 def __init__(self, arrival_time, departure_time, stop, trip, traveled_distance): 155 def __init__(self, arrival_time, departure_time, stop, trip, traveled_distance, visitnumber):
157 self.arrival_time, self.departure_time, self.stop, self.trip = arrival_time, departure_time, \ 156 self.arrival_time, self.departure_time, self.stop, self.trip = arrival_time, departure_time, \
158 stop, trip 157 stop, trip
159 self.traveled_distance = traveled_distance 158 self.traveled_distance = traveled_distance
160 self.uuid = uuid.uuid4() 159 self.visitnumber = visitnumber
161 @property 160 @property
162 def is_arrival(self): 161 def is_arrival(self):
163 if profile['regions']['use-regions']: 162 if profile['regions']['use-regions']:
164 if not hasattr(self, 'cachedIsArrival'): 163 if not hasattr(self, 'cachedIsArrival'):
165 if self.stop.region: 164 if self.stop.region:
183 return datetime.datetime.combine(date, datetime.time()) + self.departure_time 182 return datetime.datetime.combine(date, datetime.time()) + self.departure_time
184 def arrival_datetime(self, date): 183 def arrival_datetime(self, date):
185 import datetime 184 import datetime
186 return datetime.datetime.combine(date, datetime.time()) + self.arrival_time 185 return datetime.datetime.combine(date, datetime.time()) + self.arrival_time
187 def __repr__(self): 186 def __repr__(self):
188 return 'BusHalt(%r, %r, %r, %r)' % (self.arrival_time, self.departure_time, self.stop, self.trip) 187 return 'BusHalt(%r, %r, %r, %r, %r, %r)' % (self.arrival_time, self.departure_time, self.stop, self.trip, self.traveled_distance, self.visitnumber)
189 def sign(self, long = False): 188 def sign(self, long = False):
190 from busroute import reduce_schedule 189 from busroute import reduce_schedule
191 return reduce_schedule( 190 return reduce_schedule(
192 route = self.trip.concise_schedule(self), 191 route = self.trip.concise_schedule(self),
193 trip_length = self.trip.length - self.traveled_distance, 192 trip_length = self.trip.length - self.traveled_distance,
194 long = long, 193 long = long,
195 ) 194 )
195 @property
196 def primary_key(self):
197 arrival_time = self.trip.schedule[0].arrival_time;
198 return self.trip.block_id + '-' + str(int(arrival_time.seconds)) + '-' + str(self.visitnumber)
196 199
197 class BusStopCluster: 200 class BusStopCluster:
198 def __init__(self): 201 def __init__(self):
199 self.stops = set() 202 self.stops = set()
200 self.cached_center = None 203 self.cached_center = None
500 trip = all_trips[transform_trip_reference(row['trip_id'])] 503 trip = all_trips[transform_trip_reference(row['trip_id'])]
501 arrival_time = read_time(row['arrival_time']) 504 arrival_time = read_time(row['arrival_time'])
502 departure_time = read_time(row['departure_time']) 505 departure_time = read_time(row['departure_time'])
503 stop = bus_stops[row['stop_id']] 506 stop = bus_stops[row['stop_id']]
504 traveled_distance = float(row.get('shape_dist_traveled', 1)) * float(profile['metrics']['shape-modifier']) 507 traveled_distance = float(row.get('shape_dist_traveled', 1)) * float(profile['metrics']['shape-modifier'])
505 trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance)) 508 visitnumber = len(trip.schedule) + 1
509 trip.schedule.append(BusHalt(arrival_time, departure_time, stop, trip, traveled_distance, visitnumber))
506 stop.involved_trips.add(trip) 510 stop.involved_trips.add(trip)
507 progress += 1 511 progress += 1
508 if progress % 1000 == 0: 512 if progress % 1000 == 0:
509 print('\rLoading schedules... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr) 513 print('\rLoading schedules... %.1f%%' % (progress * 100 / row_count), end = ' ', file = stderr)
510 print('\rLoading schedules... complete', file = stderr) 514 print('\rLoading schedules... complete', file = stderr)
550 if len(trip.schedule) >= 2: 554 if len(trip.schedule) >= 2:
551 bus_speed_coefficient = 750 # metriƤ minuutissa 555 bus_speed_coefficient = 750 # metriƤ minuutissa
552 last_leg_distance = trip.schedule[-1].traveled_distance - trip.schedule[-2].traveled_distance 556 last_leg_distance = trip.schedule[-1].traveled_distance - trip.schedule[-2].traveled_distance
553 trip.schedule[-1].arrival_time = trip.schedule[-2].departure_time + timedelta(minutes = last_leg_distance / bus_speed_coefficient) 557 trip.schedule[-1].arrival_time = trip.schedule[-2].departure_time + timedelta(minutes = last_leg_distance / bus_speed_coefficient)
554 558
555 global trips_by_vehicle_info
556 trips_by_vehicle_info = {}
557 for trip in all_trips.values():
558 trips_by_vehicle_info[(trip.block_id, trip.schedule[0].arrival_time)] = trip
559 # Add services to all bus stops 559 # Add services to all bus stops
560 for route in routes.values(): 560 for route in routes.values():
561 for trip in route.trips: 561 for trip in route.trips:
562 for halt in trip.schedule: 562 for halt in trip.schedule:
563 halt.stop.services.add(route.service) 563 halt.stop.services.add(route.service)

mercurial