buses.py

changeset 21
6a0394d5a159
parent 20
3199e289ae62
child 22
3d094a804af8
equal deleted inserted replaced
20:3199e289ae62 21:6a0394d5a159
197 with open('regions-per-stop.json') as file: 197 with open('regions-per-stop.json') as file:
198 for pysäkkitunniste, alue in json.load(file).items(): 198 for pysäkkitunniste, alue in json.load(file).items():
199 pysäkit[pysäkkitunniste].alue = alue 199 pysäkit[pysäkkitunniste].alue = alue
200 print('%d pysäkkiä' % len(pysäkit), file = stderr) 200 print('%d pysäkkiä' % len(pysäkit), file = stderr)
201 201
202
202 class BusStopCluster: 203 class BusStopCluster:
203 def __init__(self): 204 def __init__(self):
204 self.stops = set() 205 self.stops = set()
205 self._center = None 206 self._center = None
206 self.name = None 207 self.name = None
208 @property
209 def url_name(self):
210 return self.name.lower().replace('(', '').replace(')', '').replace(' ', '-')
207 def add_stop(self, stop): 211 def add_stop(self, stop):
208 assert not stop.cluster 212 assert not stop.cluster
209 stop.cluster = self 213 stop.cluster = self
210 self.stops.add(stop) 214 self.stops.add(stop)
211 self._center = None 215 self._center = None
292 from collections import defaultdict 296 from collections import defaultdict
293 from pprint import pprint 297 from pprint import pprint
294 clusters_per_name = defaultdict(set) 298 clusters_per_name = defaultdict(set)
295 for cluster in all_clusters: 299 for cluster in all_clusters:
296 name_representing_stop = min((len(pysäkki.tunniste), pysäkki.tunniste, pysäkki) for pysäkki in cluster.stops)[2] 300 name_representing_stop = min((len(pysäkki.tunniste), pysäkki.tunniste, pysäkki) for pysäkki in cluster.stops)[2]
297 name = name_representing_stop.nimi.lower().replace(' ', '-') 301 clusters_per_name[name_representing_stop.nimi].add(cluster)
298 clusters_per_name[name].add(cluster)
299 for name, clusters in clusters_per_name.items(): 302 for name, clusters in clusters_per_name.items():
300 if len(clusters) == 1: 303 if len(clusters) == 1:
301 # Ryhmä on ainoa jolla on varaus tälle nimelle. Sen kuin vaan. 304 # Ryhmä on ainoa jolla on varaus tälle nimelle. Sen kuin vaan.
302 next(iter(clusters)).name = name 305 next(iter(clusters)).name = name
303 else: 306 else:
313 # Se klusteri jolla ei ole omaa aluetta (jos on) jätetään ilman aluepäätettä. 316 # Se klusteri jolla ei ole omaa aluetta (jos on) jätetään ilman aluepäätettä.
314 if sum([1 for unique_areas in proposal.values() if not unique_areas]) <= 1: 317 if sum([1 for unique_areas in proposal.values() if not unique_areas]) <= 1:
315 for cluster, unique_areas in proposal.items(): 318 for cluster, unique_areas in proposal.items():
316 individual_cluster_name = name 319 individual_cluster_name = name
317 if unique_areas: 320 if unique_areas:
318 individual_cluster_name += '-' + min(unique_areas).lower().replace(' ', '-') 321 individual_cluster_name += ' (' + min(unique_areas) + ')'
319 cluster.name = individual_cluster_name 322 cluster.name = individual_cluster_name
320 else: 323 else:
321 # Typerä reunatapaus. Indeksoidaan numeroin... 324 # Typerä reunatapaus. Indeksoidaan numeroin...
322 for n, (_, cluster) in enumerate(sorted( 325 for n, (_, cluster) in enumerate(sorted(
323 min((stop.tunniste.lower(), cluster) for stop in cluster.stops) 326 min((stop.tunniste.lower(), cluster) for stop in cluster.stops)
330 cluster_bus_stops() 333 cluster_bus_stops()
331 name_clusters() 334 name_clusters()
332 335
333 clusters_by_name = {} 336 clusters_by_name = {}
334 for cluster in all_clusters: 337 for cluster in all_clusters:
335 assert cluster.name not in clusters_by_name 338 assert cluster.url_name not in clusters_by_name
336 clusters_by_name[cluster.name] = cluster 339 clusters_by_name[cluster.url_name] = cluster
337 340
338 print('Ladataan aikataulut... ', end = '', flush = True, file = stderr) 341 print('Ladataan aikataulut... ', end = '', flush = True, file = stderr)
339 with open('gtfs/stop_times.txt') as file: 342 with open('gtfs/stop_times.txt') as file:
340 rivimäärä = sum(line.count('\n') for line in file) 343 rivimäärä = sum(line.count('\n') for line in file)
341 laskettu = 0 344 laskettu = 0

mercurial