compute-regions.py

changeset 88
3b86597c5a88
parent 73
c56b0ef96514
child 89
438d77bca50e
equal deleted inserted replaced
87:9139a94e540c 88:3b86597c5a88
2 import sys, json 2 import sys, json
3 from misc import * 3 from misc import *
4 from geometry import * 4 from geometry import *
5 from zipfile import ZipFile 5 from zipfile import ZipFile
6 from configparser import ConfigParser 6 from configparser import ConfigParser
7 from regions import parse_regions
7 8
8 representatives = {} 9 representatives = {}
9 10
10 region_data = ConfigParser() 11 regions = parse_regions(sys.argv[1])
11 region_data.read('regions.ini')
12
13 for section in region_data.sections():
14 for key, value in region_data[section].items():
15 representatives[key] = value
16
17 with open('regions.gmp') as file:
18 data = file.read().split('@')
19
20 bus_stops = {} 12 bus_stops = {}
21 13
22 with ZipFile('gtfs.zip') as archive: 14 with ZipFile('gtfs.zip') as archive:
23 with archive.open('stops.txt') as file: 15 with archive.open('stops.txt') as file:
24 for row in read_csv(map(bytes.decode, file)): 16 for row in read_csv(map(bytes.decode, file)):
25 location = Sijainti(float(row['stop_lat']), float(row['stop_lon'])) 17 location = Location(float(row['stop_lat']), float(row['stop_lon']))
26 reference = row['stop_id'] 18 reference = row['stop_id']
27 bus_stops[reference] = location 19 bus_stops[reference] = location
28 20
29 district_shapes = [] 21 region_shapes = []
30 districts = {} 22 districts = {}
31 23 bus_stop_regions = {}
32 for polygon in data[1].splitlines():
33 polygon = polygon.split('^')
34 coordinates = []
35 for point in polygon[3].split('~'):
36 x, y = point.split(',')
37 coordinates.append(Sijainti(float(x), float(y)))
38 district_shapes.append(Monikulmio(*coordinates))
39 if polygon[0] != 'undefined':
40 districts[polygon[0]] = district_shapes[-1]
41
42 bus_stop_districts = {}
43
44 for name, stop_id in representatives.items():
45 if stop_id:
46 if stop_id not in bus_stops:
47 print('Representative %r for region %r not found in schedule' % (stop_id, name), file = sys.stderr)
48 else:
49 for district_shape in district_shapes:
50 if district_shape.sisältää_pisteen(bus_stops[stop_id]):
51 assert name not in districts
52 districts[name] = district_shape
53 district_shapes.remove(district_shape)
54 bus_stop_districts[stop_id] = name
55 break
56 else:
57 print('Cannot find a shape for %r' % name, file = sys.stderr)
58 24
59 for stop_id, stop_position in bus_stops.items(): 25 for stop_id, stop_position in bus_stops.items():
60 for district, shape in districts.items(): 26 for region in regions.values():
61 if shape.sisältää_pisteen(stop_position): 27 if region['shape'].contains_point(stop_position):
62 bus_stop_districts[stop_id] = district 28 bus_stop_regions[stop_id] = region['name']
63 break 29 break
64 else: 30 else:
65 bus_stop_districts[stop_id] = None 31 bus_stop_regions[stop_id] = None
66 32
67 covered = sum(1 if value else 0 for value in bus_stop_districts.values()) 33 covered = sum(1 if value else 0 for value in bus_stop_regions.values())
68 total = len(bus_stops) 34 total = len(bus_stops)
69 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr) 35 print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
70 json.dump(bus_stop_districts, sys.stdout, indent = 2) 36 json.dump(bus_stop_regions, sys.stdout, indent = 2)

mercurial