16 |
11 |
17 pysäkit = {} |
12 pysäkit = {} |
18 |
13 |
19 with open('gtfs/stops.txt') as file: |
14 with open('gtfs/stops.txt') as file: |
20 for rivi in lue_csv(file): |
15 for rivi in lue_csv(file): |
21 sijainti = QPointF(float(rivi['stop_lat']), float(rivi['stop_lon'])) |
16 sijainti = Sijainti(float(rivi['stop_lat']), float(rivi['stop_lon'])) |
22 tunniste = rivi['stop_id'] |
17 tunniste = rivi['stop_id'] |
23 pysäkit[tunniste] = sijainti |
18 pysäkit[tunniste] = sijainti |
24 |
19 |
25 district_shapes = [] |
20 district_shapes = [] |
26 for polygon in data[1].splitlines(): |
21 for polygon in data[1].splitlines(): |
27 polygon = polygon.split('^') |
22 polygon = polygon.split('^') |
28 coordinates = [] |
23 coordinates = [] |
29 for point in polygon[3].split('~'): |
24 for point in polygon[3].split('~'): |
30 x, y = point.split(',') |
25 x, y = point.split(',') |
31 coordinates.append(QPointF(float(x), float(y))) |
26 coordinates.append(Sijainti(float(x), float(y))) |
32 district_shapes.append(QPolygonF(coordinates)) |
27 district_shapes.append(Monikulmio(*coordinates)) |
33 |
28 |
34 districts = {} |
29 districts = {} |
35 bus_stop_districts = {} |
30 bus_stop_districts = {} |
36 |
31 |
37 for name, stop_id in representatives.items(): |
32 for name, stop_id in representatives.items(): |
38 if stop_id is None: |
33 if stop_id is None: |
39 continue |
34 continue |
40 for district_shape in district_shapes: |
35 for district_shape in district_shapes: |
41 if district_shape.containsPoint(pysäkit[stop_id], Qt.OddEvenFill): |
36 if district_shape.sisältää_pisteen(pysäkit[stop_id]): |
42 assert name not in districts |
37 assert name not in districts |
43 districts[name] = district_shape |
38 districts[name] = district_shape |
44 district_shapes.remove(district_shape) |
39 district_shapes.remove(district_shape) |
45 bus_stop_districts[stop_id] = name |
40 bus_stop_districts[stop_id] = name |
46 break |
41 break |
47 else: |
42 else: |
48 print('cannot find a shape for %r' % name, file = sys.stderr) |
43 print('cannot find a shape for %r' % name, file = sys.stderr) |
49 |
44 |
50 for stop_id, stop_position in pysäkit.items(): |
45 for stop_id, stop_position in pysäkit.items(): |
51 for district, shape in districts.items(): |
46 for district, shape in districts.items(): |
52 if shape.containsPoint(stop_position, Qt.OddEvenFill): |
47 if shape.sisältää_pisteen(stop_position): |
53 bus_stop_districts[stop_id] = district |
48 bus_stop_districts[stop_id] = district |
54 break |
49 break |
55 else: |
50 else: |
56 bus_stop_districts[stop_id] = None |
51 bus_stop_districts[stop_id] = None |
57 |
52 |
58 covered = sum(int(bool(k)) for k in bus_stop_districts.values()) |
53 covered = sum(int(bool(k)) for k in bus_stop_districts.values()) |
59 total = len(pysäkit) |
54 total = len(pysäkit) |
60 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) |
55 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) |
61 |
|
62 json.dump(bus_stop_districts, sys.stdout, indent = 2) |
56 json.dump(bus_stop_districts, sys.stdout, indent = 2) |