itienary_processing.py

changeset 2
7378b802ddf8
child 3
10ce28475e9c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/itienary_processing.py	Thu Jul 30 21:52:31 2020 +0300
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+import sqlalchemy
+import sqlalchemy.orm
+from regions import parse_regions
+from datamodel import *
+engine = sqlalchemy.create_engine('sqlite:///gtfs.db')
+GtfsBase.metadata.create_all(engine)
+session = sqlalchemy.orm.sessionmaker(bind = engine)()
+regions = parse_regions('föli.osm')
+
+def filter_itinerary(raw_itinerary):
+	encountered = set()
+	for region in raw_itinerary:
+		if region and region not in encountered:
+			yield region
+			encountered.add(region)
+
+def get_stoptimes(session):
+	yield from session \
+		.query(GtfsStopTime) \
+		.filter(GtfsStopTime.destination == None) \
+		.limit(1000)
+
+def length_left(stoptime):
+	return stoptime.trip.shape.length - stoptime.shape_distance_traveled
+
+amount = session.query(GtfsStopTime).filter(GtfsStopTime.destination == None).count()
+k = 0
+got_stoptimes = amount != 0
+
+while got_stoptimes:
+	print('%.2f%%' % (k * 100 / amount))
+	got_stoptimes = False
+	for stoptime in get_stoptimes(session):
+		got_stoptimes = True
+		k += 1
+		itinerary = list(filter_itinerary(
+			entry.stop.stop_region
+			for entry in session.query(GtfsStopTime)
+				.filter(GtfsStopTime.trip_id == stoptime.trip_id)
+				.filter(GtfsStopTime.stop_sequence > stoptime.stop_sequence)
+		))
+		from busroute import destinations_list
+		dests = destinations_list(
+			itinerary = itinerary,
+			trip_length = float(length_left(stoptime)),
+			regions = regions,
+		)
+		stoptime.destination = '-'.join(dests)
+	session.commit()

mercurial