Code refactor

This commit is contained in:
WQY\qiong
2023-04-29 11:18:55 +08:00
parent a62700d27e
commit c6518cbae1

View File

@@ -1,6 +1,16 @@
from .database import * from .database import *
from .s0_base import get_node_links 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]: def calculate_virtual_district(name: str, centers: list[str]) -> dict[str, Any]:
write(name, 'drop table if exists vd_graph') write(name, 'drop table if exists vd_graph')
write(name, 'create table vd_graph (id serial, source integer, target integer, cost numeric)') 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 # 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'] 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}'))") # write(name, f"insert into virtual_district (id, center, boundary) values ('vd_{center}', '{center}', st_geomfromtext('{boundary}'))")
xys = _polygon_to_nodes(boundary)
outlines = str(boundary).removeprefix('POLYGON((').removesuffix('))').split(',') vds.append({ 'center': center, 'nodes': value, 'boundary': xys })
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)
write(name, f'drop table vd_{center}') write(name, f'drop table vd_{center}')