Skip to content

Commit

Permalink
Add computing of the path to follow (node to node) (handles whole edg…
Browse files Browse the repository at this point in the history
…es with their default direction)
  • Loading branch information
JustineFricou committed Apr 15, 2024
1 parent 57d9769 commit f4dcce2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
3 changes: 0 additions & 3 deletions geotrek/core/static/core/dijkstra.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Geotrek.Dijkstra = (function() {
// Warning - weight is in fact edge.length in our data
l.push({'node_id': node_dest_id, 'weight': graph_edges[edge_id].length});
});
console.log("getPairWeightNode", l)
return l;
}

Expand Down Expand Up @@ -193,7 +192,6 @@ Geotrek.shortestPath = (function() {
* Returns list of paths, and null if not found.
*/
var paths = [];
console.log('tzhrhtrztszth')
for (var j = 0; j < steps.length - 1; j++) {
var path = computeTwoStepsPath(graph, steps[j], steps[j + 1]);
if (!path)
Expand All @@ -219,7 +217,6 @@ Geotrek.shortestPath = (function() {
// path : Array of { start: Node_id, end: Node_id, edge: Edge, weight: Int (edge.length) }
// weight: Int
// }
console.log("AZERTY")
var weighted_path = Geotrek.Dijkstra.get_shortest_path_from_graph(graph, from_nodes, to_nodes);

// restore graph
Expand Down
8 changes: 4 additions & 4 deletions geotrek/core/static/core/multipath.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ L.Handler.MultiPath = L.Handler.extend({
sent_steps.push(sent_step)
})

console.log('computePaths:', 'graph', this.graph, 'steps', sent_steps)
console.log('computePaths:', 'graph', this.graph, 'steps', this.steps)
fetch(window.SETTINGS.urls['trek_geometry'], {
method: 'POST',
headers: {
Expand Down Expand Up @@ -592,9 +592,9 @@ L.Handler.MultiPath = L.Handler.extend({
this.fire('computed_paths', {
'computed_paths': new_computed_paths,
'new_edges': this.all_edges,
'old': old_computed_paths,
'marker_source': this.marker_source,
'marker_dest': this.marker_dest
// 'old': old_computed_paths,
// 'marker_source': this.marker_source,
// 'marker_dest': this.marker_dest
});
},

Expand Down
44 changes: 38 additions & 6 deletions geotrek/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def get_start_and_end_node_ids(self):

path_starting_node = bound_nodes[0][0]
path_ending_node = bound_nodes[-1][-1]
return path_starting_node, path_ending_node
return str(path_starting_node), str(path_ending_node)

Check warning on line 499 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L497-L499

Added lines #L497 - L499 were not covered by tests

def post(self, request):
try:
Expand All @@ -511,15 +511,47 @@ def post(self, request):

cs_graph = self.get_cs_graph()
matrix = csr_matrix(cs_graph)
print("cs_graph:", cs_graph)

Check warning on line 514 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L512-L514

Added lines #L512 - L514 were not covered by tests

start_node, end_node = self.get_start_and_end_node_ids()
print("start_node, end_node", start_node, end_node)
start_node_id, end_node_id = self.get_start_and_end_node_ids()
print("start_node_id, end_node_id", start_node_id, end_node_id)

Check warning on line 517 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L516-L517

Added lines #L516 - L517 were not covered by tests

result = dijkstra(matrix, return_predecessors=True, indices=2,
# Tuples (index, id) for all nodes -> for interpreting the results
self.nodes_idx_per_id = list(enumerate(self.nodes.keys()))
print(self.nodes_idx_per_id)

Check warning on line 521 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L520-L521

Added lines #L520 - L521 were not covered by tests

def get_node_idx_per_id(node_id):
for (index, id) in self.nodes_idx_per_id:
if node_id == id:
return index
return None

Check warning on line 527 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L523-L527

Added lines #L523 - L527 were not covered by tests

def get_node_id_per_idx(node_idx):
for (index, id) in self.nodes_idx_per_id:
if node_idx == index:
return id
return None

Check warning on line 533 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L529-L533

Added lines #L529 - L533 were not covered by tests

start_node_idx = get_node_idx_per_id(start_node_id)
end_node_idx = get_node_idx_per_id(end_node_id)
result = dijkstra(matrix, return_predecessors=True, indices=start_node_idx,

Check warning on line 537 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L535-L537

Added lines #L535 - L537 were not covered by tests
directed=False)
print(result)

Check warning on line 539 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L539

Added line #L539 was not covered by tests

# Retracing the path index by index, from end to start
predecessors = result[1]
current_node_id, current_node_idx = end_node_id, end_node_idx
path = [current_node_id]
while current_node_id != start_node_id:

Check warning on line 545 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L542-L545

Added lines #L542 - L545 were not covered by tests
# print("current_node_id", current_node_id, "current_node_idx", current_node_idx)
# print('start_node_id', start_node_id)
# print(type(current_node_id), type(start_node_id))
current_node_idx = predecessors[current_node_idx]
current_node_id = get_node_id_per_idx(current_node_idx)
path.append(current_node_id)

Check warning on line 551 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L549-L551

Added lines #L549 - L551 were not covered by tests

print(path)

Check warning on line 553 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L553

Added line #L553 was not covered by tests

return JsonResponse({

Check warning on line 555 in geotrek/core/views.py

View check run for this annotation

Codecov / codecov/patch

geotrek/core/views.py#L555

Added line #L555 was not covered by tests
'graph': params['graph'],
'steps': params['steps'],
'path': path,
})

0 comments on commit f4dcce2

Please sign in to comment.