alueet.py

Sat, 10 Jun 2017 16:36:36 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 10 Jun 2017 16:36:36 +0300
changeset 6
88cfb916c852
parent 5
a65b680f1774
permissions
-rwxr-xr-x

Plöö

#!/usr/bin/env python3
import sys, json
from misc import *
from geometria import *

with open('alueet.gmp') as file:
	data = file.read().split('@')

with open('alue-edustajat.json') as file:
	representatives = json.load(file)

pysäkit = {}

with open('gtfs/stops.txt') as file:
	for rivi in lue_csv(file):
		sijainti = Sijainti(float(rivi['stop_lat']), float(rivi['stop_lon']))
		tunniste = rivi['stop_id']
		pysäkit[tunniste] = sijainti

district_shapes = []
for polygon in data[1].splitlines():
	polygon = polygon.split('^')
	coordinates = []
	for point in polygon[3].split('~'):
		x, y = point.split(',')
		coordinates.append(Sijainti(float(x), float(y)))
	district_shapes.append(Monikulmio(*coordinates))

districts = {}
bus_stop_districts = {}

for name, stop_id in representatives.items():
	if stop_id is None:
		continue
	if stop_id not in pysäkit:
		print('Alueen %r edustaja %r ei löydy aikatauluista' % (name, stop_id), file = sys.stderr)
	else:
		for district_shape in district_shapes:
			if district_shape.sisältää_pisteen(pysäkit[stop_id]):
				assert name not in districts
				districts[name] = district_shape
				district_shapes.remove(district_shape)
				bus_stop_districts[stop_id] = name
				break
		else:
			print('cannot find a shape for %r' % name, file = sys.stderr)

for stop_id, stop_position in pysäkit.items():
	for district, shape in districts.items():
		if shape.sisältää_pisteen(stop_position):
			bus_stop_districts[stop_id] = district
			break
	else:
		bus_stop_districts[stop_id] = None

covered = sum(int(bool(k)) for k in bus_stop_districts.values())
total = len(pysäkit)
print('%.1f%% bus stops covered.' % (covered * 100 / total), file = sys.stderr)
json.dump(bus_stop_districts, sys.stdout, indent = 2)

mercurial