diff --git a/api/__init__.py b/api/__init__.py index 5239992..a9088e5 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -145,7 +145,7 @@ from .s32_region import get_region_schema, get_region, set_region, add_region, d from .s32_water_distribution import calculate_demand_to_nodes, calculate_demand_to_region, calculate_demand_to_network from .s33_district_metering_area import PARTITION_TYPE_RB, PARTITION_TYPE_KWAY -from .s33_district_metering_area import calculate_district_metering_area +from .s33_district_metering_area import calculate_district_metering_area_for_nodes, calculate_district_metering_area_for_region, calculate_district_metering_area_for_network from .s34_service_area import calculate_service_area diff --git a/api/s33_district_metering_area.py b/api/s33_district_metering_area.py index d907374..592d5df 100644 --- a/api/s33_district_metering_area.py +++ b/api/s33_district_metering_area.py @@ -1,6 +1,8 @@ import ctypes import os from .database import * +from .s0_base import get_nodes +from .s32_region_util import get_nodes_in_region from .s32_region_util import Topology @@ -8,7 +10,7 @@ PARTITION_TYPE_RB = 0 PARTITION_TYPE_KWAY = 1 -def calculate_district_metering_area(name: str, nodes: list[str], part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: +def calculate_district_metering_area_for_nodes(name: str, nodes: list[str], part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: if part_type != PARTITION_TYPE_RB and part_type != PARTITION_TYPE_KWAY: return [] if part_count <= 0: @@ -83,3 +85,13 @@ def calculate_district_metering_area(name: str, nodes: list[str], part_count: in dmas[c_out_part[i]].append(t_node_list[i]) return dmas + + +def calculate_district_metering_area_for_region(name: str, region: str, part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: + nodes = get_nodes_in_region(name, region) + return calculate_district_metering_area_for_nodes(name, nodes, part_count, part_type) + + +def calculate_district_metering_area_for_network(name: str, part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: + nodes = get_nodes(name) + return calculate_district_metering_area_for_nodes(name, nodes, part_count, part_type) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 66bea40..c2aa259 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -5882,6 +5882,38 @@ class TestApi: self.leave(p) + def test_calculate_district_metering_area_for_region(self): + p = 'test_calculate_district_metering_area_for_region' + read_inp(p, f'./inp/net3.inp', '3') + open_project(p) + + assert get_node_coord(p, '177') == { 'x' : 0.0, 'y' : 0.0 } + + add_region(p, ChangeSet({'id': 'r', 'boundary': [(-10000.0, -10000.0), (10000.0, -10000.0), (10000.0, 10000.0), (-10000.0, 10000.0), (-10000.0, -10000.0)]})) + + nodes = get_nodes_in_region(p, 'r') + assert len(nodes) == 97 + + dmas = calculate_district_metering_area_for_region(p, 'r', 3) + assert dmas == [['10', '60', '601', '61', '101', '103', '105', '107', '109', '111', '113', '115', '117', '119', '120', '121', '123', '125', '145', '147', '149', '151', '153', '157', '159', '197', '257', '259', '261', '263', 'River', 'Lake'], ['35', '40', '161', '163', '164', '166', '167', '169', '171', '173', '179', '181', '183', '184', '185', '187', '189', '191', '193', '195', '199', '201', '203', '204', '205', '207', '265', '267', '269', '271', '273', '275', '1', '177'], ['15', '20', '50', '127', '129', '131', '139', '141', '143', '206', '208', '209', '211', '213', '215', '217', '219', '225', '229', '231', '237', '239', '241', '243', '247', '249', '251', '253', '255', '2', '3']] + + self.leave(p) + + + def test_calculate_district_metering_area_for_network(self): + p = 'test_calculate_district_metering_area_for_region' + read_inp(p, f'./inp/net3.inp', '3') + open_project(p) + + dmas = calculate_district_metering_area_for_network(p, 3) + assert len(dmas) == 3 + assert dmas[0] == ['173', '184', '185', '199', '2', '201', '203', '205', '206', '207', '208', '209', '211', '213', '215', '217', '219', '225', '229', '231', '237', '239', '241', '243', '247', '249', '251', '253', '255', '273', '275', '50'] + assert dmas[1] == ['1', '10', '101', '103', '109', '111', '113', '161', '163', '164', '166', '167', '169', '171', '179', '181', '183', '187', '189', '191', '193', '195', '197', '204', '265', '267', '269', '271', '35', '40', 'Lake'] + assert dmas[2] == ['105', '107', '115', '117', '119', '120', '121', '123', '125', '127', '129', '131', '139', '141', '143', '145', '147', '149', '15', '151', '153', '157', '159', '20', '257', '259', '261', '263', '3', '60', '601', '61', 'River'] + + self.leave(p) + + # 34 service_area diff --git a/tjnetwork.py b/tjnetwork.py index d5361e1..2a26955 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -826,7 +826,7 @@ def set_option_v3(name: str, cs: ChangeSet) -> ChangeSet: # coord 24.[COORDINATES] ############################################################ -def get_node_coord(name: str, node_id: str) -> dict[str, float] | None: +def get_node_coord(name: str, node_id: str) -> dict[str, float]: return api.get_node_coord(name, node_id) @@ -1033,13 +1033,13 @@ def delete_region(name: str, cs: ChangeSet) -> ChangeSet: ############################################################ def calculate_district_metering_area_for_nodes(name: str, nodes: list[str], part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: - return api.calculate_district_metering_area(name, nodes, part_count, part_type) + return api.calculate_district_metering_area_for_nodes(name, nodes, part_count, part_type) def calculate_district_metering_area_for_region(name: str, region: str, part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: - return [] + return api.calculate_district_metering_area_for_region(name, region, part_count, part_type) def calculate_district_metering_area_for_network(name: str, part_count: int = 1, part_type: int = PARTITION_TYPE_RB) -> list[list[str]]: - return [] + return api.calculate_district_metering_area_for_network(name, part_count, part_type) def get_district_metering_area_schema(name: str) -> dict[str, dict[str, Any]]: return {}#api.get_district_metering_area_schema(name)