compute-regions.py

changeset 88
3b86597c5a88
parent 73
c56b0ef96514
child 89
438d77bca50e
--- a/compute-regions.py	Thu Mar 29 23:55:36 2018 +0300
+++ b/compute-regions.py	Fri Apr 13 17:32:40 2018 +0300
@@ -4,67 +4,33 @@
 from geometry import *
 from zipfile import ZipFile
 from configparser import ConfigParser
+from regions import parse_regions
 
 representatives = {}
 
-region_data = ConfigParser()
-region_data.read('regions.ini')
-
-for section in region_data.sections():
-	for key, value in region_data[section].items():
-		representatives[key] = value
-
-with open('regions.gmp') as file:
-	data = file.read().split('@')
-
+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 = Sijainti(float(row['stop_lat']), float(row['stop_lon']))
+			location = Location(float(row['stop_lat']), float(row['stop_lon']))
 			reference = row['stop_id']
 			bus_stops[reference] = location
 
-district_shapes = []
+region_shapes = []
 districts = {}
-
-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))
-	if polygon[0] != 'undefined':
-		districts[polygon[0]] = district_shapes[-1]
-
-bus_stop_districts = {}
-
-for name, stop_id in representatives.items():
-	if stop_id:
-		if stop_id not in bus_stops:
-			print('Representative %r for region %r not found in schedule' % (stop_id, name), file = sys.stderr)
-		else:
-			for district_shape in district_shapes:
-				if district_shape.sisältää_pisteen(bus_stops[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)
+bus_stop_regions = {}
 
 for stop_id, stop_position in bus_stops.items():
-	for district, shape in districts.items():
-		if shape.sisältää_pisteen(stop_position):
-			bus_stop_districts[stop_id] = district
+	for region in regions.values():
+		if region['shape'].contains_point(stop_position):
+			bus_stop_regions[stop_id] = region['name']
 			break
 	else:
-		bus_stop_districts[stop_id] = None
+		bus_stop_regions[stop_id] = None
 
-covered = sum(1 if value else 0 for value in bus_stop_districts.values())
+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_districts, sys.stdout, indent = 2)
+json.dump(bus_stop_regions, sys.stdout, indent = 2)

mercurial