67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
from ..core.database import read_all, sql_literal, try_read
|
|
|
|
|
|
def sql_update_coord(node: str, x: float, y: float) -> str:
|
|
geom = f"st_setsrid(st_makepoint({sql_literal(x)}, {sql_literal(y)}), 900914)"
|
|
return f"update gis.node_geometries set geom = {geom} where node_id = {sql_literal(node)};"
|
|
|
|
|
|
def sql_insert_coord(node: str, x: float, y: float) -> str:
|
|
geom = f"st_setsrid(st_makepoint({sql_literal(x)}, {sql_literal(y)}), 900914)"
|
|
return f"insert into gis.node_geometries (node_id, geom) values ({sql_literal(node)}, {geom});"
|
|
|
|
|
|
def sql_delete_coord(node: str) -> str:
|
|
return f"delete from gis.node_geometries where node_id = {sql_literal(node)};"
|
|
|
|
|
|
def from_postgis_point(coord: str) -> dict[str, float]:
|
|
xy = coord.lower().removeprefix("point(").removesuffix(")").split(" ")
|
|
return {"x": float(xy[0]), "y": float(xy[1])}
|
|
|
|
|
|
def get_node_coord(name: str, node: str) -> dict[str, float]:
|
|
row = try_read(
|
|
name,
|
|
"select st_astext(geom) as coord_geom from gis.node_geometries where node_id = %s",
|
|
(node,),
|
|
)
|
|
if row is None:
|
|
return {"x": 0.0, "y": 0.0}
|
|
return from_postgis_point(row["coord_geom"])
|
|
|
|
|
|
def node_has_coord(name: str, node: str) -> bool:
|
|
return try_read(
|
|
name, "select node_id from gis.node_geometries where node_id = %s", (node,)
|
|
) is not None
|
|
|
|
|
|
#--------------------------------------------------------------
|
|
# [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]
|
|
x, y = float(tokens[1]), float(tokens[2])
|
|
return sql_insert_coord(node, x, y)
|
|
|
|
|
|
def inp_out_coord(name: str) -> list[str]:
|
|
lines = []
|
|
objs = read_all(
|
|
name,
|
|
"select node_id, st_astext(geom) as coord_geom from gis.node_geometries",
|
|
)
|
|
for obj in objs:
|
|
node = obj["node_id"]
|
|
coord = from_postgis_point(obj["coord_geom"])
|
|
x = coord["x"]
|
|
y = coord["y"]
|
|
lines.append(f"{node} {x} {y}")
|
|
return lines
|