1 #!/usr/bin/env python3 |
1 #!/usr/bin/env python3 |
2 from pprint import pprint |
2 from pprint import pprint |
|
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 |
3 with open('alueet.gmp') as file: |
11 with open('alueet.gmp') as file: |
4 data = file.read() |
12 data = file.read().split('@') |
5 |
13 |
6 shapes = {} |
14 with open('alue-edustajat.json') as file: |
7 polygons = data.split('@')[1] |
15 representatives = json.load(file) |
8 for polygon in polygons.splitlines(): |
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(): |
9 polygon = polygon.split('^') |
27 polygon = polygon.split('^') |
10 nimi = polygon[2].rsplit('>', 1)[1] |
|
11 coordinates = [] |
28 coordinates = [] |
12 for point in polygon[3].split('~'): |
29 for point in polygon[3].split('~'): |
13 x, y = point.split(',') |
30 x, y = point.split(',') |
14 coordinates.append((float(x), float(y))) |
31 coordinates.append(QPointF(float(x), float(y))) |
15 shapes[nimi.strip().replace('\u200b', '')] = coordinates |
32 district_shapes.append(QPolygonF(coordinates)) |
16 |
33 |
17 pprint(shapes) |
34 districts = {} |
|
35 bus_stop_districts = {} |
|
36 |
|
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) |