Wed, 26 Sep 2018 13:12:11 +0300
refactor
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 |
2 | 7 | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
8 | class Blockmap: |
128 | 9 | ''' |
10 | Models a map of blocks. Maps each location to a square area. | |
11 | ''' | |
12 | block_factor = 0.005 | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
13 | def __init__(self, default = None): |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
14 | from collections import defaultdict |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
15 | self.blocks = default or defaultdict(set) |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
16 | def __getitem__(self, blockid): |
128 | 17 | ''' |
18 | Returns a block for block coordinates. The block is a set that can | |
19 | contain anything. | |
20 | ''' | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
21 | return self.blocks[blockid] |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
22 | def blockpoint(self, point): |
128 | 23 | ''' |
24 | Returns a block point for a location. The block point is a | |
25 | coordinate in the blockmap. | |
26 | ''' | |
27 | block_coordinate = lambda x: int(x / self.block_factor) | |
28 | return block_coordinate(point.x), block_coordinate(point.y) | |
29 | ||
30 | def minmax(data): | |
31 | ''' | |
32 | Computes the minimum and maximum values in one-pass using only | |
33 | 1.5*len(data) comparisons | |
34 | ''' | |
35 | import itertools | |
36 | it = iter(data) | |
37 | try: | |
38 | lo = hi = next(it) | |
39 | except StopIteration: | |
40 | raise ValueError('minmax() arg is an empty sequence') | |
41 | for x, y in itertools.zip_longest(it, it, fillvalue = lo): | |
42 | if x > y: | |
43 | x, y = y, x | |
44 | lo = min(x, lo) | |
45 | hi = max(y, hi) | |
46 | return lo, hi | |
2 | 47 | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
48 | def blocks_in_shape(blockmap, shape): |
128 | 49 | ''' |
50 | Finds all blocks inside the bounding box of a shape. | |
51 | ''' | |
52 | from itertools import product | |
53 | min_x, max_x = minmax(point.x for point in shape.points) | |
54 | min_y, max_y = minmax(point.y for point in shape.points) | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
55 | 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
|
56 | max_blockpoint = blockmap.blockpoint(Location(max_x, max_y)) |
128 | 57 | range_x = range(min_blockpoint[0], max_blockpoint[0] + 1) |
58 | range_y = range(min_blockpoint[1], max_blockpoint[1] + 1) | |
59 | yield from (blockmap[x, y] for x, y in product(range_x, range_y)) | |
2 | 60 | |
128 | 61 | def location_from_row(row): |
62 | return Location(float(row['stop_lat']), float(row['stop_lon'])) | |
63 | ||
64 | def read_bus_stops(archive_path): | |
65 | bus_stops = dict() | |
66 | with ZipFile(archive_path) as archive: | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
67 | with archive.open('stops.txt') as file: |
128 | 68 | return { |
69 | row['stop_id']: location_from_row(row) | |
70 | for row in read_csv(map(bytes.decode, file)) | |
71 | } | |
72 | ||
73 | def create_blockmap(regions): | |
74 | ''' | |
75 | Creates a blockmap of regions | |
76 | ''' | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
77 | blockmap = Blockmap() |
88
3b86597c5a88
major update, moved the map to an osm patch
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
78 | for region in regions.values(): |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
79 | 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
|
80 | set.add(block, region['name']) |
128 | 81 | return blockmap |
82 | ||
83 | def get_args(): | |
84 | from argparse import ArgumentParser | |
85 | parser = ArgumentParser() | |
86 | parser.add_argument('gtfs_zip') | |
87 | parser.add_argument('mapfile') | |
88 | return parser.parse_args() | |
89 | ||
90 | def compute_bus_stop_regions(regions, bus_stops): | |
126
369e242edc5d
commented and simplified
Teemu Piippo <teemu@hecknology.net>
parents:
125
diff
changeset
|
91 | # Find the region every node is in |
128 | 92 | blockmap = create_blockmap(regions) |
93 | bus_stop_regions = dict() | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
94 | 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
|
95 | 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
|
96 | region = regions[region_name] |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
97 | if region['shape'].contains_point(stop_position): |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
98 | bus_stop_regions[stop_id] = region['name'] |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
99 | break |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
100 | else: |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
101 | bus_stop_regions[stop_id] = None |
128 | 102 | return bus_stop_regions |
103 | ||
104 | def percentage(a): | |
105 | return '%.1f%%' % (a * 100) | |
106 | ||
107 | def main(): | |
108 | from regions import parse_regions | |
109 | args = get_args() | |
110 | regions = parse_regions(args.mapfile) | |
111 | bus_stops = read_bus_stops(args.gtfs_zip) | |
112 | bus_stop_regions = compute_bus_stop_regions(regions, bus_stops) | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
113 | 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
|
114 | total = len(bus_stops) |
128 | 115 | print('%s bus stops covered.' % percentage(covered / total), |
116 | file = sys.stderr) | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
117 | json.dump(bus_stop_regions, sys.stdout, indent = 2) |
2 | 118 | |
124
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
119 | if __name__ == '__main__': |
c3b022f51704
optimized region computation with a blockmap
Teemu Piippo <teemu@hecknology.net>
parents:
89
diff
changeset
|
120 | main() |