commented and simplified

Tue, 25 Sep 2018 22:22:10 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 25 Sep 2018 22:22:10 +0300
changeset 126
369e242edc5d
parent 125
f191a394697a
child 127
2bc0529d44a5
child 128
7a55abeab5fd

commented and simplified

compute-regions.py file | annotate | diff | comparison | revisions
--- 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]

mercurial