Drop demand & pattern from junction

This commit is contained in:
WQY\qiong
2023-03-01 22:59:40 +08:00
parent e83d3a83a5
commit 628258ef52
6 changed files with 18 additions and 133 deletions

View File

@@ -8,8 +8,6 @@ def get_junction_schema(name: str) -> dict[str, dict[str, Any]]:
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
'elevation' : {'type': 'float' , 'optional': False , 'readonly': False},
'demand' : {'type': 'float' , 'optional': True , 'readonly': False},
'pattern' : {'type': 'str' , 'optional': True , 'readonly': False},
'links' : {'type': 'str_list' , 'optional': False , 'readonly': True } }
@@ -21,8 +19,6 @@ def get_junction(name: str, id: str) -> dict[str, Any]:
d['x'] = float(xy['x'])
d['y'] = float(xy['y'])
d['elevation'] = float(j['elevation'])
d['demand'] = float(j['demand']) if j['demand'] != None else None
d['pattern'] = str(j['pattern']) if j['pattern'] != None else None
d['links'] = get_node_links(name, id)
return d
@@ -34,18 +30,14 @@ class Junction(object):
self.x = float(input['x'])
self.y = float(input['y'])
self.elevation = float(input['elevation'])
self.demand = float(input['demand']) if 'demand' in input and input['demand'] != None else None
self.pattern = str(input['pattern']) if 'pattern' in input and input['pattern'] != None else None
self.f_type = f"'{self.type}'"
self.f_id = f"'{self.id}'"
self.f_coord = f"'({self.x}, {self.y})'"
self.f_elevation = self.elevation
self.f_demand = self.demand if self.demand != None else 'null'
self.f_pattern = f"'{self.pattern}'" if self.pattern != None else 'null'
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation, 'demand': self.demand, 'pattern': self.pattern }
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation }
def as_id_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id }
@@ -62,11 +54,11 @@ def set_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
raw_new[key] = new_dict[key]
new = Junction(raw_new)
redo_sql = f"update junctions set elevation = {new.f_elevation}, demand = {new.f_demand}, pattern = {new.f_pattern} where id = {new.f_id};"
redo_sql = f"update junctions set elevation = {new.f_elevation} where id = {new.f_id};"
redo_sql += f"\nupdate coordinates set coord = {new.f_coord} where node = {new.f_id};"
undo_sql = f"update coordinates set coord = {old.f_coord} where node = {old.f_id};"
undo_sql += f"\nupdate junctions set elevation = {old.f_elevation}, demand = {old.f_demand}, pattern = {old.f_pattern} where id = {old.f_id};"
undo_sql += f"\nupdate junctions set elevation = {old.f_elevation} where id = {old.f_id};"
redo_cs = g_update_prefix | new.as_dict()
undo_cs = g_update_prefix | old.as_dict()
@@ -82,7 +74,7 @@ def add_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
new = Junction(cs.operations[0])
redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});"
redo_sql += f"\ninsert into junctions (id, elevation, demand, pattern) values ({new.f_id}, {new.f_elevation}, {new.f_demand}, {new.f_pattern});"
redo_sql += f"\ninsert into junctions (id, elevation) values ({new.f_id}, {new.f_elevation});"
redo_sql += f"\ninsert into coordinates (node, coord) values ({new.f_id}, {new.f_coord});"
undo_sql = f"delete from coordinates where node = {new.f_id};"
@@ -107,7 +99,7 @@ def delete_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
redo_sql += f"\ndelete from _node where id = {old.f_id};"
undo_sql = f"insert into _node (id, type) values ({old.f_id}, {old.f_type});"
undo_sql += f"\ninsert into junctions (id, elevation, demand, pattern) values ({old.f_id}, {old.f_elevation}, {old.f_demand}, {old.f_pattern});"
undo_sql += f"\ninsert into junctions (id, elevation) values ({old.f_id}, {old.f_elevation});"
undo_sql += f"\ninsert into coordinates (node, coord) values ({old.f_id}, {old.f_coord});"
redo_cs = g_delete_prefix | old.as_id_dict()
@@ -150,6 +142,7 @@ def inp_in_junction(section: list[str]) -> ChangeSet:
continue
obj = InpJunction(s)
cs.append(g_add_prefix | {'type': 'junction', 'id': obj.id, 'elevation': obj.elevation, 'demand': obj.demand, 'pattern': obj.pattern})
cs.append(g_update_prefix | { 'type': 'demand', 'junction': obj.id, 'demands': [{'demand': obj.demand, 'pattern': obj.pattern, 'category': None}] })
return cs
@@ -159,18 +152,6 @@ def inp_out_junction(name: str) -> list[str]:
for obj in objs:
id = obj['id']
elev = obj['elevation']
demand = obj['demand'] if obj['demand'] != None else ''
pattern = obj['pattern'] if obj['pattern'] != None else ''
desc = ';'
lines.append(f'{id} {elev} {demand} {pattern} {desc}')
lines.append(f'{id} {elev} {desc}')
return lines
def unset_junction_by_pattern(name: str, pattern: str) -> ChangeSet:
cs = ChangeSet()
rows = read_all(name, f"select id from junctions where pattern = '{pattern}'")
for row in rows:
cs.append(g_update_prefix | {'type': 'junction', 'id': row['id'], 'pattern': None})
return cs