Thu, 25 May 2017 16:41:21 +0300
Paljon asioita
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import sys, json |
3 | from misc import * | |
5 | 4 | from geometria import * |
2 | 5 | |
1 | 6 | with open('alueet.gmp') as file: |
2 | 7 | data = file.read().split('@') |
1 | 8 | |
2 | 9 | with open('alue-edustajat.json') as file: |
10 | representatives = json.load(file) | |
11 | ||
12 | pysäkit = {} | |
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'] |
18 | pysäkit[tunniste] = sijainti | |
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 | |
35 | for district_shape in district_shapes: | |
5 | 36 | if district_shape.sisältää_pisteen(pysäkit[stop_id]): |
2 | 37 | assert name not in districts |
38 | districts[name] = district_shape | |
39 | district_shapes.remove(district_shape) | |
40 | bus_stop_districts[stop_id] = name | |
41 | break | |
42 | else: | |
43 | print('cannot find a shape for %r' % name, file = sys.stderr) | |
44 | ||
45 | for stop_id, stop_position in pysäkit.items(): | |
46 | for district, shape in districts.items(): | |
5 | 47 | if shape.sisältää_pisteen(stop_position): |
2 | 48 | bus_stop_districts[stop_id] = district |
49 | break | |
50 | else: | |
51 | bus_stop_districts[stop_id] = None | |
52 | ||
53 | covered = sum(int(bool(k)) for k in bus_stop_districts.values()) | |
54 | total = len(pysäkit) | |
55 | print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) | |
56 | json.dump(bus_stop_districts, sys.stdout, indent = 2) |