pgRouting was first called pgDijkstra, because it implemented only shortest path search with Dijkstra algorithm. Later other functions were added and the library was renamed to pgRouting. This chapter will cover selected pgRouting algorithms and some of the attributes required.
Dijkstra algorithm was the first algorithm implemented in pgRouting. It doesn’t
require other attributes than id
, source
and target
ID and cost
.
You can specify when to consider the graph as directed or undirected.
Signature Summary
pgr_dijkstra(edges_sql, start_vid, end_vid)
pgr_dijkstra(edges_sql, start_vid, end_vid, directed)
pgr_dijkstra(edges_sql, start_vid, end_vids, directed)
pgr_dijkstra(edges_sql, start_vids, end_vid, directed)
pgr_dijkstra(edges_sql, start_vids, end_vids, directed)
RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost)
OR EMPTY SET
Description of the parameters can be found in pgr_dijkstra.
Note
sql::text
as one of their arguments. While
this may look confusing at first, it makes the functions very flexible as
the user can pass any SELECT
statement as function argument as long as
the returned result contains the required number of attributes and the
correct attribute names.Identifiers for the Queries
The assignment of the vertices identifiers on the source and target columns may be different, the following exercises will use the results of this query. For the workshop, some locations of the FOSS4G Bonn event are going to be used. These locations are within this area http://www.openstreetmap.org/#map=15/50.7101/7.1262
SELECT osm_id, id FROM ways_vertices_pgr
WHERE osm_id IN (33180347, 253908904, 332656435, 3068609695, 277708679)
ORDER BY osm_id;
osm_id | id
------------+-------
33180347 | 13224
253908904 | 6549
277708679 | 6963
332656435 | 1458
3068609695 | 9224
(4 rows)
The corresponding id
, used in the workshop, and a sample route:
Exercise 1 - “Single pedestrian routing”
13224
to vertex 6549
.length
, which
was calculated by osm2pgrouting, is in unit degrees
.undirected
, that is, the
pedestrian can move in both directions on all segments.SELECT * FROM pgr_dijkstra('
SELECT gid AS id,
source,
target,
length AS cost
FROM ways',
13224, 6549, directed := false);
Note
ORDER BY seq
will ensure that the path is
in the right order again.edges_sql::text
argument. In this example cost is length
in unit
“degrees”. Cost may be time, distance or any combination of both or any
other attributes or a custom formula.node
and edge
results may vary depending on the assignment of the
identifiers to the vertices given by osm2pgrouting.Exercise 2 - “Many Pedestrians going to the same destination.”
6549
, 1458
and 9224
.13224
.length_m
.SELECT * FROM pgr_dijkstra('
SELECT gid AS id,
source,
target,
length_m AS cost
FROM ways',
ARRAY[6549, 1458, 9224], 13224, directed := false);
Exercise 3 - “Many Pedestrians departing from the same location”
13224
.6549
, 1458
and 9224
.s = 1.3 m/s
and t = d/s
SELECT * FROM pgr_dijkstra('
SELECT gid AS id,
source,
target,
length_m / 1.3 AS cost
FROM ways',
13224, ARRAY[6549, 1458, 9224], directed := false);
Exercise 4 - “Many Pedestrians going to different destinations.”
6549
, 1458
and 9224
.13224
or 6963
.s = 1.3 m/s
and t = d/s
SELECT * FROM pgr_dijkstra('
SELECT gid AS id,
source,
target,
length_m / 1.3 / 60 AS cost
FROM ways',
ARRAY[6549, 1458, 9224],
ARRAY[13224, 6963],
directed := false);
Note
Inspecting the results, looking for totals (when edge = -1):
58.54119347 = 19.9557289926127 + 6.63986000245047 + 31.9456044752323
41.268599693 = 13.5539128131556 + 8.34274572465808 + 19.3719411554243
When the main goal is to calculate the total cost, for example to calculate
multiple routes for a cost matrix, then pgr_dijkstraCost
returns a more
compact result.
Signature Summary
pgr_dijkstraCost(edges_sql, start_vid, end_vid)
pgr_dijkstraCost(edges_sql, start_vid, end_vid, directed)
pgr_dijkstraCost(edges_sql, start_vid, end_vids, directed)
pgr_dijkstraCost(edges_sql, start_vids, end_vid, directed)
pgr_dijkstraCost(edges_sql, start_vids, end_vids, directed)
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Description of the parameters can be found in pgr_dijkstraCost
Exercise 5 - “Many Pedestrians going to different destinations returning aggregate costs.”
6549
, 1458
and 9224
.13224
or 6963
.s = 1.3 m/s
and t = d/s
SELECT *
FROM pgr_dijkstraCost('
SELECT gid AS id,
source,
target,
length_m / 1.3 / 60 AS cost
FROM ways',
ARRAY[6549, 1458, 9224],
ARRAY[13224, 6963],
directed := false) ORDER BY end_vid;
A-Star algorithm is another well-known routing algorithm. It adds geographical information to source and target of each network link. This enables the routing query to prefer links which are closer to the target of the shortest path search.
Signature Summary
pgr_costResult[] pgr_astar(sql text, source integer, target integer, directed boolean, has_rcost boolean);
Returns a set of pgr_costResult
(seq, id1, id2, cost) rows, that make up a path.
Description of the parameters can be found in pgr_astar.
Exercise 6 - “Single Pedestrian Routing with Astar.”
13224
to vertex 6549
.length
is in degrees
.SELECT seq, id1 AS node, id2 AS edge, cost FROM pgr_astar('
SELECT gid::integer AS id,
source::integer,
target::integer,
length::double precision AS cost,
x1, y1, x2, y2
FROM ways',
13224, 6549, false, false);
Note
There are many other functions available with the latest pgRouting release, most of them work in similar ways. For the complete list of pgRouting functions see the API documentation: http://docs.pgrouting.org/