compute-regions.py

changeset 32
ca1a0ea81cf6
parent 31
60045b362d71
child 52
cab8d38fe5c6
equal deleted inserted replaced
31:60045b362d71 32:ca1a0ea81cf6
30 30
31 districts = {} 31 districts = {}
32 bus_stop_districts = {} 32 bus_stop_districts = {}
33 33
34 for name, stop_id in representatives.items(): 34 for name, stop_id in representatives.items():
35 if stop_id is None: 35 if stop_id:
36 continue 36 if stop_id not in bus_stops:
37 if stop_id not in bus_stops: 37 print('Representative %r for region %r not found in schedule' % (stop_id, name), file = sys.stderr)
38 print('Representative %r for region %r not found in schedule' % (stop_id, name), file = sys.stderr)
39 else:
40 for district_shape in district_shapes:
41 if district_shape.sisältää_pisteen(bus_stops[stop_id]):
42 assert name not in districts
43 districts[name] = district_shape
44 district_shapes.remove(district_shape)
45 bus_stop_districts[stop_id] = name
46 break
47 else: 38 else:
48 print('Cannot find a shape for %r' % name, file = sys.stderr) 39 for district_shape in district_shapes:
40 if district_shape.sisältää_pisteen(bus_stops[stop_id]):
41 assert name not in districts
42 districts[name] = district_shape
43 district_shapes.remove(district_shape)
44 bus_stop_districts[stop_id] = name
45 break
46 else:
47 print('Cannot find a shape for %r' % name, file = sys.stderr)
49 48
50 for stop_id, stop_position in bus_stops.items(): 49 for stop_id, stop_position in bus_stops.items():
51 for district, shape in districts.items(): 50 for district, shape in districts.items():
52 if shape.sisältää_pisteen(stop_position): 51 if shape.sisältää_pisteen(stop_position):
53 bus_stop_districts[stop_id] = district 52 bus_stop_districts[stop_id] = district
54 break 53 break
55 else: 54 else:
56 bus_stop_districts[stop_id] = None 55 bus_stop_districts[stop_id] = None
57 56
58 covered = sum(int(bool(k)) for k in bus_stop_districts.values()) 57 covered = sum(1 if value else 0 for value in bus_stop_districts.values())
59 total = len(bus_stops) 58 total = len(bus_stops)
60 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) 59 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
61 json.dump(bus_stop_districts, sys.stdout, indent = 2) 60 json.dump(bus_stop_districts, sys.stdout, indent = 2)

mercurial