alueet.py

Sun, 23 Apr 2017 11:01:51 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 23 Apr 2017 11:01:51 +0300
changeset 3
ca2df60215ff
parent 2
48efa8ca14dd
child 5
a65b680f1774
permissions
-rwxr-xr-x

Lisätty alueita

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

mercurial