compute-regions.py

Thu, 06 Dec 2018 23:39:20 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 06 Dec 2018 23:39:20 +0200
changeset 134
4ac0f2e2ec4e
parent 130
9c68fa498050
permissions
-rwxr-xr-x

removed uuid mapping

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

mercurial