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 Argentina event are going to be used. These locations are within this area https://www.openstreetmap.org#map=15/-34.5847/-58.3970
192903446
Plaza Intendente Alvear4289340366
Hard Rock Café2153015792
Facultad de Derecho6357258588
Centro de Convenciones Buenos Aires196017392
Palacio Duhau-Park Hyatt Buenos Aires
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 (192903446, 4289340366, 2153015792, 6357258588, 196017392)
3ORDER BY osm_id;
1 osm_id | id
2------------+-------
3 192903446 | 1993
4 196017392 | 2197
5 2153015792 | 6646
6 4289340366 | 9129
7 6357258588 | 15011
8(5 rows)
9
192903446
Plaza Intendente Alvear (1993
)4289340366
Hard Rock Café (9129
)2153015792
Facultad de Derecho (6646
)6357258588
Centro de Convenciones Buenos Aires (15011
)196017392
Palacio Duhau-Park Hyatt Buenos Aires (2197
)
The corresponding id
are shown in the following image, and a sample route from
“Facultad de Derecho” to “Palacio Duhau-Park Hyatt Buenos Aires”.
2.1.1. Exercise 1: Single pedestrian routing¶
Problem:
Walking from “Plaza Intendente Alvear” to the “Facultad de Derecho”.
Solution:
The pedestrian wants to go from vertex
1993
to vertex6646
(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 1993,
10 6646,
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 “Plaza Intendente Alvear” and “Hard Rock Café” to the “Facultad de Derecho”.
Solution:
The pedestrians are departing at vertices
1993
and9129
(line 9).All pedestrians want to go to vertex
6646
(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[1993,9129],
106646,
11directed := false);
2.1.3. Exercise 3: Many Pedestrians departing from the same location¶
Problem:
Walking from the “Facultad de Derecho” to the “Plaza Intendente Alvear” and “Hard Rock Café” (in seconds).
Solution:
All pedestrians are departing from vertex
6646
(line 9).Pedestrians want to go to locations
1993
and9129
(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 ',
96646,
10ARRAY[1993,9129],
11directed := false);
2.1.4. Exercise 4: Many Pedestrians going to different destinations¶
Problem:
Walking from the hotels to the “Centro de Convenciones Buenos Aires” and “Palacio Duhau-Park Hyatt Buenos Aires” (in minutes).
Solution:
The pedestrians depart from
1993
and9129
(line 9).The pedestrians want to go to destinations
15011
and2197
(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[1993, 9129],
10ARRAY[15011, 2197],
11directed := false);
Exercise: 4 (Chapter: Pedestrian)
Note
Inspecting the results, looking for totals (edge = -1):
Going to vertex
15011
:from
1993
takes 8.84.. minutes (seq = 35)from
9129
takes 5.84.. minutes (seq = 74)
Going to vertex
2197
:from
1993
takes 7.44.. minutes (seq = 7)from
9129
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 “Centro de Convenciones Buenos Aires” or “Palacio Duhau-Park Hyatt Buenos Aires” (get only the cost in minutes).
Solution:
The pedestrians depart from
1993
and9129
(line 10).The pedestrians want to go to destinations
15011
and2197
(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[1993, 9129],
11ARRAY[15011, 2197],
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 “Centro de Convenciones Buenos Aires” or “Palacio Duhau-Park Hyatt Buenos Aires” (summarize cost in minutes).
Solution:
The pedestrians depart from
1993
and9129
(line 10).The pedestrians want to go to destinations
15011
and2197
(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[1993, 9129],
11 ARRAY[15011, 2197],
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 “Hard Rock Café” than from the “Plaza Intendente Alvear”.