compute-regions.py

Sat, 22 Sep 2018 00:08:43 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 22 Sep 2018 00:08:43 +0300
changeset 122
942f6b3e4d4a
parent 89
438d77bca50e
child 124
c3b022f51704
permissions
-rwxr-xr-x

split up Ruissalo

#!/usr/bin/env python3
import sys, json
from misc import *
from geometry import *
from zipfile import ZipFile
from configparser import ConfigParser
from regions import parse_regions

representatives = {}

regions = parse_regions(sys.argv[2])
bus_stops = {}

with ZipFile(sys.argv[1]) as archive:
	with archive.open('stops.txt') as file:
		for row in read_csv(map(bytes.decode, file)):
			location = Location(float(row['stop_lat']), float(row['stop_lon']))
			reference = row['stop_id']
			bus_stops[reference] = location

region_shapes = []
districts = {}
bus_stop_regions = {}

for stop_id, stop_position in bus_stops.items():
	for region in regions.values():
		if region['shape'].contains_point(stop_position):
			bus_stop_regions[stop_id] = region['name']
			break
	else:
		bus_stop_regions[stop_id] = None

covered = sum(1 if value else 0 for value in bus_stop_regions.values())
total = len(bus_stops)
print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
json.dump(bus_stop_regions, sys.stdout, indent = 2)

mercurial