diff --git a/api/s32_virtual_district.py b/api/s32_virtual_district.py index 85b8585..6ce4682 100644 --- a/api/s32_virtual_district.py +++ b/api/s32_virtual_district.py @@ -1,6 +1,16 @@ from .database import * from .s0_base import get_node_links + +def _polygon_to_nodes(polygon: str) -> list[tuple[float, float]]: + boundary = polygon.removeprefix('POLYGON((').removesuffix('))').split(',') + xys = [] + for pt in boundary: + xy = pt.split(' ') + xys.append((float(xy[0]), float(xy[1]))) + return xys + + def calculate_virtual_district(name: str, centers: list[str]) -> dict[str, Any]: write(name, 'drop table if exists vd_graph') write(name, 'create table vd_graph (id serial, source integer, target integer, cost numeric)') @@ -71,15 +81,8 @@ def calculate_virtual_district(name: str, centers: list[str]) -> dict[str, Any]: # TODO: check none boundary = read(name, f'select st_astext(st_convexhull(st_collect(array(select coord from coordinates where node in (select * from vd_{center}))))) as boundary' )['boundary'] # write(name, f"insert into virtual_district (id, center, boundary) values ('vd_{center}', '{center}', st_geomfromtext('{boundary}'))") - - outlines = str(boundary).removeprefix('POLYGON((').removesuffix('))').split(',') - outline_tuples = [] - for pt in outlines: - xy = pt.split(' ') - outline_tuples.append((float(xy[0]), float(xy[1]))) - - vd = { 'center': center, 'nodes': value, 'boundary': outline_tuples } - vds.append(vd) + xys = _polygon_to_nodes(boundary) + vds.append({ 'center': center, 'nodes': value, 'boundary': xys }) write(name, f'drop table vd_{center}')