From 34c0e660135ae342c7c9fbebc543f6bb26708c01 Mon Sep 17 00:00:00 2001 From: napakalas Date: Thu, 10 Oct 2024 12:55:15 +1300 Subject: [PATCH] handling possible error due to empty dictionary (#91) --- mapmaker/routing/__init__.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mapmaker/routing/__init__.py b/mapmaker/routing/__init__.py index 307aeb3f..c4af9fe7 100644 --- a/mapmaker/routing/__init__.py +++ b/mapmaker/routing/__init__.py @@ -1039,13 +1039,14 @@ def get_centreline_from_containing_features(start_dict, end_dict, feature_ids: s len(se_dict.get('used', {}) & set(properties['subgraph'].nodes)) == 0: candidates= {(n, s):self.__flatmap.get_feature(n).geometry.centroid.distance(self.__flatmap.get_feature(s).geometry.centroid) for n, s in itertools.product(se_dict.get('used', set()), properties['subgraph'].nodes)} - if len(selected:=min(candidates, key=candidates.get)) == 2: - for n, s in itertools.product(se_dict['node'], used_nodes): - if (n, s) in connectivity_graph.edges: - edge_dict = connectivity_graph.edges[(n, s)] - tmp_edge_dicts[selected] = edge_dict - new_direct_edges.update([selected]) - break + if len(candidates) > 0: + if len(selected:=min(candidates, key=candidates.get)) == 2: + for n, s in itertools.product(se_dict['node'], used_nodes): + if (n, s) in connectivity_graph.edges: + edge_dict = connectivity_graph.edges[(n, s)] + tmp_edge_dicts[selected] = edge_dict + new_direct_edges.update([selected]) + break for ends, list_path_nodes in graph_utils.connected_paths(connectivity_graph).items(): for path_nodes in list_path_nodes: @@ -1075,7 +1076,8 @@ def get_centreline_from_containing_features(start_dict, end_dict, feature_ids: s if (nf:=self.__map_feature(n)) is not None and (sf:=self.__map_feature(s)) is not None: candidates[(n,s)] = nf.geometry.centroid.distance(sf.geometry.centroid) tmp_edge_dicts[(n,s)] = edge_dict - new_direct_edges.update([min(candidates, key=candidates.get)]) # type: ignore + if len(candidates) > 0: + new_direct_edges.update([min(candidates, key=candidates.get)]) # type: ignore elif ((p_dict:=connectivity_graph.nodes[prev_node])['type'] == 'feature' and (n_dict:=connectivity_graph.nodes[node])['type'] == 'feature'): if ((pf:=list(p_dict.get('features'))[0].id) in route_graph.nodes and