Sat, 10 Jun 2017 16:36:36 +0300
Plöö
| 1 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | import sys, json |
| 3 | from misc import * | |
| 5 | 4 | from geometria import * |
| 2 | 5 | |
| 1 | 6 | with open('alueet.gmp') as file: |
| 2 | 7 | data = file.read().split('@') |
| 1 | 8 | |
| 2 | 9 | with open('alue-edustajat.json') as file: |
| 10 | representatives = json.load(file) | |
| 11 | ||
| 12 | pysäkit = {} | |
| 13 | ||
| 14 | with open('gtfs/stops.txt') as file: | |
| 15 | for rivi in lue_csv(file): | |
| 5 | 16 | sijainti = Sijainti(float(rivi['stop_lat']), float(rivi['stop_lon'])) |
| 2 | 17 | tunniste = rivi['stop_id'] |
| 18 | pysäkit[tunniste] = sijainti | |
| 19 | ||
| 20 | district_shapes = [] | |
| 21 | for polygon in data[1].splitlines(): | |
| 1 | 22 | polygon = polygon.split('^') |
| 23 | coordinates = [] | |
| 24 | for point in polygon[3].split('~'): | |
| 25 | x, y = point.split(',') | |
| 5 | 26 | coordinates.append(Sijainti(float(x), float(y))) |
| 27 | district_shapes.append(Monikulmio(*coordinates)) | |
| 2 | 28 | |
| 29 | districts = {} | |
| 30 | bus_stop_districts = {} | |
| 1 | 31 | |
| 2 | 32 | for name, stop_id in representatives.items(): |
| 33 | if stop_id is None: | |
| 34 | continue | |
| 6 | 35 | if stop_id not in pysäkit: |
| 36 | print('Alueen %r edustaja %r ei löydy aikatauluista' % (name, stop_id), file = sys.stderr) | |
| 2 | 37 | else: |
| 6 | 38 | for district_shape in district_shapes: |
| 39 | if district_shape.sisältää_pisteen(pysäkit[stop_id]): | |
| 40 | assert name not in districts | |
| 41 | districts[name] = district_shape | |
| 42 | district_shapes.remove(district_shape) | |
| 43 | bus_stop_districts[stop_id] = name | |
| 44 | break | |
| 45 | else: | |
| 46 | print('cannot find a shape for %r' % name, file = sys.stderr) | |
| 2 | 47 | |
| 48 | for stop_id, stop_position in pysäkit.items(): | |
| 49 | for district, shape in districts.items(): | |
| 5 | 50 | if shape.sisältää_pisteen(stop_position): |
| 2 | 51 | bus_stop_districts[stop_id] = district |
| 52 | break | |
| 53 | else: | |
| 54 | bus_stop_districts[stop_id] = None | |
| 55 | ||
| 56 | covered = sum(int(bool(k)) for k in bus_stop_districts.values()) | |
| 57 | total = len(pysäkit) | |
| 58 | print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) | |
| 59 | json.dump(bus_stop_districts, sys.stdout, indent = 2) |