Files
TJWaterServer/api/s24_coordinates.py
2023-03-31 09:55:06 +08:00

41 lines
1.2 KiB
Python

from .database import *
def _to_client_point(coord: str) -> dict[str, float]:
xy = coord.removeprefix('(').removesuffix(')').split(',')
return { 'x': float(xy[0]), 'y': float(xy[1]) }
def get_node_coord(name: str, id: str) -> dict[str, float]:
row = try_read(name, f"select * from coordinates where node = '{id}'")
if row == None:
write(name, f"insert into coordinates (node, coord) values ('{id}', '(0.0,0.0)');")
return {'x': 0.0, 'y': 0.0}
return _to_client_point(row['coord'])
#--------------------------------------------------------------
# [EPA2][EPA3][IN][OUT]
# id x y
#--------------------------------------------------------------
# exception ! need merge to node change set !
def inp_in_coord(line: str) -> str:
tokens = line.split()
node = tokens[0]
coord = f"st_geomfromtext('point({tokens[1]} {tokens[2]})')"
return f"insert into coordinates (node, coord) values ('{node}', {coord});"
def inp_out_coord(name: str) -> list[str]:
lines = []
objs = read_all(name, 'select * from coordinates')
for obj in objs:
node = obj['node']
coord = _to_client_point(obj['coord'])
x = coord['x']
y = coord['y']
lines.append(f'{node} {x} {y}')
return lines