Sat, 10 Jun 2017 20:56:38 +0300
Lisätty bussipysäkkien ryhmittely
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import sys, json |
3 | from misc import * | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
4 | from geometry import * |
2 | 5 | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
6 | with open('regions.gmp') as file: |
2 | 7 | data = file.read().split('@') |
1 | 8 | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
9 | with open('region-representatives.json') as file: |
2 | 10 | representatives = json.load(file) |
11 | ||
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
12 | bus_stops = {} |
2 | 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'] |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
18 | bus_stops[tunniste] = sijainti |
2 | 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 | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
35 | if stop_id not in bus_stops: |
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
36 | print('Representative %r for region %r not found in schedule' % (stop_id, name), file = sys.stderr) |
2 | 37 | else: |
6 | 38 | for district_shape in district_shapes: |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
39 | if district_shape.sisältää_pisteen(bus_stops[stop_id]): |
6 | 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: | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
46 | print('Cannot find a shape for %r' % name, file = sys.stderr) |
2 | 47 | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
48 | for stop_id, stop_position in bus_stops.items(): |
2 | 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()) | |
7
f3791dccfd03
Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
57 | total = len(bus_stops) |
2 | 58 | print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) |
59 | json.dump(bus_stop_districts, sys.stdout, indent = 2) |