I have created the "ways" (=edges) and "ways_vertices_pgr" (=nodes) tables from OSM road data with osm2pgrouting in PostGIS. I work with this data in QGIS.
Given a set of ids from the "ways_vertices_pgr" table I would like to tackle the following task:
- perform a travelling salesperson problem
- create a vector layer that represents the result
So far I have managed to create a cost matrix with pgr_dijkstraCostMatrix, and pass it to the pgr_TSP function.
SELECT seq, node, the_geom FROM pgr_TSP(
$$
SELECT * FROM pgr_dijkstraCostMatrix(
'SELECT gid AS id, source, target, cost, reverse_cost FROM ways',
(
SELECT ARRAY[9769, 20604, 48398, 42533, 49819, 41141, 45520] node_array
),
directed := false
)
$$,
start_id := 9769,
randomize := false
) as res
JOIN ways_vertices_pgr
ON ways_vertices_pgr.id = res.node;
The result is the sequence, in which the nodes should be travelled:
From here I am stuck.
How do I connect the dots, to create a full path from this which can be represented as a vector layer?
My naive approach is to use pgr_dijkstra on every segment of the path and then create a large table from the result. It is a real pain to implement this in PostgreSQL, though and I haven't managed to do that. This approach seems to be too complicated and not very elegant, anyways.
pgr_dijkstraVia
, with yournode
ids inseq
order as array. that still lacks a certain elegance, since it's adding another call to the routing engine (which, if use right, is blazingly fast, so not to worry much), but the alternative would be to join the edge table on ambiguousnode AND LEAD(node) = source OR target
constructs.