compute-regions.py

Tue, 25 Sep 2018 22:17:37 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 25 Sep 2018 22:17:37 +0300
changeset 125
f191a394697a
parent 124
c3b022f51704
child 126
369e242edc5d
permissions
-rwxr-xr-x

adjusted block_factor

1
22c22ff63e66 Aluemuotoja
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #!/usr/bin/env python3
2
48efa8ca14dd Suppea ajovuoroesitys
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
2 import sys, json
48efa8ca14dd Suppea ajovuoroesitys
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
3 from misc import *
7
f3791dccfd03 Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
4 from geometry import *
31
60045b362d71 - Ajovuoroa ei enää esitetä kahdessa välilehdessä vaan puukuvaimessa
Teemu Piippo <teemu@hecknology.net>
parents: 27
diff changeset
5 from zipfile import ZipFile
52
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
6 from configparser import ConfigParser
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
7 from regions import parse_regions
52
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
8
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
9 representatives = {}
cab8d38fe5c6 Kuntauudistus
Teemu Piippo <teemu@hecknology.net>
parents: 32
diff changeset
10
89
438d77bca50e map updates
Teemu Piippo <teemu@hecknology.net>
parents: 88
diff changeset
11 regions = parse_regions(sys.argv[2])
7
f3791dccfd03 Käännetty tiedostojen nimet englanniksi
Teemu Piippo <teemu@hecknology.net>
parents: 6
diff changeset
12 bus_stops = {}
125
f191a394697a adjusted block_factor
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
13 block_factor = 0.005
2
48efa8ca14dd Suppea ajovuoroesitys
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
14
124
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
15 class Blockmap:
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
16 def __init__(self, default = None):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
17 from collections import defaultdict
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
18 self.blocks = default or defaultdict(set)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
19 def __getitem__(self, blockid):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
20 return self.blocks[blockid]
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
21 def blockpoint(self, point):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
22 return int(point.x / block_factor), int(point.y / block_factor)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
23 def block_min_corner(self, blockpoint):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
24 return Location(blockpoint[0] * block_factor, blockpoint[1] * block_factor)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
25 def block_max_corner(self, blockpoint):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
26 return Location((blockpoint[0] + 1) * block_factor, (blockpoint[1] + 1) * block_factor)
2
48efa8ca14dd Suppea ajovuoroesitys
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
27
124
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
28 def blocks_in_shape(blockmap, shape):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
29 min_x = min(point.x for point in shape.points)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
30 min_y = min(point.y for point in shape.points)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
31 max_x = max(point.x for point in shape.points)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
32 max_y = max(point.y for point in shape.points)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
33 min_blockpoint = blockmap.blockpoint(Location(min_x, min_y))
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
34 max_blockpoint = blockmap.blockpoint(Location(max_x, max_y))
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
35 for x in range(min_blockpoint[0], max_blockpoint[0] + 1):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
36 for y in range(min_blockpoint[1], max_blockpoint[1] + 1):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
37 yield blockmap[x, y]
2
48efa8ca14dd Suppea ajovuoroesitys
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
38
124
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
39 def main():
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
40 with ZipFile(sys.argv[1]) as archive:
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
41 with archive.open('stops.txt') as file:
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
42 for row in read_csv(map(bytes.decode, file)):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
43 location = Location(float(row['stop_lat']), float(row['stop_lon']))
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
44 reference = row['stop_id']
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
45 bus_stops[reference] = location
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
46 region_shapes = list()
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
47 districts = dict()
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
48 bus_stop_regions = dict()
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
49 blocks_per_region = dict()
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
50 blockmap = Blockmap()
88
3b86597c5a88 major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
51 for region in regions.values():
124
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
52 blocks_per_region[region['name']] = list(blocks_in_shape(blockmap, region['shape']))
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
53 for block in blocks_per_region[region['name']]:
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
54 set.add(block, region['name'])
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
55 for stop_id, stop_position in bus_stops.items():
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
56 for region_name in blockmap[blockmap.blockpoint(stop_position)]:
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
57 region = regions[region_name]
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
58 if region['shape'].contains_point(stop_position):
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
59 bus_stop_regions[stop_id] = region['name']
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
60 break
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
61 else:
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
62 bus_stop_regions[stop_id] = None
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
63 covered = sum(1 if value else 0 for value in bus_stop_regions.values())
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
64 total = len(bus_stops)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
65 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
66 json.dump(bus_stop_regions, sys.stdout, indent = 2)
2
48efa8ca14dd Suppea ajovuoroesitys
Teemu Piippo <teemu@hecknology.net>
parents: 1
diff changeset
67
124
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
68 if __name__ == '__main__':
c3b022f51704 optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents: 89
diff changeset
69 main()

mercurial