alueet.py

changeset 7
f3791dccfd03
parent 6
88cfb916c852
child 8
d12e8a560faa
equal deleted inserted replaced
6:88cfb916c852 7:f3791dccfd03
1 #!/usr/bin/env python3
2 import sys, json
3 from misc import *
4 from geometria import *
5
6 with open('alueet.gmp') as file:
7 data = file.read().split('@')
8
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):
16 sijainti = Sijainti(float(rivi['stop_lat']), float(rivi['stop_lon']))
17 tunniste = rivi['stop_id']
18 pysäkit[tunniste] = sijainti
19
20 district_shapes = []
21 for polygon in data[1].splitlines():
22 polygon = polygon.split('^')
23 coordinates = []
24 for point in polygon[3].split('~'):
25 x, y = point.split(',')
26 coordinates.append(Sijainti(float(x), float(y)))
27 district_shapes.append(Monikulmio(*coordinates))
28
29 districts = {}
30 bus_stop_districts = {}
31
32 for name, stop_id in representatives.items():
33 if stop_id is None:
34 continue
35 if stop_id not in pysäkit:
36 print('Alueen %r edustaja %r ei löydy aikatauluista' % (name, stop_id), file = sys.stderr)
37 else:
38 for district_shape in district_shapes:
39 if district_shape.sisältää_pisteen(pysäkit[stop_id]):
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:
46 print('cannot find a shape for %r' % name, file = sys.stderr)
47
48 for stop_id, stop_position in pysäkit.items():
49 for district, shape in districts.items():
50 if shape.sisältää_pisteen(stop_position):
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())
57 total = len(pysäkit)
58 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
59 json.dump(bus_stop_districts, sys.stdout, indent = 2)

mercurial