refactor(db)!: finalize pooled WNDB v2 migration
This commit is contained in:
@@ -2,10 +2,8 @@ import platform
|
||||
import math
|
||||
from typing import Any
|
||||
import pyclipper
|
||||
from ..model.elements import get_node_links, get_link_nodes, is_pipe
|
||||
from ..model.pipes import get_pipe
|
||||
from ..core.database import read, try_read, read_all
|
||||
from .coordinates import node_has_coord, get_node_coord
|
||||
from .network_views import get_boundary_link_ids, get_topology_rows
|
||||
|
||||
|
||||
def from_postgis_polygon(polygon: str) -> list[tuple[float, float]]:
|
||||
@@ -42,21 +40,7 @@ def get_nodes_in_boundary(name: str, boundary: list[tuple[float, float]]) -> lis
|
||||
|
||||
|
||||
def _get_links_on_boundary(name: str, nodes: list[str]) -> list[str]:
|
||||
links: list[str] = []
|
||||
|
||||
for node in nodes:
|
||||
node_links = get_node_links(name, node)
|
||||
for link in node_links:
|
||||
if link in links:
|
||||
continue
|
||||
|
||||
link_nodes = get_link_nodes(name, link)
|
||||
if link_nodes[0] in nodes and link_nodes[1] not in nodes:
|
||||
links.append(link)
|
||||
elif link_nodes[0] not in nodes and link_nodes[1] in nodes:
|
||||
links.append(link)
|
||||
|
||||
return links
|
||||
return get_boundary_link_ids(name, nodes)
|
||||
|
||||
|
||||
def get_nodes_in_region(name: str, region_id: str) -> list[str]:
|
||||
@@ -128,37 +112,39 @@ def _angle_of_node_link(node: str, link: str, nodes, links) -> float:
|
||||
|
||||
class Topology:
|
||||
def __init__(self, db: str, nodes: list[str]) -> None:
|
||||
self._nodes: dict[str, Any] = {}
|
||||
self._max_x_node = ''
|
||||
self._node_list: list[str] = []
|
||||
for node in nodes:
|
||||
if not node_has_coord(db, node):
|
||||
continue
|
||||
if get_node_links(db, node) == 0:
|
||||
continue
|
||||
self._nodes[node] = get_node_coord(db, node) | { 'links': [] }
|
||||
self._node_list.append(node)
|
||||
if self._max_x_node == '' or self._nodes[node]['x'] > self._nodes[self._max_x_node]['x']:
|
||||
self._max_x_node = node
|
||||
node_rows, link_rows = get_topology_rows(db, nodes)
|
||||
|
||||
self._nodes: dict[str, Any] = {
|
||||
str(row["id"]): {
|
||||
"x": float(row["x"]),
|
||||
"y": float(row["y"]),
|
||||
"type": str(row["node_type"]),
|
||||
"links": [],
|
||||
}
|
||||
for row in node_rows
|
||||
}
|
||||
self._node_list = list(self._nodes)
|
||||
self._max_x_node = max(
|
||||
self._nodes,
|
||||
key=lambda node_id: self._nodes[node_id]["x"],
|
||||
default="",
|
||||
)
|
||||
|
||||
self._links: dict[str, Any] = {}
|
||||
self._link_list: list[str] = []
|
||||
for node in self._nodes:
|
||||
for link in get_node_links(db, node):
|
||||
candidate = True
|
||||
link_nodes = get_link_nodes(db, link)
|
||||
for link_node in link_nodes:
|
||||
if link_node not in self._nodes:
|
||||
candidate = False
|
||||
break
|
||||
if candidate:
|
||||
length = get_pipe(db, link)['length'] if is_pipe(db, link) else 0.0
|
||||
self._links[link] = { 'node1' : link_nodes[0], 'node2' : link_nodes[1], 'length' : length }
|
||||
self._link_list.append(link)
|
||||
if link not in self._nodes[link_nodes[0]]['links']:
|
||||
self._nodes[link_nodes[0]]['links'].append(link)
|
||||
if link not in self._nodes[link_nodes[1]]['links']:
|
||||
self._nodes[link_nodes[1]]['links'].append(link)
|
||||
for row in link_rows:
|
||||
link_id = str(row["id"])
|
||||
node1 = str(row["start_node_id"])
|
||||
node2 = str(row["end_node_id"])
|
||||
if node1 not in self._nodes or node2 not in self._nodes:
|
||||
continue
|
||||
self._links[link_id] = {
|
||||
"node1": node1,
|
||||
"node2": node2,
|
||||
"length": float(row["length"]),
|
||||
}
|
||||
self._nodes[node1]["links"].append(link_id)
|
||||
self._nodes[node2]["links"].append(link_id)
|
||||
self._link_list = list(self._links)
|
||||
|
||||
def nodes(self):
|
||||
return self._nodes
|
||||
|
||||
Reference in New Issue
Block a user