|         | 
     1 #!/usr/bin/env python3 | 
|         | 
     2 import sqlalchemy | 
|         | 
     3 import sqlalchemy.orm | 
|         | 
     4 from regions import parse_regions | 
|         | 
     5 from datamodel import * | 
|         | 
     6 engine = sqlalchemy.create_engine('sqlite:///gtfs.db') | 
|         | 
     7 GtfsBase.metadata.create_all(engine) | 
|         | 
     8 session = sqlalchemy.orm.sessionmaker(bind = engine)() | 
|         | 
     9 regions = parse_regions('föli.osm') | 
|         | 
    10  | 
|         | 
    11 def filter_itinerary(raw_itinerary): | 
|         | 
    12 	encountered = set() | 
|         | 
    13 	for region in raw_itinerary: | 
|         | 
    14 		if region and region not in encountered: | 
|         | 
    15 			yield region | 
|         | 
    16 			encountered.add(region) | 
|         | 
    17  | 
|         | 
    18 def get_stoptimes(session): | 
|         | 
    19 	yield from session \ | 
|         | 
    20 		.query(GtfsStopTime) \ | 
|         | 
    21 		.filter(GtfsStopTime.destination == None) \ | 
|         | 
    22 		.limit(1000) | 
|         | 
    23  | 
|         | 
    24 def length_left(stoptime): | 
|         | 
    25 	return stoptime.trip.shape.length - stoptime.shape_distance_traveled | 
|         | 
    26  | 
|         | 
    27 amount = session.query(GtfsStopTime).filter(GtfsStopTime.destination == None).count() | 
|         | 
    28 k = 0 | 
|         | 
    29 got_stoptimes = amount != 0 | 
|         | 
    30  | 
|         | 
    31 while got_stoptimes: | 
|         | 
    32 	print('%.2f%%' % (k * 100 / amount)) | 
|         | 
    33 	got_stoptimes = False | 
|         | 
    34 	for stoptime in get_stoptimes(session): | 
|         | 
    35 		got_stoptimes = True | 
|         | 
    36 		k += 1 | 
|         | 
    37 		itinerary = list(filter_itinerary( | 
|         | 
    38 			entry.stop.stop_region | 
|         | 
    39 			for entry in session.query(GtfsStopTime) | 
|         | 
    40 				.filter(GtfsStopTime.trip_id == stoptime.trip_id) | 
|         | 
    41 				.filter(GtfsStopTime.stop_sequence > stoptime.stop_sequence) | 
|         | 
    42 		)) | 
|         | 
    43 		from busroute import destinations_list | 
|         | 
    44 		dests = destinations_list( | 
|         | 
    45 			itinerary = itinerary, | 
|         | 
    46 			trip_length = float(length_left(stoptime)), | 
|         | 
    47 			regions = regions, | 
|         | 
    48 		) | 
|         | 
    49 		stoptime.destination = '-'.join(dests) | 
|         | 
    50 	session.commit() |