Tue, 25 Sep 2018 22:22:10 +0300
commented and simplified
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import sys, json |
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 | 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 | 8 | |
9 | representatives = {} | |
10 | ||
89 | 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 | 13 | block_factor = 0.005 |
2 | 14 | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
15 | class Blockmap: |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
16 | ''' Models a map of blocks. Maps each location to a square area. ''' |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
17 | def __init__(self, default = None): |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
18 | from collections import defaultdict |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
19 | self.blocks = default or defaultdict(set) |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
20 | def __getitem__(self, blockid): |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
21 | ''' Returns a block for block coordinates. The block is a set that can contain anything. ''' |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
22 | return self.blocks[blockid] |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
23 | def blockpoint(self, point): |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
24 | ''' Returns a block point for a location. The block point is a coordinate in the blockmap. ''' |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
25 | return int(point.x / block_factor), int(point.y / block_factor) |
2 | 26 | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
27 | def blocks_in_shape(blockmap, shape): |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
28 | ''' Finds all blocks inside the bounding box of a shape. ''' |
124
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 | 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 | blockmap = Blockmap() |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
50 | # Find all regions for every block. |
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(): |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
52 | for block in blocks_in_shape(blockmap, region['shape']): |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
53 | set.add(block, region['name']) |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
54 | # Find the region every node is in |
124
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 | 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() |