-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtools.py
289 lines (199 loc) · 8.72 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import networkx as nx
import numpy as np
from itertools import combinations
from network import Network
from heapq import heappop, heapify
def get_odd_degree_nodes(graph):
""" Finds all odd degree nodes.
Args:
- graph (networkx.Graph): undirected graph
Returns:
- (list): list of odd degree nodes
"""
return [v for v, d in graph.degree() if d % 2 == 1]
def map_osmnx_nodes2integers(graph):
""" Maps osmnx node ids to integers in range (0, node_count)
Args:
- graph (networkx.Graph): graph
Returns
- node_count (integer): number of nodes
- nodes_list (list): list of all nodes converted to integers in range (0, node_count)
"""
nodes_list = list(graph.nodes)
nodes_list.sort()
node_count = len(nodes_list)
return node_count, nodes_list
def map_osmnx_edges2integers(graph, edges):
""" Maps edges with osmnx ids to to integers in range (0, node_count)
Args:
- graph (networkx.Graph): graph
- edges (list or networkx.EdgeView): list of osmnx edges
Returns
- converted_edges (list): list of all edges with converted nodes to integers in range(0, node_count)
"""
n, nodes_list = map_osmnx_nodes2integers(graph)
converted_edges = []
for v, u in edges:
# TODO: is .index() the best option, what about using dict()
converted_edges.append((nodes_list.index(v), nodes_list.index(u)))
return converted_edges
def create_weighted_complete_graph(pair_weights):
""" Creates a weighted complete graph with [inverse] negative weights given list of weighted edges.
(Negative weights are due to using maximal matching algorithm instead of minimal matching algorithm)
Args:
- pair_weights (list): list of 3-tuples denoting an edge with weight
Returns:
- graph (networkx.Graph): complete graph
"""
graph = nx.Graph()
graph.add_weighted_edges_from([(e[0], e[1], - np.round(w, 2)) for e, w in pair_weights.items()])
return graph
def min_matching(pair_weights):
""" From weighted edges list forms a complete graph and finds a minimal matching [1]
Args:
- pair_weights (list): list of 3-tuples denoting an edge with weight
Returns:
- matching_weights (list): list of triples denoting an edge with weight
Sources:
.. [1] Galil, Z. (1986). Efficient algorithms for finding maximum matching in graphs. ACM Comput. Surv., 18, 23-38.
https://www.semanticscholar.org/paper/Efficient-algorithms-for-finding-maximum-matching-Galil/ef1b31b4728615a52e3b8084379a4897b8e526ea?p2df
"""
complete_graph = create_weighted_complete_graph(pair_weights)
matching = nx.algorithms.max_weight_matching(complete_graph, True)
matching_weights = []
for v, u in matching:
if (v, u) in pair_weights:
matching_weights.append((v, u, np.round(pair_weights[v, u], 2)))
else:
matching_weights.append((v, u, np.round(pair_weights[u, v], 2)))
return matching_weights
def get_shortest_distance_for_odd_degrees(graph, odd_degree_nodes):
""" Finds shortest distance for all odd degree nodes combinations
Args:
- graph (networkx.Graph): undirected graph
- odd_degree_nodes (list): list of odd degree nodes
Returns:
- (dict): a dict with shortest distances for each combination of nodes
"""
pairs = combinations(odd_degree_nodes, 2)
return {(v, u): nx.dijkstra_path_length(graph, v, u, weight="length") for v, u in pairs}
def get_shortest_paths(graph, nodes):
""" Creates a list of shortest paths between two [not neighboring] nodes.
Args:
- graph (networkx.Graph): undirected graph
- nodes (list): list of tuples (start, end, weight) for which we find a shortest path
Return:
- shortest_paths (list): list of shortest paths between two [not neighboring] nodes
"""
shortest_paths = []
for u, v, _ in nodes:
path = nx.dijkstra_path(graph, u, v)
shortest_paths.extend([(path[i - 1], path[i]) for i in range(1, len(path))])
return shortest_paths
def networkx2network(graph):
""" Converts networkx.Graph() into Network
Args:
graph (Graph): a Network object
Returns:
A Network object.
"""
new_graph = nx.Graph()
nodes = list(graph.nodes())
nodes.sort()
edges = [(nodes.index(v), nodes.index(u), np.round(d["length"], 2)) for (v, u, d) in graph.edges(data=True) if "length" in d] # noqa
new_graph.add_weighted_edges_from(edges)
n = len(nodes)
return Network(n, edges, directed=False, weighted=True)
def get_double_edge_heap(graph):
""" Creates a heap for multi-edges sorted [asc] with respect to weight
Args:
- graph (networkx.Graph): undirected graph
Returns:
- double_edge_heap (heapify dict): heap of multi-edges sorted [asc] with respect to weight
"""
double_edge_heap = {}
for v, u, i in graph.edges:
# There are multiple option for the same edge, not recorded yet
if i > 0 and (u, v) not in double_edge_heap and (v, u) not in double_edge_heap:
data = graph.get_edge_data(v, u)
double_edge_heap[v, u] = []
for k, d in data.items():
double_edge_heap[v, u].append((np.round(d["length"], 2), k))
heapify(double_edge_heap[v, u])
return double_edge_heap
def convert_path(graph, path, double_edge_heap):
""" Converts path with respect to multi-edges. It makes sure that each edge in multi-edge set is visited.
If an edge needs to be visited more than number of multi-edges, the shortest one is always selected.
Args:
- graph (networkx.Graph): undirected graph
- path (list): list of [edges] tuples that form a final path
- double_edge_heap (heapify dict): heap of multi-edges sorted [asc] with respect to weight
Returns:
- path_edge_list (list):
"""
double_edges = double_edge_heap.keys()
path_edge_list = []
perv_node = path.pop()
def pop_double_edge(deh, e):
if len(deh[e]) > 1:
return heappop(deh[e])
else:
return deh[e][0]
while len(path) > 0:
current_node = path.pop()
# An edge is a self loop, if self does not exist in the [org] graph then remove from the list
if current_node == perv_node and not graph.get_edge_data(current_node, perv_node):
current_node = path.pop()
edge = (perv_node, current_node)
# Double edge
if edge in double_edges:
_, i = pop_double_edge(double_edge_heap, edge)
elif edge[::-1] in double_edges:
_, i = pop_double_edge(double_edge_heap, edge[::-1])
else:
i = 0
path_edge_list.append((perv_node, current_node, i))
perv_node = current_node
return path_edge_list
def convert_integer_path2osmnx_nodes(path, osmnx_nodes):
converted_path = []
if max(path) > len(osmnx_nodes):
return converted_path
osmnx_nodes = list(osmnx_nodes)
osmnx_nodes.sort()
for p in path:
converted_path.append(osmnx_nodes[p])
return converted_path
def convert_final_path_to_coordinates(org_graph, final_path):
""" Converts final path of [osmnx] edges into list of (lat, log) tuples which are then read by Leaflet JS library.
Args:
- org_graph (networkx.Graph): directed [original] graph
- final_path (list): list of [edges] tuples that form a final path
Returns:
- path (list): list of (lat, log) tuples
"""
path = []
for (u, v, i) in final_path:
# Edge does not exist in org_graph, revert the coordinates
# (unfortunately, there is a bug in osmnx with using undirected graph,
# ~5% of one way streets have incorrect coords)
if not org_graph.get_edge_data(u, v) or i not in org_graph.get_edge_data(u, v):
inverse_edge = org_graph.get_edge_data(v, u)
if inverse_edge and i in inverse_edge and "geometry" in inverse_edge[i]:
coords = list(inverse_edge[i]["geometry"].coords)[::-1]
for (x, y) in coords:
path.append([y, x])
continue
# Edge does not have `geometry` field, return origin and end coordinates
if "geometry" not in org_graph.get_edge_data(u, v)[i]:
x1 = org_graph.nodes[u]["x"]
x2 = org_graph.nodes[v]["x"]
y1 = org_graph.nodes[u]["y"]
y2 = org_graph.nodes[v]["y"]
path.extend([[y1, x1], [y2, x2]])
continue
# Edge does exists and has geometry and was hit in the right direction
coords = list(org_graph.get_edge_data(u, v)[i]["geometry"].coords)
for (x, y) in coords:
path.append([y, x])
return path