compute-regions.py

changeset 124
c3b022f51704
parent 89
438d77bca50e
child 125
f191a394697a
equal deleted inserted replaced
123:759b0302ee7a 124:c3b022f51704
8 8
9 representatives = {} 9 representatives = {}
10 10
11 regions = parse_regions(sys.argv[2]) 11 regions = parse_regions(sys.argv[2])
12 bus_stops = {} 12 bus_stops = {}
13 block_factor = 0.01
13 14
14 with ZipFile(sys.argv[1]) as archive: 15 class Blockmap:
15 with archive.open('stops.txt') as file: 16 def __init__(self, default = None):
16 for row in read_csv(map(bytes.decode, file)): 17 from collections import defaultdict
17 location = Location(float(row['stop_lat']), float(row['stop_lon'])) 18 self.blocks = default or defaultdict(set)
18 reference = row['stop_id'] 19 def __getitem__(self, blockid):
19 bus_stops[reference] = location 20 return self.blocks[blockid]
21 def blockpoint(self, point):
22 return int(point.x / block_factor), int(point.y / block_factor)
23 def block_min_corner(self, blockpoint):
24 return Location(blockpoint[0] * block_factor, blockpoint[1] * block_factor)
25 def block_max_corner(self, blockpoint):
26 return Location((blockpoint[0] + 1) * block_factor, (blockpoint[1] + 1) * block_factor)
20 27
21 region_shapes = [] 28 def blocks_in_shape(blockmap, shape):
22 districts = {} 29 min_x = min(point.x for point in shape.points)
23 bus_stop_regions = {} 30 min_y = min(point.y for point in shape.points)
31 max_x = max(point.x for point in shape.points)
32 max_y = max(point.y for point in shape.points)
33 min_blockpoint = blockmap.blockpoint(Location(min_x, min_y))
34 max_blockpoint = blockmap.blockpoint(Location(max_x, max_y))
35 for x in range(min_blockpoint[0], max_blockpoint[0] + 1):
36 for y in range(min_blockpoint[1], max_blockpoint[1] + 1):
37 yield blockmap[x, y]
24 38
25 for stop_id, stop_position in bus_stops.items(): 39 def main():
40 with ZipFile(sys.argv[1]) as archive:
41 with archive.open('stops.txt') as file:
42 for row in read_csv(map(bytes.decode, file)):
43 location = Location(float(row['stop_lat']), float(row['stop_lon']))
44 reference = row['stop_id']
45 bus_stops[reference] = location
46 region_shapes = list()
47 districts = dict()
48 bus_stop_regions = dict()
49 blocks_per_region = dict()
50 blockmap = Blockmap()
26 for region in regions.values(): 51 for region in regions.values():
27 if region['shape'].contains_point(stop_position): 52 blocks_per_region[region['name']] = list(blocks_in_shape(blockmap, region['shape']))
28 bus_stop_regions[stop_id] = region['name'] 53 for block in blocks_per_region[region['name']]:
29 break 54 set.add(block, region['name'])
30 else: 55 for stop_id, stop_position in bus_stops.items():
31 bus_stop_regions[stop_id] = None 56 for region_name in blockmap[blockmap.blockpoint(stop_position)]:
57 region = regions[region_name]
58 if region['shape'].contains_point(stop_position):
59 bus_stop_regions[stop_id] = region['name']
60 break
61 else:
62 bus_stop_regions[stop_id] = None
63 covered = sum(1 if value else 0 for value in bus_stop_regions.values())
64 total = len(bus_stops)
65 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
66 json.dump(bus_stop_regions, sys.stdout, indent = 2)
32 67
33 covered = sum(1 if value else 0 for value in bus_stop_regions.values()) 68 if __name__ == '__main__':
34 total = len(bus_stops) 69 main()
35 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
36 json.dump(bus_stop_regions, sys.stdout, indent = 2)

mercurial