Replace coordinate with postgis geometry

This commit is contained in:
WQY\qiong
2023-04-29 10:09:12 +08:00
parent 089a12e137
commit 0ac4a88295
4 changed files with 39 additions and 28 deletions

View File

@@ -1,17 +1,31 @@
from .database import *
def sql_update_coord(node: str, x: float, y: float) -> str:
coord = f"st_geomfromtext('point({x} {y})')"
return f"update coordinates set coord = {coord} where node = '{node}';"
def sql_insert_coord(node: str, x: float, y: float) -> str:
coord = f"st_geomfromtext('point({x} {y})')"
return f"insert into coordinates (node, coord) values ('{node}', {coord});"
def sql_delete_coord(node: str) -> str:
return f"delete from coordinates where node = '{node}';"
def _to_client_point(coord: str) -> dict[str, float]:
xy = coord.removeprefix('(').removesuffix(')').split(',')
xy = coord.lower().removeprefix('point(').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}'")
def get_node_coord(name: str, node: str) -> dict[str, float]:
row = try_read(name, f"select st_astext(coord) as coord_geom from coordinates where node = '{node}'")
if row == None:
write(name, f"insert into coordinates (node, coord) values ('{id}', '(0.0,0.0)');")
write(name, sql_insert_coord(node, 0.0, 0.0))
return {'x': 0.0, 'y': 0.0}
return _to_client_point(row['coord'])
return _to_client_point(row['coord_geom'])
#--------------------------------------------------------------
@@ -30,10 +44,10 @@ def inp_in_coord(line: str) -> str:
def inp_out_coord(name: str) -> list[str]:
lines = []
objs = read_all(name, 'select * from coordinates')
objs = read_all(name, 'select node, st_astext(coord) as coord_geom from coordinates')
for obj in objs:
node = obj['node']
coord = _to_client_point(obj['coord'])
coord = _to_client_point(obj['coord_geom'])
x = coord['x']
y = coord['y']
lines.append(f'{node} {x} {y}')