diff --git a/api/__init__.py b/api/__init__.py index b79e53c..6512225 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -138,7 +138,7 @@ from .s31_scada_element import get_scada_element_schema, get_scada_element, set_ from .s31_scada_element import get_all_scada_element_ids, get_all_scada_elements from .clean_api import clean_scada_element -from .s32_region_util import get_nodes_in_boundary, get_nodes_in_region, calculate_convex_hull, calculate_boundary, inflate_boundary, inflate_region +from .s32_region_util import get_nodes_in_boundary, get_nodes_in_region, get_links_on_region_boundary, calculate_convex_hull, calculate_boundary, inflate_boundary, inflate_region from .s32_region import get_region_schema, get_region, set_region, add_region, delete_region from .s33_dma_cal import PARTITION_TYPE_RB, PARTITION_TYPE_KWAY diff --git a/api/s32_region_util.py b/api/s32_region_util.py index a01fee9..3830776 100644 --- a/api/s32_region_util.py +++ b/api/s32_region_util.py @@ -46,13 +46,62 @@ def get_nodes_in_boundary(name: str, boundary: list[tuple[float, float]]) -> lis return nodes +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 + + +# if region is general or wda => get_nodes_in_boundary +# if region is dma, sa or vd => get stored nodes in table def get_nodes_in_region(name: str, region_id: str) -> list[str]: nodes: list[str] = [] - for row in read_all(name, f"select c.node from coordinates as c, region as r where ST_Intersects(c.coord, r.boundary) and r.id = '{region_id}'"): - nodes.append(row['node']) + + row = try_read(name, f"select r_type from region where id = '{region_id}'") + if row == None: + return nodes + + r_type = str(row['r_type']) + + if r_type == 'DMA' or r_type == 'SA' or r_type == 'VD': + table = '' + if r_type == 'DMA': + table = 'region_dma' + elif r_type == 'SA': + table = 'region_sa' + elif r_type == 'VD': + table = 'region_vd' + + if table != '': + row = try_read(name, f"select nodes from {table} where id = '{region_id}'") + if row != None: + nodes = eval(str(row['nodes'])) + + if nodes == []: + for row in read_all(name, f"select c.node from coordinates as c, region as r where ST_Intersects(c.coord, r.boundary) and r.id = '{region_id}'"): + nodes.append(row['node']) + return nodes +def get_links_on_region_boundary(name: str, region_id: str) -> list[str]: + nodes = get_nodes_in_region(name, region_id) + print(nodes) + return _get_links_on_boundary(name, nodes) + + def calculate_convex_hull(name: str, nodes: list[str]) -> list[tuple[float, float]]: write(name, f'delete from temp_node') for node in nodes: diff --git a/dev.py b/dev.py index 08f18a4..9d4df83 100644 --- a/dev.py +++ b/dev.py @@ -1,11 +1,10 @@ from tjnetwork import * p = 'dev' -read_inp(p, './inp/net3.inp', '3') +read_inp(p, f'./inp/net3.inp', '3') open_project(p) -sass = calculate_service_area(p) -assert len(sass) == 25 -print(sass[0]) -print(sass[1]) \ No newline at end of file +add_region(p, ChangeSet({'id': 'r', 'boundary': [(24.614,13.087), (24.835,11.069), (26.144,10.747), (27.290,11.543), (25.726,12.987), (24.614,13.087)]})) +links = get_links_on_region_boundary(p, 'r') +print(links) \ No newline at end of file diff --git a/test_tjnetwork.py b/test_tjnetwork.py index aa06f6f..b893cdb 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -5680,11 +5680,27 @@ class TestApi: boundary = calculate_boundary(p, vds[0]['nodes']) boundary = inflate_boundary(p, boundary) - add_region(p, ChangeSet({'id': 'r', 'boundary': boundary})) + add_region(p, ChangeSet({'id': 'r1', 'boundary': boundary})) - nodes = get_nodes_in_region(p, 'r') + nodes = get_nodes_in_region(p, 'r1') assert nodes == ['10', '101', '103', '105', '107', '109', '111', '115', '117', '119', '120', '257', '259', '261', '263', 'Lake'] + add_district_metering_area(p, ChangeSet({'id': 'r2', 'boundary': boundary, 'nodes': ['10', '101', '103']})) + nodes = get_nodes_in_region(p, 'r2') + assert nodes == ['10', '101', '103'] + + self.leave(p) + + + def test_get_links_on_boundary(self): + p = 'get_links_on_boundary' + read_inp(p, f'./inp/net3.inp', '3') + open_project(p) + + add_region(p, ChangeSet({'id': 'r', 'boundary': [(24.614,13.087), (24.835,11.069), (26.144,10.747), (27.290,11.543), (25.726,12.987), (24.614,13.087)]})) + links = get_links_on_region_boundary(p, 'r') + assert links == ['183', '185', '229', '313', '315'] + self.leave(p) diff --git a/tjnetwork.py b/tjnetwork.py index 133f0a4..f0430a3 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -984,16 +984,15 @@ def get_all_scada_elements(name: str) -> list[dict[str, Any]]: # general_region 32 ############################################################ -# pure geometry function, test point is in polygon def get_nodes_in_boundary(name: str, boundary: list[tuple[float, float]]) -> list[str]: return api.get_nodes_in_boundary(name, boundary) -# if region is general or wda => get_nodes_in_boundary -# if region is dma, sa or vd => get stored nodes in table -# TODO: more test def get_nodes_in_region(name: str, region_id: str) -> list[str]: return api.get_nodes_in_region(name, region_id) +def get_links_on_region_boundary(name: str, region_id: str) -> list[str]: + return api.get_links_on_region_boundary(name, region_id) + def calculate_convex_hull(name: str, nodes: list[str]) -> list[tuple[float, float]]: return api.calculate_convex_hull(name, nodes)