buses.py

changeset 114
b736478416d4
parent 109
88a5110b66ba
child 127
2bc0529d44a5
--- a/buses.py	Wed Sep 12 08:27:27 2018 +0300
+++ b/buses.py	Thu Sep 13 21:32:30 2018 +0300
@@ -10,11 +10,12 @@
 	return reference
 
 class BusTrip:
-	def __init__(self, reference, route, service, length, block_id):
+	def __init__(self, reference, route, service, length, block_id, shape):
 		self.reference, self.route, self.service, self.block_id = reference, route, service, block_id
 		self.length = length
 		self.schedule = []
 		self.name = transform_trip_reference(reference)
+		self.shape = str(shape)
 	def __repr__(self):
 		return 'all_trips[%r]' % self.name
 	def contains_stop(self, stop):
@@ -219,6 +220,8 @@
 			result += stop.schedule(max_amount = max_amount)
 		result.sort(key = lambda schedule_entry: schedule_entry['time'])
 		return result[:max_amount]
+	def __lt__(self, other):
+		return (self.name and other.name) and (self.name < other.name) or (id(self) < id(other))
 
 class CustomBusStopCluster(BusStopCluster):
 	def __init__(self, *, name, stops):
@@ -232,6 +235,7 @@
 		from urllib.request import quote
 		return 'custom?stops=' + ';'.join(stop.code for stop in self.stops) + '&name=' + quote(self.name)
 
+from collections import defaultdict
 routes = {}
 routes_per_id = {}
 all_trips = {}
@@ -241,6 +245,7 @@
 viimeinen_käyttöpäivä = None
 clusters_by_name = {}
 services_for_day = {}
+shapes = defaultdict(list)
 
 def load_buses(gtfs_zip_path):
 	global viimeinen_käyttöpäivä
@@ -274,6 +279,7 @@
 		try:
 			with gtfs_zip.open('shapes.txt') as file:
 				for row in read_csv(map(bytes.decode, file)):
+					list.append(shapes[row['shape_id']], (row['shape_pt_lat'], row['shape_pt_lon']))
 					shape_distances[row['shape_id']] = max(shape_distances.get(row['shape_id'], 0), float(row['shape_dist_traveled']))
 		except KeyError:
 			pass
@@ -289,6 +295,7 @@
 					service = services[row['service_id']],
 					length = shape_distances.get(row.get('shape_id'), 1) * float(profile['metrics']['shape-modifier']),
 					block_id = row.get('block_id') or row['service_id'],
+					shape = row.get('shape_id')
 				)
 				route.trips.add(trip)
 				if trip.name in all_trips:

mercurial