2. Pedestrian Routing¶
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.
2.1. pgr_dijkstra¶
Dijkstra algorithm was the first algorithm implemented in pgRouting. It doesn’t
require other attributes than id
, source
and target
ID and cost
and reverse_cost
.
You can specify when to consider the graph as directed or undirected.
Signature Summary
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])
pgr_dijkstra(Edges SQL, Combinations SQL [, 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
Many pgRouting functions have
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 aSELECT
statement as function argument as long as the returned result contains the required number of attributes and the correct attribute names.Most of pgRouting implemented algorithms do not require the geometry.
The pgRouting functions do not return a geometry, but only an ordered list of nodes or edges.
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 near of the FOSS4G event are going to be used. These locations are within this area https://www.openstreetmap.org#map=15/-34.5847/-58.3970
266939565
Mercato Centrale2531656518
Stazione di Santa Maria Novella5020458299
Palazzo dei Congressi6483155124
Hotel Albani1798374718
Cattedrale di Santa Maria del Fiore
Connect to the database, if not connected:
psql city_routing
Get the vertex identifiers
1SELECT osm_id, id FROM ways_vertices_pgr
2WHERE osm_id IN (266939565, 2531656518, 5020458299, 6483155124, 1798374718)
3ORDER BY osm_id;
1 osm_id | id
2------------+-------
3 266939565 | 956
4 1798374718 | 6354
5 2531656518 | 7039
6 5020458299 | 11186
7 6483155124 | 21407
8(5 rows)
9
266939565
Mercato Centrale (956
)2531656518
Stazione di Santa Maria Novella (7039
)5020458299
Palazzo dei Congressi (11186
)6483155124
Hotel Albani (21407
)1798374718
Cattedrale di Santa Maria del Fiore (6354
)
The corresponding id
are shown in the following image, and a sample route from
“Palazzo dei Congressi” to “Cattedrale di Santa Maria del Fiore”.
2.1.1. Exercise 1: Single pedestrian routing¶
Problem:
Walking from “Mercato Centrale” to the “Palazzo dei Congressi”.
Solution:
The pedestrian wants to go from vertex
956
to vertex11186
(lines 9 and 10).The pedestrian’s cost is in terms of length. In this case
length
(line 6), which was calculated by osm2pgrouting, is in unitdegrees
.From a pedestrian perspective the graph is
undirected
(line 11), that is, the pedestrian can move in both directions on all segments.
1SELECT * FROM pgr_dijkstra(
2 '
3 SELECT gid AS id,
4 source,
5 target,
6 length AS cost
7 FROM ways
8 ',
9 956,
10 11186,
11 directed := false);
Exercise: 1 (Chapter: Pedestrian)
Note
The returned cost attribute represents the cost specified in the inner SQL query (
edges_sql::text
argument). In this example cost islength
in unit “degrees”. Cost may be time, distance or any combination of both or any other attributes or a custom formula.node
andedge
results may vary depending on the assignment of the identifiers to the vertices given by osm2pgrouting.
2.1.2. Exercise 2: Many Pedestrians going to the same destination¶
Problem:
Walking from the “Mercato Centrale” and “Stazione di Santa Maria Novella” to the “Palazzo dei Congressi”.
Solution:
The pedestrians are departing at vertices
956
and7039
(line 9).All pedestrians want to go to vertex
11186
(line 10).The cost to be in meters using attribute
length_m
(line 6).
1SELECT * FROM pgr_dijkstra(
2 '
3 SELECT gid AS id,
4 source,
5 target,
6 length_m AS cost
7 FROM ways
8 ',
9ARRAY[956,7039],
1011186,
11directed := false);
2.1.3. Exercise 3: Many Pedestrians departing from the same location¶
Problem:
Walking from the “Palazzo dei Congressi” to the “Mercato Centrale” and “Stazione di Santa Maria Novella” (in seconds).
Solution:
All pedestrians are departing from vertex
11186
(line 9).Pedestrians want to go to locations
956
and7039
(line 10).The cost to be in seconds, with a walking speed
s = 1.3 m/s
andt = d/s
(line 6).
1SELECT * FROM pgr_dijkstra(
2 '
3 SELECT gid AS id,
4 source,
5 target,
6 length_m / 1.3 AS cost
7 FROM ways
8 ',
911186,
10ARRAY[956,7039],
11directed := false);
2.1.4. Exercise 4: Many Pedestrians going to different destinations¶
Problem:
Walking from Mercato Centrale and Stazione di Santa Maria Novella to the “Hotel Albani” and “Cattedrale di Santa Maria del Fiore” (in minutes).
Solution:
The pedestrians depart from
956
and7039
(line 9).The pedestrians want to go to destinations
21407
and6354
(line 10).The cost to be in minutes, with a walking speed
s = 1.3 m/s
andt = d/s
(line 6).Result adds the costs per destination.
1SELECT * FROM pgr_dijkstra(
2 '
3 SELECT gid AS id,
4 source,
5 target,
6 length_m / 1.3 / 60 AS cost
7 FROM ways
8 ',
9ARRAY[956, 7039],
10ARRAY[21407, 6354],
11directed := false);
Exercise: 4 (Chapter: Pedestrian)
Note
Inspecting the results, looking for totals (edge = -1):
Going to vertex
21407
:from
956
takes 8.84.. minutes (seq = 35)from
7039
takes 5.84.. minutes (seq = 74)
Going to vertex
6354
:from
956
takes 7.44.. minutes (seq = 7)from
7039
takes 12.06.. minutes (seq = 55)
2.2. pgr_dijkstraCost¶
When the main goal is to calculate the total cost, without “inspecting” the pgr_dijkstra results,
using pgr_dijkstraCost
returns a more compact result.
Signature Summary
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])
pgr_dijkstraCost(edges_sql, combinations_sql [, directed])
RETURNS SET OF (start_vid, end_vid, agg_cost)
OR EMPTY SET
Description of the parameters can be found in pgr_dijkstraCost
2.2.1. Exercise 5: Many Pedestrians going to different destinations returning aggregate costs¶
Problem:
Walking from the hotels to the “Hotel Albani” or “Cattedrale di Santa Maria del Fiore” (get only the cost in minutes).
Solution:
The pedestrians depart from
956
and7039
(line 10).The pedestrians want to go to destinations
21407
and6354
(line 11).The cost to be in minutes, with a walking speed
s = 1.3 m/s
andt = d/s
(line 7).Result as aggregated costs.
1SELECT *
2FROM pgr_dijkstraCost(
3 '
4 SELECT gid AS id,
5 source,
6 target,
7 length_m / 1.3 / 60 AS cost
8 FROM ways
9 ',
10ARRAY[956, 7039],
11ARRAY[21407, 6354],
12directed := false);
Exercise: 5 (Chapter: Pedestrian)
Compare with Exercise 4: Many Pedestrians going to different destinations ‘s note.
2.2.2. Exercise 6: Many Pedestrians going to different destinations summarizing the total costs per departure¶
Problem:
Walking from the hotels to the “Hotel Albani” or “Cattedrale di Santa Maria del Fiore” (summarize cost in minutes).
Solution:
The pedestrians depart from
956
and7039
(line 10).The pedestrians want to go to destinations
21407
and6354
(line 11).The cost to be in minutes, with a walking speed s = 1.3 m/s and t = d/s (line 7).
Result adds the costs per destination.
1SELECT start_vid, sum(agg_cost)
2FROM pgr_dijkstraCost(
3 '
4 SELECT gid AS id,
5 source,
6 target,
7 length_m / 1.3 / 60 AS cost
8 FROM ways
9 ',
10 ARRAY[956, 7039],
11 ARRAY[21407, 6354],
12 directed := false)
13GROUP BY start_vid
14ORDER BY start_vid;
Exercise: 6 (Chapter: Pedestrian)
Note
An interpretation of the result can be: In general, it is faster to depart from the “Stazione di Santa Maria Novella” than from the “Mercato Centrale”.