204 lines
6.9 KiB
Python
204 lines
6.9 KiB
Python
from typing import Any
|
|
|
|
from ..core.database import (
|
|
ChangeSet,
|
|
DatabaseCommand,
|
|
execute_command,
|
|
execute_locked_command,
|
|
g_add_prefix,
|
|
g_delete_prefix,
|
|
g_update_prefix,
|
|
read_all,
|
|
sql_literal,
|
|
try_read,
|
|
)
|
|
from ..gis.coordinates import (
|
|
get_node_coord,
|
|
sql_delete_coord,
|
|
sql_insert_coord,
|
|
sql_update_coord,
|
|
)
|
|
from .elements import get_all_node_links, get_node_links
|
|
|
|
|
|
def get_reservoir_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'head' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
'pattern' : {'type': 'str' , 'optional': True , 'readonly': False},
|
|
'links' : {'type': 'str_list' , 'optional': False , 'readonly': True } }
|
|
|
|
|
|
def get_reservoir(name: str, id: str) -> dict[str, Any]:
|
|
r = try_read(name, "select node_id as id, head, pattern_id as pattern from network.reservoirs where node_id = %s", (id,))
|
|
if r == None:
|
|
return {}
|
|
xy = get_node_coord(name, id)
|
|
d = {}
|
|
d['id'] = str(r['id'])
|
|
d['x'] = float(xy['x'])
|
|
d['y'] = float(xy['y'])
|
|
d['head'] = float(r['head'])
|
|
d['pattern'] = str(r['pattern']) if r['pattern'] != None else None
|
|
d['links'] = get_node_links(name, id)
|
|
return d
|
|
|
|
# DingZQ, 2025-03-29
|
|
def get_all_reservoirs(name: str) -> list[dict[str, Any]]:
|
|
rows = read_all(
|
|
name,
|
|
"SELECT id, head, pattern_id AS pattern, x, y "
|
|
"FROM gis.reservoirs ORDER BY id",
|
|
)
|
|
if rows == None:
|
|
return []
|
|
|
|
links_by_node = get_all_node_links(name)
|
|
result = []
|
|
for row in rows:
|
|
d = {}
|
|
id = str(row['id'])
|
|
d['id'] = id
|
|
d['x'] = float(row['x'] or 0.0)
|
|
d['y'] = float(row['y'] or 0.0)
|
|
d['head'] = float(row['head']) if row['head'] != None else None
|
|
d['pattern'] = str(row['pattern']) if row['pattern'] != None else None
|
|
d['links'] = links_by_node.get(id, [])
|
|
result.append(d)
|
|
|
|
return result
|
|
|
|
class Reservoir(object):
|
|
def __init__(self, input: dict[str, Any]) -> None:
|
|
self.type = 'reservoir'
|
|
self.id = str(input['id'])
|
|
self.x = float(input['x'])
|
|
self.y = float(input['y'])
|
|
self.head = float(input['head'])
|
|
self.pattern = str(input['pattern']) if 'pattern' in input and input['pattern'] != None else None
|
|
|
|
self.f_type = sql_literal(self.type)
|
|
self.f_id = sql_literal(self.id)
|
|
self.f_head = sql_literal(self.head)
|
|
self.f_pattern = sql_literal(self.pattern)
|
|
|
|
def as_dict(self) -> dict[str, Any]:
|
|
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'head': self.head, 'pattern': self.pattern }
|
|
|
|
def _set_reservoir(
|
|
name: str,
|
|
cs: ChangeSet,
|
|
current: dict[str, Any] | None = None,
|
|
) -> DatabaseCommand:
|
|
raw_new = current if current is not None else get_reservoir(name, cs.operations[0]['id'])
|
|
|
|
new_dict = cs.operations[0]
|
|
schema = get_reservoir_schema(name)
|
|
for key, value in schema.items():
|
|
if key in new_dict and not value['readonly']:
|
|
raw_new[key] = new_dict[key]
|
|
new = Reservoir(raw_new)
|
|
|
|
statement = f"update network.reservoirs set head = {new.f_head}, pattern_id = {new.f_pattern} where node_id = {new.f_id};"
|
|
statement += f"\n{sql_update_coord(new.id, new.x, new.y)}"
|
|
|
|
change = g_update_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def set_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
|
operation = cs.operations[0]
|
|
if 'id' not in operation:
|
|
return ChangeSet()
|
|
|
|
def build_command() -> DatabaseCommand | None:
|
|
current = get_reservoir(name, operation['id'])
|
|
return None if current == {} else _set_reservoir(name, cs, current)
|
|
|
|
return execute_locked_command(name, build_command)
|
|
|
|
|
|
def _add_reservoir(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
new = Reservoir(cs.operations[0])
|
|
|
|
statement = f"insert into network.nodes (id, node_type) values ({new.f_id}, {new.f_type});"
|
|
statement += f"\ninsert into network.reservoirs (node_id, head, pattern_id) values ({new.f_id}, {new.f_head}, {new.f_pattern});"
|
|
statement += f"\n{sql_insert_coord(new.id, new.x, new.y)}"
|
|
|
|
change = g_add_prefix | new.as_dict()
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def add_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_reservoir(name, cs.operations[0]['id']) != {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _add_reservoir(name, cs))
|
|
|
|
|
|
def _delete_reservoir(name: str, cs: ChangeSet) -> DatabaseCommand:
|
|
element_id = str(cs.operations[0]['id'])
|
|
f_id = sql_literal(element_id)
|
|
|
|
statement = sql_delete_coord(element_id)
|
|
statement += f"\ndelete from network.nodes where id = {f_id};"
|
|
|
|
change = g_delete_prefix | {'type': 'reservoir', 'id': element_id}
|
|
|
|
return DatabaseCommand(statement, [change])
|
|
|
|
|
|
def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
|
if 'id' not in cs.operations[0]:
|
|
return ChangeSet()
|
|
if get_reservoir(name, cs.operations[0]['id']) == {}:
|
|
return ChangeSet()
|
|
return execute_command(name, _delete_reservoir(name, cs))
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
# [EPA2][EPA3][IN][OUT]
|
|
# id elev (pattern) ;desc
|
|
#--------------------------------------------------------------
|
|
|
|
|
|
def inp_in_reservoir(line: str) -> str:
|
|
tokens = line.split()
|
|
|
|
num = len(tokens)
|
|
has_desc = tokens[-1].startswith(';')
|
|
num_without_desc = (num - 1) if has_desc else num
|
|
|
|
id = str(tokens[0])
|
|
head = float(tokens[1])
|
|
pattern = str(tokens[2]) if num_without_desc >= 3 else None
|
|
desc = str(tokens[-1]) if has_desc else None
|
|
|
|
return str(f"insert into network.nodes (id, node_type) values ({sql_literal(id)}, 'reservoir');insert into network.reservoirs (node_id, head, pattern_id) values ({sql_literal(id)}, {sql_literal(head)}, {sql_literal(pattern)});")
|
|
|
|
|
|
def inp_out_reservoir(name: str) -> list[str]:
|
|
lines = []
|
|
objs = read_all(name, 'select node_id as id, head, pattern_id as pattern from network.reservoirs order by node_id')
|
|
for obj in objs:
|
|
id = obj['id']
|
|
head = obj['head']
|
|
pattern = obj['pattern'] if obj['pattern'] != None else ''
|
|
desc = ';'
|
|
lines.append(f'{id} {head} {pattern} {desc}')
|
|
return lines
|
|
|
|
|
|
def unset_reservoir_by_pattern(name: str, pattern: str) -> ChangeSet:
|
|
cs = ChangeSet()
|
|
|
|
rows = read_all(name, "select node_id as id from network.reservoirs where pattern_id = %s", (pattern,))
|
|
for row in rows:
|
|
cs.append(g_update_prefix | {'type': 'reservoir', 'id': row['id'], 'pattern': None})
|
|
|
|
return cs
|