diff -r 22c22ff63e66 -r 48efa8ca14dd alueet.py --- a/alueet.py Mon Apr 17 22:56:39 2017 +0300 +++ b/alueet.py Tue Apr 18 14:59:18 2017 +0300 @@ -1,17 +1,62 @@ #!/usr/bin/env python3 from pprint import pprint +from PyQt5.QtCore import QPointF, Qt +from PyQt5.QtGui import QPolygonF +from PyQt5.QtWidgets import QApplication +import sys, json +from misc import * + +app = QApplication(sys.argv) + with open('alueet.gmp') as file: - data = file.read() + data = file.read().split('@') -shapes = {} -polygons = data.split('@')[1] -for polygon in polygons.splitlines(): +with open('alue-edustajat.json') as file: + representatives = json.load(file) + +pysäkit = {} + +with open('gtfs/stops.txt') as file: + for rivi in lue_csv(file): + sijainti = QPointF(float(rivi['stop_lat']), float(rivi['stop_lon'])) + tunniste = rivi['stop_id'] + pysäkit[tunniste] = sijainti + +district_shapes = [] +for polygon in data[1].splitlines(): polygon = polygon.split('^') - nimi = polygon[2].rsplit('>', 1)[1] coordinates = [] for point in polygon[3].split('~'): x, y = point.split(',') - coordinates.append((float(x), float(y))) - shapes[nimi.strip().replace('\u200b', '')] = coordinates + coordinates.append(QPointF(float(x), float(y))) + district_shapes.append(QPolygonF(coordinates)) + +districts = {} +bus_stop_districts = {} -pprint(shapes) +for name, stop_id in representatives.items(): + if stop_id is None: + continue + for district_shape in district_shapes: + if district_shape.containsPoint(pysäkit[stop_id], Qt.OddEvenFill): + assert name not in districts + districts[name] = district_shape + district_shapes.remove(district_shape) + bus_stop_districts[stop_id] = name + break + else: + print('cannot find a shape for %r' % name, file = sys.stderr) + +for stop_id, stop_position in pysäkit.items(): + for district, shape in districts.items(): + if shape.containsPoint(stop_position, Qt.OddEvenFill): + bus_stop_districts[stop_id] = district + break + else: + bus_stop_districts[stop_id] = None + +covered = sum(int(bool(k)) for k in bus_stop_districts.values()) +total = len(pysäkit) +print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) + +json.dump(bus_stop_districts, sys.stdout, indent = 2)