compute-regions.py

Fri, 13 Apr 2018 17:32:40 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 13 Apr 2018 17:32:40 +0300
changeset 88
3b86597c5a88
parent 73
c56b0ef96514
child 89
438d77bca50e
permissions
-rwxr-xr-x

major update, moved the map to an osm patch

#!/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[1])
bus_stops = {}

with ZipFile('gtfs.zip') 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