Skip to content

Commit

Permalink
Add handling of an impossible path: the view returns a status 204 and…
Browse files Browse the repository at this point in the history
… the js does not attempt to display the path
  • Loading branch information
JustineFricou committed May 6, 2024
1 parent 5c92de1 commit ac82486
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
9 changes: 9 additions & 0 deletions geotrek/core/path_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def get_edge_weight(self, edge_id):
def get_route(self, steps):
self.steps = steps
line_strings = self.compute_list_of_paths()
if line_strings is None:
return None

multi_line_string = GeometryCollection(line_strings, srid=settings.SRID)
multi_line_string.transform(settings.API_SRID)
Expand All @@ -164,6 +166,8 @@ def compute_list_of_paths(self):
from_step = self.steps[i]
to_step = self.steps[i + 1]
line_strings = self.compute_two_steps_line_strings(from_step, to_step)
if line_strings is None:
return None
merged_line_string = self.merge_line_strings(line_strings)
all_line_strings.append(merged_line_string)
return all_line_strings
Expand All @@ -174,6 +178,8 @@ def compute_two_steps_line_strings(self, from_step, to_step):

shortest_path = self.get_shortest_path(from_node_info['node_id'],
to_node_info['node_id'])
if shortest_path is None:
return None
line_strings = self.node_list_to_line_strings(shortest_path,
from_node_info, to_node_info)

Expand Down Expand Up @@ -409,6 +415,9 @@ def get_node_id_per_idx(node_idx):
current_node_id, current_node_idx = to_node_id, to_node_idx
path = [current_node_id]
while current_node_id != from_node_id:
if current_node_idx < 0:
# The path ends here but this node is not the destination
return None
current_node_idx = predecessors[current_node_idx]
current_node_id = get_node_id_per_idx(current_node_idx)
path.append(current_node_id)
Expand Down
14 changes: 10 additions & 4 deletions geotrek/core/static/core/multipath.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,17 @@ L.Handler.MultiPath = L.Handler.extend({
steps: sent_steps,
})
})
.then(response => response.json())
.then(response => {
// console.log("response", response)
if (response.status == 200)
return response.json()
})
.then(data => {
console.log('response:', data)
var route = {'geojson': data}
this.fire('fetched_route', route);
// console.log('response data:', data)
if (data) {
var route = {'geojson': data}
this.fire('fetched_route', route);
}
})
// .catch(e => {
// console.log("fetchRoute", e)
Expand Down
4 changes: 3 additions & 1 deletion geotrek/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def route_geometry(self, request, *args, **kwargs):

try:
path_router = PathRouter()
response, status = path_router.get_route(steps), 200
response = path_router.get_route(steps)
status = 200 if response is not None else 204

except Exception as exc:
response, status = {'error': '%s' % exc, }, 500
return Response(response, status)
Expand Down

0 comments on commit ac82486

Please sign in to comment.