From bf118ae49df58c2edb3dd1af6096df1a7713e690 Mon Sep 17 00:00:00 2001 From: napakalas Date: Thu, 21 Aug 2025 15:36:34 +1200 Subject: [PATCH 1/3] Update min() usage with explicit key function to prevent linter warnings (#154). --- mapmaker/routing/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mapmaker/routing/__init__.py b/mapmaker/routing/__init__.py index 7e689db1..afdb27c4 100644 --- a/mapmaker/routing/__init__.py +++ b/mapmaker/routing/__init__.py @@ -986,7 +986,7 @@ def add_route_edges_from_graph(G, used_nodes): candidates[(n,s)] = nf.geometry.centroid.distance(sf.geometry.centroid) tmp_edge_dicts[(n,s)] = edge_dict if len(candidates) > 0: - selected_c = min(candidates, key=candidates.get) # type: ignore + selected_c = min(candidates, key=lambda k: candidates[k]) neighbouring_ids.update([selected_c[0]]) closest_feature_dict[selected_c[0]] = selected_c[1] for n_id in neighbouring_ids: @@ -1070,7 +1070,7 @@ def get_centreline_from_containing_features(start_dict, end_dict, feature_ids: s 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(candidates) > 0: - if len(selected:=min(candidates, key=candidates.get)) == 2: + if len(selected:=min(candidates, key=lambda k: candidates[k])) == 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)] @@ -1107,7 +1107,7 @@ def get_centreline_from_containing_features(start_dict, end_dict, feature_ids: s candidates[(n,s)] = nf.geometry.centroid.distance(sf.geometry.centroid) tmp_edge_dicts[(n,s)] = edge_dict if len(candidates) > 0: - new_direct_edges.update([min(candidates, key=candidates.get)]) # type: ignore + new_direct_edges.update([min(candidates, key=lambda k: candidates[k])]) 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 @@ -1173,7 +1173,7 @@ def get_node_feature(node_dict, neighbour_features, used_features) -> Feature: for nf in neighbour_features: distances += [nf.geometry.centroid.distance(f.geometry.centroid)] feature_distances[f] = sum(distances)/len(distances) - selected_feature = min(feature_distances, key=feature_distances.get) # type: ignore + selected_feature = min(feature_distances, key=lambda k: feature_distances[k]) else: prev_features = [feature for features in used_features.values() for feature in features] if len(prev_features) > 0 and len(features) > 1: From bf1240ced8b45e24870ac1ad7142209e488eb3a7 Mon Sep 17 00:00:00 2001 From: napakalas Date: Thu, 21 Aug 2025 15:40:25 +1200 Subject: [PATCH 2/3] Check if self.__flatmap.get_feature() returns None (#154). --- mapmaker/routing/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mapmaker/routing/__init__.py b/mapmaker/routing/__init__.py index afdb27c4..38d060e7 100644 --- a/mapmaker/routing/__init__.py +++ b/mapmaker/routing/__init__.py @@ -1067,8 +1067,12 @@ def get_centreline_from_containing_features(start_dict, end_dict, feature_ids: s if se_dict.get('type') == 'segment' and \ len(se_dict.get('used', {})) > 0 and \ 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)} + candidates = { + (n, s): f_n.geometry.centroid.distance(f_s.geometry.centroid) + for n, s in itertools.product(se_dict.get('used', set()), properties['subgraph'].nodes) + if (f_n := self.__flatmap.get_feature(n)) is not None + and (f_s := self.__flatmap.get_feature(s)) is not None + } if len(candidates) > 0: if len(selected:=min(candidates, key=lambda k: candidates[k])) == 2: for n, s in itertools.product([se_dict['node']], used_nodes): From e418ddd55c87963103ec02e78a3ea09920fc3bf6 Mon Sep 17 00:00:00 2001 From: napakalas Date: Thu, 21 Aug 2025 15:42:25 +1200 Subject: [PATCH 3/3] Remove redundant length check (#154). --- mapmaker/routing/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mapmaker/routing/__init__.py b/mapmaker/routing/__init__.py index 38d060e7..93938579 100644 --- a/mapmaker/routing/__init__.py +++ b/mapmaker/routing/__init__.py @@ -1073,14 +1073,14 @@ def get_centreline_from_containing_features(start_dict, end_dict, feature_ids: s if (f_n := self.__flatmap.get_feature(n)) is not None and (f_s := self.__flatmap.get_feature(s)) is not None } - if len(candidates) > 0: - if len(selected:=min(candidates, key=lambda k: candidates[k])) == 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 candidates: + selected = min(candidates, key=lambda k: candidates[k]) + 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: