Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pgRouting A star #4263

Draft
wants to merge 216 commits into
base: move_routing_to_backend
Choose a base branch
from
Draft
Changes from all commits
Commits
Show all changes
216 commits
Select commit Hold shift + click to select a range
97b3ab4
Add TrekGeometry view with get and post methods
Apr 10, 2024
58766b1
Add a parameter to TrekGeometry post method
Apr 10, 2024
74f0477
Add graph and steps info being sent to TrekGeometry view
Apr 12, 2024
6e16158
Add computing of the dijkstra matrix and start and end nodes (only ha…
Apr 12, 2024
b200b87
Add computing of the path to follow (node to node) (handles whole edg…
Apr 15, 2024
c0e554c
Modify path in response: it's now from start to end
Apr 15, 2024
f356ab9
Refacto: nodes idx to id correspondence
Apr 15, 2024
5feecc1
Add structure of algorithm handling partial edges and more than 2 steps
Apr 15, 2024
113995a
ADD handling of edge percentage and more than 2 markers
Apr 16, 2024
165d6ed
Add display of a hard-coded polyline sent from the TrekGeometry view
Apr 16, 2024
188d969
The graph is directly taken from the backend
Apr 16, 2024
253ef5b
Add handling of GPS coordinates as input
Apr 17, 2024
5c31da3
Add use of Point ewkt when using sql
Apr 17, 2024
4305b0f
Add TODOs for next step: obtaining a LineString from dijkstra results
Apr 17, 2024
20ab44e
Starting to work on converting the resulting nodes list into LineStrings
Apr 17, 2024
f6fd6c0
Add conversion of dijstra results into a displayed Linestring
Apr 18, 2024
e661685
Correctng some comments
Apr 23, 2024
4ad3bd9
Fix path not computed if adjacent steps are on the same edge
Apr 23, 2024
c229a6c
Fix negative dijkstra weight when we're going backwards relative to a…
Apr 23, 2024
0ced117
Fix path not computed when going backwards on a Path if going through…
Apr 23, 2024
7fc0002
Starting to display fetched route instead of locally computed route
Apr 25, 2024
0c7ea24
Preparing topology_helper to handle several linestrings
Apr 25, 2024
c5eea3b
Automating the geojson generation in the view
Apr 25, 2024
93cfd65
Add handling of several steps
Apr 26, 2024
a03d6f7
Replacing MultiLineString by GeometryCollection in Response
Apr 26, 2024
7b8a371
Fixing the organization of layers in the displayed path
Apr 26, 2024
89d842c
Removing old code related to the graph
Apr 26, 2024
b9e43e3
Removing old code
Apr 26, 2024
6a877a7
Removing old code
Apr 26, 2024
3ddb077
Making the TrekGeometry post method into a PathViewSet action
Apr 29, 2024
366622b
Reordering PathRouter methods
Apr 29, 2024
b4de5d8
Removing graph action of PathViewSet
Apr 29, 2024
02cb662
Moving initial graph generation into the PathRouter class
Apr 29, 2024
25bd112
Removing no longer used imports
Apr 29, 2024
51904ff
Renaming graph.py to path_router.py
Apr 29, 2024
b68bfc0
Adding parameters error handling for the route_geometry action
Apr 29, 2024
34f7b39
Using dict.update instead of a custom method
Apr 29, 2024
7853a83
Adding status codes to Responses
Apr 30, 2024
a4c53cd
Optimizing the cs_graph generation (generating the upper triangle the…
May 2, 2024
42ba9b9
Optimizing matrix generation: it is now generated at init and modifie…
May 2, 2024
e2f3a69
Optimizing matrix generation by using its symmetrical property
May 2, 2024
a884ab8
Refactoring: making duplicate code into a method
May 3, 2024
c412222
The route is now fetched only when the marker dragging stops
May 3, 2024
21f0154
Saving the dijkstra matrix in the cache
May 6, 2024
28c74fb
Saving the path graph in the cache
May 6, 2024
f8dd461
Moving duplicate cache-related code into a single method
May 6, 2024
7050cc7
Add handling of an impossible path: the view returns a status 204 and…
May 6, 2024
3fdbfe7
Modify error handling in js: using Promise.reject
May 6, 2024
8417b30
Correct returns when no path could be found
May 13, 2024
57f062c
Replaces status 204 by status 400 when no path can be found
May 13, 2024
eced2d0
Add a spinner when the route is being fetched
May 14, 2024
b07dec2
Add sending of the modified steps indexes to fetchRoute
May 14, 2024
6c899a0
Adds fetching of the route for only the modified steps
May 14, 2024
d946de2
Add algorithm that updates the displayed route layers
May 16, 2024
4dbb444
Add use of currently displayed layers indexes to replace them
May 16, 2024
466a9dd
Fix replacing the currently displayed layers indexes by new ones
May 16, 2024
116ef08
Fix updating layers not working after removing a marker
May 16, 2024
6ab8848
Bump leaflet.textpath.js to v1.1.0
May 17, 2024
25dda92
Move leaflet static files into a vendor directory
May 17, 2024
527ce43
Bump leaflet.lineextremities.js to v0.1.1
May 21, 2024
fd7b1df
Fix missing path layer when an unlinkable via marker is removed
May 21, 2024
b83fb9f
Remove console logs
May 21, 2024
393468c
Add coloring of the markers when one is isolated
May 22, 2024
fae8b3c
Add coloring of all markers to normal appearance when back to normal
May 22, 2024
8c9330c
Add enabling/disabling of markers drag when a route is correct/incorrect
May 22, 2024
98b5b9a
Add enabling/disabling of markers deletion when a route is correct/in…
May 22, 2024
8fd0b21
Add impossibility of creating new via-steps while the route is invalid
May 23, 2024
db0e952
Move spinner.stop() into a finally()
May 23, 2024
60fddbc
Fix one route layer missing when an isolated via-step is corrected
May 24, 2024
b665dea
Add deletion of a marker if not dropped on a path
May 24, 2024
1b3d7f2
Add resetting of a marker to its previous valid position when not dro…
May 24, 2024
681b235
Fix isolated marker highlight being removed when reset to its previou…
May 24, 2024
c59ddaa
Remove duplicate part of code
May 24, 2024
553f119
Remove hiding of route layer when the start or end marker is moved
May 24, 2024
19d100d
Fix out of date step idx used when deleting a via point
May 24, 2024
c17e95e
Add display of an error toast when a marker is incorrect
May 27, 2024
c487a9d
Modify error_toast elements class names for genericity
May 30, 2024
228c800
Fix toast stacking: top toast not on the bottom when it's the only on…
May 30, 2024
4937c33
Fix wrong marker being deleted after trying to create an unsnapped ma…
May 30, 2024
0e8db25
Fix route layer not displayed after start and end markers created and…
May 30, 2024
9565ec3
Add disabling of markers when the route is being fetched
May 30, 2024
ea77759
Add saving of the route topology through the form
May 30, 2024
3406a42
Add modification of the route topology through the edit form
Jun 3, 2024
46d9456
Fix Geotrek object no longer created before use
justinefricou Jun 10, 2024
e4fab92
Remove use of non-sparse matrix for dijkstra (using csr_array)
justinefricou Jun 10, 2024
e5b13b0
Modify use of sparse array (using lil_array instead of csr_array)
justinefricou Jun 11, 2024
b23607d
Optimize dijkstra matrix generation
justinefricou Jun 12, 2024
097d816
Remove calls to now deleted js scripts
justinefricou Jun 12, 2024
1eb6835
Remove no longer used methods
justinefricou Jun 12, 2024
bd5bedc
Add benchmarking system based on frontend routing version
justinefricou Jul 1, 2024
3c73576
Fix typo in cypress.config.js
justinefricou Jul 1, 2024
e2f2d3b
Startng to work on adapting frontend benchmark system for backend
justinefricou Jul 1, 2024
1d9cb0a
Add record of the number of runs for a benchmark measure
justinefricou Jul 2, 2024
e74231e
Modify route control clicking via cypress
justinefricou Jul 2, 2024
327d50a
Replace broken 3 via-pts topology with a 2 via-pts topology
justinefricou Jul 2, 2024
ee34924
Modify cypress benchmark: adapt the frontend version to work with bac…
justinefricou Jul 2, 2024
7db90b4
Modify python time measures: now in milliseconds instead of seconds
justinefricou Jul 2, 2024
b100c13
Modify time measurement: startTime is now recorded before the click/d…
justinefricou Jul 2, 2024
f229b03
Modify benchmark.sh: making the session id a variable
justinefricou Jul 2, 2024
8c22826
Add scenario using the big DB and no via point
justinefricou Jul 2, 2024
d5c4f55
Add display of current branch name for each measure
justinefricou Jul 3, 2024
7cb1b15
Replace big database no via pts scenario with 25 via pts scenario
justinefricou Jul 4, 2024
2af15c4
Add readme for the benchmarking system
justinefricou Jul 4, 2024
71d150b
Modify benchmarking script: cypress spec file and session id are now …
justinefricou Jul 4, 2024
9647975
Fix crash when computing time averages if a file is missing
justinefricou Jul 4, 2024
dd0b986
Add 2 via-pts topology for big database
justinefricou Jul 5, 2024
e8f2113
Replace 25-via-pts scenario with 2-via-pts scenario for big database
justinefricou Jul 5, 2024
bd6d540
Add unit tests for route_geometry view (check of request body)
justinefricou Jul 5, 2024
1154c31
Modify cypress timeouts: now 30min
justinefricou Jul 5, 2024
bed3cc3
Start to work on impossible and possible path test cases
justinefricou Jul 5, 2024
731d9ef
Add check of lat and lng type and range in route_geometry view
justinefricou Jul 5, 2024
73eb941
Fix request body not sent in the right format (route_geometry tests)
justinefricou Jul 5, 2024
b5fbd57
Add new scenario for big DB with no via point
justinefricou Jul 8, 2024
c3111d2
Add test case: steps not on paths
justinefricou Jul 8, 2024
a90349e
Add test case: steps one paths not linked
justinefricou Jul 8, 2024
007be92
Add tests for route_geometry view
justinefricou Jul 8, 2024
04a11b9
Fix test for the route_geometry view
justinefricou Jul 8, 2024
684e792
Remove prints
justinefricou Jul 8, 2024
4e06386
Move benchmarking directory into tools/
justinefricou Jul 8, 2024
9dbea64
Fix norm
justinefricou Jul 8, 2024
5c4edd8
Modify Cypress 'create trek' test after routing changes
justinefricou Jul 8, 2024
e8ac450
Try to fix cypress e2e tests
justinefricou Jul 9, 2024
2ab0f44
Uncomment env_dev settings
justinefricou Jul 9, 2024
cc289a8
Modify Cypress e2e tests: actually click on paths to create a trek
justinefricou Jul 9, 2024
4c0aa0a
Remove console.log and debugger
justinefricou Jul 9, 2024
24cefea
Fix cypress e2e tests: id of clicked path
justinefricou Jul 9, 2024
9511397
Add test case for route_geometry view: error 500
justinefricou Jul 9, 2024
b155ffa
Try to fix cypress tests: ensure the topology control is enabled befo…
justinefricou Jul 9, 2024
3f0d2be
Comment content of test_graph temporarily
justinefricou Jul 9, 2024
d5c465e
Adapt graph generation tests to the new routing system
justinefricou Jul 10, 2024
1602df2
Add tests for path_router
justinefricou Jul 10, 2024
8a265cb
Add unit test for cache handling in path_router
justinefricou Jul 10, 2024
e3dfe44
Fix cache-related test for PathRouter
justinefricou Jul 10, 2024
2d44d79
Modify test_route_geometry_not_fail_with_via_points_several_paths to …
justinefricou Jul 11, 2024
0608c86
Modify route_geometry tests: more precision when checking results
justinefricou Jul 11, 2024
2561e14
Remove old unit test for route_geometry view
justinefricou Jul 11, 2024
01c9ed5
Remove redundant unit test for path_router
justinefricou Jul 12, 2024
ab8cc8c
Add script to take backend time measures
justinefricou Jul 12, 2024
13e2a58
Add steps for route with 100 via-pts for backend time measures script
justinefricou Jul 12, 2024
63a3ed2
Fix topology used in 100-via-pts fixture
justinefricou Jul 15, 2024
23579f0
Add big database topology in backend time measuring script
justinefricou Jul 15, 2024
4749253
Improve readability of path_router test: using yied
justinefricou Jul 15, 2024
f3bd738
Update benchmark README to include the new script
justinefricou Jul 15, 2024
faac013
Fix requirements.txt after rebase
justinefricou Jul 23, 2024
f8241b1
Modify leaflet map: start spinner and disable markers ASAP
justinefricou Jul 30, 2024
6ffb596
Modify leaflet map: stop spinner and reenable markers as late as poss…
justinefricou Jul 30, 2024
79197d1
Adapt backend benchmark script to new PR
justinefricou Aug 5, 2024
e9d6e32
Modify display of route layers: only redisplay those affected by a ro…
justinefricou Aug 14, 2024
635a7b5
Fix route modif not saved if an incorrect marker has been fixed
justinefricou Aug 27, 2024
42e23bc
Fix path layers not displayed at creation
justinefricou Aug 27, 2024
0789f4d
Add computing of the dijkstra matrix and start and end nodes (only ha…
Apr 12, 2024
96def0c
Modify docker-compose to use pgrouting image
justinefricou Jul 18, 2024
06fac0d
Add routing with pgRouting for hard-coded topology
justinefricou Jul 18, 2024
143eab5
Add use of request latlngs for pgRouting
justinefricou Jul 18, 2024
8d9d721
Generate linestrings for routes with no via-pts
justinefricou Jul 19, 2024
c0b24be
Add display of the route (geometry is wrong on last edge)
justinefricou Jul 19, 2024
99ad8b0
Add generation of the serialized topology
justinefricou Jul 19, 2024
c8e8dac
Remove old dijkstra-related methods
justinefricou Jul 19, 2024
85d4cf4
Remove old dijkstra-related imports
justinefricou Jul 19, 2024
bd057b9
Fix route always containing the whole last path instead of a fraction…
justinefricou Jul 22, 2024
babafb1
Replace call to ST_Linesubstring by ST_SmartLineSubstring to split st…
justinefricou Jul 22, 2024
022a25a
Fix route geometry reversed when a path is taken from its end to its …
justinefricou Jul 22, 2024
015062a
Remove LineString rounding before merging
justinefricou Jul 22, 2024
7f9a26e
Fix route topology not correct when a path is taken in reverse
justinefricou Jul 22, 2024
86c7541
Remove prints
justinefricou Jul 22, 2024
914506a
Uncomment try except
justinefricou Jul 22, 2024
8dad54e
Add source and target columns to core_path table for pgRouting
justinefricou Jul 23, 2024
8a69c49
Add error handling when no path is found
justinefricou Jul 23, 2024
f76dbf1
Remove out of date unit tests
justinefricou Jul 23, 2024
946d419
Add use of settings PATH_SNAPPING_DISTANCE for pgRouting tolerance
justinefricou Jul 23, 2024
20f5201
Add extensions required for database
justinefricou Jul 23, 2024
e0692e6
Modify database extensions
justinefricou Jul 23, 2024
cff4874
Remove unused imports after rebase
justinefricou Jul 23, 2024
f5a3e86
Fix postgres images for github CI
justinefricou Jul 24, 2024
0adc513
Add setting of source and target to null for a path that is being split
justinefricou Jul 25, 2024
d6be26d
Modify routing: don't try to route on draft paths
justinefricou Jul 25, 2024
3e0538e
Fix routing taking too much time (unnecessary recalculation of graph)
justinefricou Jul 25, 2024
233194e
Move setting of pgRouting-related values to null from specific case t…
justinefricou Jul 25, 2024
d32ccf0
Fix PathViewsTest test_route_geometry_not_fail_no_via_point_one_path
justinefricou Jul 26, 2024
450f829
Fix no route found when a marker is on a path's extremity (0 or 1)
justinefricou Jul 26, 2024
8939a96
Fix verification of serialized positions in tests (wrong format)
justinefricou Jul 26, 2024
f7304d5
Modify how the closest path is found: get it from the front rather th…
justinefricou Jul 26, 2024
ef7ac47
Fix routing tests: create a path before sending the request with a pa…
justinefricou Jul 26, 2024
7734674
Fix norm
justinefricou Jul 26, 2024
5e62125
Delete now empty test_path_router.py
justinefricou Jul 26, 2024
5ce3928
Modify routing conditions: don't route on paths whose visiblity is se…
justinefricou Jul 26, 2024
5417fb1
Remove modification of geom_3d as a condition for setting pgRouting-r…
justinefricou Jul 26, 2024
12c3d79
Fix incorrect path id test (wrong lng used)
justinefricou Jul 29, 2024
453a5ba
Add checking of error messages for route_geometry unit tests
justinefricou Jul 29, 2024
d153ceb
Add more detailed check of response values for route_geometry tests
justinefricou Jul 29, 2024
ff97465
Add test cases for route_geometry: draft and invisible paths, editing…
justinefricou Jul 29, 2024
7637b30
Add django command to generate pgRouting network topology
justinefricou Jul 30, 2024
81bf2e0
Add unit test for command generate_pgr_network_topology
justinefricou Jul 31, 2024
e74d36c
Add entry in doc explaining how to use the generate_pgr_network_topol…
justinefricou Jul 31, 2024
00be0ac
Move tests docstrings descriptions to top
justinefricou Aug 2, 2024
eacfb77
Add docstrings to path_router
justinefricou Aug 2, 2024
08359c5
Adapt backend benchmark script for new PR
justinefricou Aug 5, 2024
0c736f5
manage pgrouting in nstall scripts
submarcos Sep 3, 2024
f79d993
enable pgrouting extension in debian postinst
submarcos Sep 3, 2024
473f1d3
Update benchmark system for pgRouting
justinefricou Sep 3, 2024
d0ed7f4
fix tests and viewset permission
submarcos Sep 4, 2024
e87edeb
prevent flaky test
submarcos Sep 4, 2024
9972711
Merge branch 'move_routing_to_backend' into use_pgRouting
submarcos Sep 4, 2024
6ef6802
Update docs/install/import.rst
submarcos Sep 4, 2024
aa6cb6a
fix rebase
submarcos Sep 4, 2024
72e7eb2
fix rebase
submarcos Sep 4, 2024
eaa86bc
Modify routing alorithm: use pgr A*
justinefricou Aug 9, 2024
05c4b09
Fix route topology extremities taking the whole edge
justinefricou Aug 9, 2024
2cf3124
Fix direction of route topology and geometry
justinefricou Aug 9, 2024
316a3f8
Fix generation of topology (start and end fractions)
justinefricou Aug 12, 2024
c907b18
Fix computation of more than 2 steps not possible: temp tables not dr…
justinefricou Aug 12, 2024
b1721a7
Fix multilistrings sometimes not merging
justinefricou Aug 12, 2024
0c50091
Remove comments and prints
justinefricou Aug 12, 2024
2cdabcc
Improve precision of coordinates rounding
justinefricou Aug 12, 2024
16dc179
Merge branch 'move_routing_to_backend' into use_pgRouting_A_star
submarcos Sep 13, 2024
d39e682
Merge branch 'move_routing_to_backend' into use_pgRouting_A_star
submarcos Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 133 additions & 143 deletions geotrek/core/path_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,168 +128,158 @@ def compute_two_steps_route(self, from_step, to_step):
from_step: {edge_id: int, fraction: float}
to_step: {edge_id: int, fraction: float}
"""
start_edge = from_step.get('edge_id')
end_edge = to_step.get('edge_id')
fraction_start = self._fix_fraction(from_step.get('fraction'))
fraction_end = self._fix_fraction(to_step.get('fraction'))

query = """
WITH points as (
-- This is a virtual table of the points (start and end)
-- and their position on the closest edge
DO $$
DECLARE
max_edge_id integer;
max_vertex_id integer;

BEGIN

SELECT MAX(id) FROM core_path INTO max_edge_id;
SELECT MAX(vid) FROM (
SELECT source AS vid FROM core_path
UNION
SELECT target AS vid FROM core_path
) AS vids INTO max_vertex_id;

DROP TABLE IF EXISTS temporary_edges_info;
CREATE TEMPORARY TABLE temporary_edges_info AS
-- This info will be added to the A* inner query edges_sql.
-- It represents the temporary edges created by adding the start
-- and end steps as nodes onto the graph (aka pgr network topology)
WITH graph_temporary_edges AS (
SELECT
core_path.id AS path_id,
max_edge_id + index AS edge_id,
ST_SmartLineSubstring(
core_path.geom,
fraction_start,
fraction_end
) as edge_geom,
CASE
WHEN (index % 2) != 0
THEN core_path.source
ELSE max_vertex_id + (index > 2)::int + 1
END AS source_id,
CASE
WHEN (index % 2) = 0
THEN core_path.target
ELSE max_vertex_id + (index > 2)::int + 1
END AS target_id,
CASE
WHEN (index % 2) != 0
THEN ST_StartPoint(geom)
ELSE ST_LineInterpolatePoint(core_path.geom, fraction_start)
END AS source_geom,
CASE
WHEN (index % 2) = 0
THEN ST_EndPoint(geom)
ELSE ST_LineInterpolatePoint(core_path.geom, fraction_end)
END AS target_geom
FROM (
VALUES
(1, '{}'::int, 0, '{}'::float),
(2, '{}'::int, '{}'::float, 1),
(3, '{}'::int, 0, '{}'::float),
(4, '{}'::int, '{}'::float, 1)
) AS tmp_edges_info (index, path_id, fraction_start, fraction_end)
JOIN core_path ON core_path.id = tmp_edges_info.path_id
)
SELECT
points.pid,
points.edge_id,
points.fraction_start,
points.fraction_end,
ST_SmartLineSubstring(
core_path.geom,
points.fraction_start,
points.fraction_end
) AS geom
FROM
(VALUES
(1, %s, 0, %s::float),
(1, %s, %s::float, 1),
(2, %s, 0, %s::float),
(2, %s, %s::float, 1)
) AS points(pid, edge_id, fraction_start, fraction_end)
JOIN core_path ON core_path.id = points.edge_id
),

pgr AS (
-- Get the route from point 1 to point 2 using pgr_withPoints.
-- next_node, prev_geom and next_geom will be used later
-- to reconstruct the final geometry of the shortest path.
path_id,
edge_id AS id,
source_id AS source,
target_id AS target,
ST_Length(edge_geom) AS cost,
ST_Length(edge_geom) AS reverse_cost,
ST_X(source_geom) AS x1,
ST_Y(source_geom) AS y1,
ST_X(target_geom) AS x2,
ST_Y(target_geom) AS y2,
edge_geom as geom
FROM graph_temporary_edges;

DROP TABLE IF EXISTS route;
CREATE TEMPORARY TABLE route AS
WITH pgr AS (
SELECT
pgr.path_seq,
pgr.node,
pgr.edge,
core_path.geom as edge_geom,
(LEAD(pgr.node) OVER (ORDER BY path_seq)) AS next_node,
(LAG(core_path.geom) OVER (ORDER BY path_seq)) AS prev_geom,
(LEAD(core_path.geom) OVER (ORDER BY path_seq)) AS next_geom
COALESCE(core_path.source, temporary_edges_info.source) as source,
COALESCE(core_path.target, temporary_edges_info.target) as target,
COALESCE(core_path.geom, temporary_edges_info.geom) as edge_geom,
CASE
WHEN pgr.edge > max_edge_id
THEN temporary_edges_info.path_id
ELSE pgr.edge
END AS edge
FROM
pgr_withPoints(
pgr_aStar(
'SELECT
id,
source,
target,
length as cost,
length as reverse_cost
length AS cost,
length AS reverse_cost,
ST_X(ST_StartPoint(geom)) AS x1,
ST_Y(ST_StartPoint(geom)) AS y1,
ST_X(ST_EndPoint(geom)) AS x2,
ST_Y(ST_EndPoint(geom)) AS y2
FROM core_path
WHERE draft = false AND visible = true
UNION ALL SELECT
id, source, target, cost, reverse_cost, x1, y1, x2, y2
FROM temporary_edges_info
ORDER by id',
'SELECT *
FROM (
VALUES
(1, %s, %s::float),
(2, %s, %s::float)
) AS points (pid, edge_id, fraction)',
-1, -2
) as pgr
JOIN core_path ON core_path.id = pgr.edge
),

route_geometry AS (
-- Get the geometries edge by edge.
-- At point 1 and 2, we get a portion of the edge.
SELECT
CASE
WHEN node = -1 THEN -- Start point
(SELECT points.geom
FROM points
-- Get the edge portion that leads to the next edge
WHERE points.pid = -pgr.node
ORDER BY ST_Distance(points.geom, pgr.next_geom) ASC
LIMIT 1)
WHEN next_node is NULL THEN
-- End point: the next row does not exist because next edge id is -1
(SELECT points.geom
FROM points
-- Get the edge portion that leads to the previous edge
WHERE points.pid = 2
ORDER BY ST_Distance(points.geom, pgr.prev_geom) ASC
LIMIT 1)
ELSE
edge_geom -- Return the full edge's geometry
END AS final_geometry,
edge,
CASE
WHEN node = -1 THEN
(SELECT points.fraction_start
FROM points
WHERE points.pid = -pgr.node
ORDER BY points.fraction_start DESC
LIMIT 1)
ELSE 0
END AS fraction_start,
CASE
WHEN next_node is NULL THEN
-- The next row does not exist because next edge id is -1
(SELECT points.fraction_end
FROM points
WHERE points.pid = 2
ORDER BY points.fraction_end ASC
LIMIT 1)
ELSE 1
END AS fraction_end,
node,
next_node
FROM pgr
max_vertex_id + 1, max_vertex_id + 2
) AS pgr
LEFT JOIN core_path ON id = pgr.edge
LEFT JOIN temporary_edges_info ON temporary_edges_info.id = pgr.edge
WHERE edge != -1
)

SELECT

CASE -- Reverse the geometries if needed
WHEN core_path.source = route_geometry.next_node
OR core_path.target = route_geometry.node THEN
-- If the ending point of the geometry (next_node) is the starting
-- point of the edge (path source), the geometry must be reversed
-- (same for starting point of the geom and ending pt of the edge).
-- pgr_createTopology has assigned the path's start point as core_path's source
-- with ST_StartPoint and the path's end point as core_path's target with ST_EndPoint
-- so we can use core_path.source as the starting pt of the geom.
ST_Reverse(final_geometry)
ELSE
final_geometry
END AS geometry,

CASE
WHEN source = next_node THEN ST_Reverse(edge_geom)
ELSE edge_geom
END AS edge_geom,
edge,

CASE -- Set fraction_start to 0 or 1 depending on the path
-- direction compared to the route (same logic as for geometry)
WHEN core_path.target = route_geometry.node THEN
-- fraction_start is set to 1 if the path is reversed
-- except for the starting geom
1
ELSE
fraction_start
END AS fraction_start,

CASE -- Set fraction_end to 0 or 1 depending on the path
-- direction compared to the route
WHEN core_path.source = route_geometry.next_node THEN
-- fraction_end is set to 0 if the path is reversed
-- except for the ending geom
0
ELSE
fraction_end
END AS fraction_end

FROM route_geometry
JOIN core_path on core_path.id = route_geometry.edge
"""

start_edge = from_step.get('edge_id')
end_edge = to_step.get('edge_id')
fraction_start = self._fix_fraction(from_step.get('fraction'))
fraction_end = self._fix_fraction(to_step.get('fraction'))
CASE
WHEN node = max_vertex_id + 1 THEN '{}'::float
WHEN node = source THEN 0
ELSE 1 -- node = target
END AS fraction_start,
CASE
WHEN next_node IS NULL THEN '{}'::float
WHEN next_node = source THEN 0
ELSE 1 -- next_node = target
END AS fraction_end
FROM pgr;

END $$;

SELECT
edge_geom,
edge,
fraction_start,
fraction_end
FROM route
""".format(
start_edge, fraction_start,
start_edge, fraction_start,
end_edge, fraction_end,
end_edge, fraction_end,
fraction_start, fraction_end
)

with connection.cursor() as cursor:
cursor.execute(query, [
start_edge, fraction_start,
start_edge, fraction_start,
end_edge, fraction_end,
end_edge, fraction_end,
start_edge, fraction_start,
end_edge, fraction_end
])
cursor.execute(query)

query_result = cursor.fetchall()
if query_result == []:
Expand Down
Loading