Refactor coordinate api
This commit is contained in:
@@ -3,6 +3,8 @@ from .connection import g_conn_dict as conn
|
||||
from .s0_base import *
|
||||
from .operation import *
|
||||
from .change_set import ChangeSet
|
||||
from .s24_coordinates import *
|
||||
|
||||
|
||||
def add_tank(name: str, id: str, x: float, y: float, elevation: float, init_level: float = 0, min_level: float = 0, max_level: float = 0, diameter: float = 0, min_vol: float = 0) -> ChangeSet:
|
||||
if is_node(name, id):
|
||||
@@ -120,22 +122,8 @@ def get_tank_overflow(name: str, id: str) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _to_point(coord: str) -> dict[str, float]:
|
||||
coord = coord.removeprefix('(')
|
||||
coord = coord.removesuffix(')')
|
||||
coord = coord.split(',')
|
||||
return { 'x': float(coord[0]), 'y': float(coord[1]) }
|
||||
|
||||
|
||||
def get_tank_coord(name: str, id: str) -> dict[str, float] | None:
|
||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(f"select * from coordinates where node = '{id}'")
|
||||
row = cur.fetchone()
|
||||
if row == None:
|
||||
return None
|
||||
|
||||
coord = str(row['coord'])
|
||||
return _to_point(coord)
|
||||
return get_node_coord(name, id)
|
||||
|
||||
|
||||
def set_tank_elevation(name: str, id: str, elevation: float) -> ChangeSet:
|
||||
@@ -312,22 +300,4 @@ def set_tank_overflow(name: str, id: str, overflow: str) -> ChangeSet:
|
||||
|
||||
|
||||
def set_tank_coord(name: str, id: str, x: float, y: float) -> ChangeSet:
|
||||
if not is_tank(name, id):
|
||||
return
|
||||
|
||||
old = get_tank_coord(name, id)
|
||||
if old == None:
|
||||
return
|
||||
old_x, old_y = old['x'], old['y']
|
||||
|
||||
with conn[name].cursor() as cur:
|
||||
sql = f"update coordinates set coord = '({x},{y})' where node = '{id}'"
|
||||
cur.execute(sql)
|
||||
|
||||
redo = sql.replace("'", '"')
|
||||
undo = f'update coordinates set coord = "({old_x},{old_y})" where node = "{id}"'
|
||||
add_operation(name, redo, undo)
|
||||
|
||||
change = ChangeSet()
|
||||
change.update('tank', id, 'coord', 'point', str({'x': x, 'y': y}))
|
||||
return change
|
||||
return set_node_coord(name, id, x, y)
|
||||
|
||||
Reference in New Issue
Block a user