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