--- a/compute-regions.py Tue Sep 25 22:17:37 2018 +0300 +++ b/compute-regions.py Tue Sep 25 22:22:10 2018 +0300 @@ -13,19 +13,19 @@ block_factor = 0.005 class Blockmap: + ''' Models a map of blocks. Maps each location to a square area. ''' def __init__(self, default = None): from collections import defaultdict self.blocks = default or defaultdict(set) def __getitem__(self, blockid): + ''' Returns a block for block coordinates. The block is a set that can contain anything. ''' return self.blocks[blockid] def blockpoint(self, point): + ''' Returns a block point for a location. The block point is a coordinate in the blockmap. ''' return int(point.x / block_factor), int(point.y / block_factor) - def block_min_corner(self, blockpoint): - return Location(blockpoint[0] * block_factor, blockpoint[1] * block_factor) - def block_max_corner(self, blockpoint): - return Location((blockpoint[0] + 1) * block_factor, (blockpoint[1] + 1) * block_factor) def blocks_in_shape(blockmap, shape): + ''' Finds all blocks inside the bounding box of a shape. ''' min_x = min(point.x for point in shape.points) min_y = min(point.y for point in shape.points) max_x = max(point.x for point in shape.points) @@ -46,12 +46,12 @@ region_shapes = list() districts = dict() bus_stop_regions = dict() - blocks_per_region = dict() blockmap = Blockmap() + # Find all regions for every block. for region in regions.values(): - blocks_per_region[region['name']] = list(blocks_in_shape(blockmap, region['shape'])) - for block in blocks_per_region[region['name']]: + for block in blocks_in_shape(blockmap, region['shape']): set.add(block, region['name']) + # Find the region every node is in for stop_id, stop_position in bus_stops.items(): for region_name in blockmap[blockmap.blockpoint(stop_position)]: region = regions[region_name]