Unsupported versions:2.6 2.5 2.4 2.3 2.2
pgr_withPointsCost
¶
pgr_withPointsCost
- Calculates the shortest path and returns only the
aggregate cost of the shortest path found, for the combination of points
given.
Availability
Version 4.0.0
Function promoted to official.
Output columns standardized to
(start_vid, end_vid, agg_cost)
Signature change:
driving_side
parameter changed from named optional to unnamed positional.Directed graph valid values:
l
orL
andr
,R
Undirected graph valid values:
b
orB
Version 3.2.0
New proposed signature:
pgr_withPointsCost(Combinations)
Version 2.2.0
New proposed function.
Description¶
Modify the graph to include points defined by points_sql. Using Dijkstra algorithm, return only the aggregate cost of the shortest path found.
The main characteristics are:
Process is done only on edges with positive costs.
It does not return a path.
Returns the sum of the costs of the shortest path for pair combination of vertices in the modified graph.
The returned values are in the form of a set of
(start_vid, end_vid, agg_cost)
.
Vertices of the graph are:
positive when it belongs to the edges sql
negative when it belongs to the points sql
Values are returned when there is a path.
When the starting vertex and ending vertex are the same, there is no path.
The agg_cost in the non included values (v, v) is 0
When the starting vertex and ending vertex are the different and there is no path:
The agg_cost in the non included values (u, v) is
If the values returned are stored in a table, the unique index would be the pair: (start_vid, end_vid).
For undirected graphs, the results are symmetric.
The agg_cost of (u, v) is the same as for (v, u).
For optimization purposes, any duplicated value in the input arrays of start vids or end vids or are ignored.
The returned values are ordered:
start_vid ascending
end_vid ascending
Running time:
Signatures¶
[directed]
(start_vid, end_vid, agg_cost)
Note
There is no details flag, unlike the other members of the withPoints family of functions.
One to One¶
[directed]
(start_vid, end_vid, agg_cost)
- Example:
From point
to vertex with default options.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
-1, 10,'r');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | 10 | 6.4
(1 row)
One to Many¶
:class: signatures
- pgr_withPointsCost(Edges SQL, Points SQL, start vid, end vids, driving side [options])
- options:
[directed]
Returns set of(start_vid, end_vid, agg_cost)
OR EMPTY SET
- Example:
From point
to point and vertex on an undirected graph
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
-1, ARRAY[-3, 7], 'B',
directed => false);
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | -3 | 3.2
-1 | 7 | 1.6
(2 rows)
Many to One¶
[directed]
(start_vid, end_vid, agg_cost)
- Example:
From point
and vertex to point
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[-1, 6], -3, 'R');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | -3 | 4
6 | -3 | 2.6
(2 rows)
Many to Many¶
[directed]
(start_vid, end_vid, agg_cost)
- Example:
From point
and vertex to point and vertex
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[-1, 6], ARRAY[-3, 1], 'L');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | -3 | 3.2
-1 | 1 | 3.6
6 | -3 | 2.6
6 | 1 | 3
(4 rows)
Combinations¶
[directed]
(start_vid, end_vid, agg_cost)
- Example:
Two combinations
From point
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
'SELECT * FROM (VALUES (-1, 10), (6, -3)) AS combinations(source, target)',
'r');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | 10 | 6.4
6 | -3 | 2.6
(2 rows)
Parameters¶
Column |
Type |
Description |
---|---|---|
|
Edges SQL as described below |
|
|
Points SQL as described below |
|
|
Combinations SQL as described below |
|
start vid |
|
Identifier of the starting vertex of the path. Negative value is for point’s identifier. |
start vids |
|
Array of identifiers of starting vertices. Negative values are for point’s identifiers. |
end vid |
|
Identifier of the ending vertex of the path. Negative value is for point’s identifier. |
end vids |
|
Array of identifiers of ending vertices. Negative values are for point’s identifiers. |
Optional parameters¶
Column |
Type |
Default |
Description |
---|---|---|---|
|
|
|
|
With points optional parameters¶
Parameter |
Type |
Default |
Description |
---|---|---|---|
|
|
|
Value in [
|
Inner Queries¶
Edges SQL¶
Column |
Type |
Default |
Description |
---|---|---|---|
|
ANY-INTEGER |
Identifier of the edge. |
|
|
ANY-INTEGER |
Identifier of the first end point vertex of the edge. |
|
|
ANY-INTEGER |
Identifier of the second end point vertex of the edge. |
|
|
ANY-NUMERICAL |
Weight of the edge ( |
|
|
ANY-NUMERICAL |
-1 |
Weight of the edge (
|
Where:
- ANY-INTEGER:
SMALLINT
,INTEGER
,BIGINT
- ANY-NUMERICAL:
SMALLINT
,INTEGER
,BIGINT
,REAL
,FLOAT
Points SQL¶
Parameter |
Type |
Default |
Description |
---|---|---|---|
|
ANY-INTEGER |
value |
Identifier of the point.
|
|
ANY-INTEGER |
Identifier of the “closest” edge to the point. |
|
|
ANY-NUMERICAL |
Value in <0,1> that indicates the relative postition from the first end point of the edge. |
|
|
|
|
Value in [
|
Where:
- ANY-INTEGER:
SMALLINT
,INTEGER
,BIGINT
- ANY-NUMERICAL:
SMALLINT
,INTEGER
,BIGINT
,REAL
,FLOAT
Combinations SQL¶
Parameter |
Type |
Description |
---|---|---|
|
ANY-INTEGER |
Identifier of the departure vertex. |
|
ANY-INTEGER |
Identifier of the arrival vertex. |
Where:
- ANY-INTEGER:
SMALLINT
,INTEGER
,BIGINT
Result columns¶
Set of (start_vid, end_vid, agg_cost)
Column |
Type |
Description |
---|---|---|
|
|
Identifier of the starting vertex. |
|
|
Identifier of the ending vertex. |
|
|
Aggregate cost from |
Note
When start_vid or end_vid columns have negative values, the identifier is for a Point.
Additional Examples¶
Use pgr_findCloseEdges in the Points SQL.¶
Find the cost of the routes from vertex
SELECT * FROM pgr_withPointsCost(
$e$ SELECT * FROM edges $e$,
$p$ SELECT edge_id, round(fraction::numeric, 2) AS fraction, side
FROM pgr_findCloseEdges(
$$SELECT id, geom FROM edges$$,
(SELECT ST_POINT(2.9, 1.8)),
0.5, cap => 2)
$p$,
1, ARRAY[-1, -2],'B', false);
start_vid | end_vid | agg_cost
-----------+---------+----------
1 | -2 | 2.9
1 | -1 | 3.2
(2 rows)
Point
corresponds to the closest edge from point (2.9, 1.8).Point
corresponds to the next close edge from point (2.9, 1.8).Being close to the graph does not mean have a shorter route.
Right side driving topology¶
Traveling from point
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[5, -1], ARRAY[-2, -3, -6, 10, 11],
'r');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | -6 | 2.1
-1 | -3 | 4
-1 | -2 | 4.8
-1 | 10 | 6.4
-1 | 11 | 3.4
5 | -6 | 1.7
5 | -3 | 3.6
5 | -2 | 4.4
5 | 10 | 6
5 | 11 | 3
(10 rows)
Left side driving topology¶
Traveling from point
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[5, -1], ARRAY[-2, -3, -6, 10, 11],
'l');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | -6 | 1.3
-1 | -3 | 3.2
-1 | -2 | 5.2
-1 | 10 | 5.6
-1 | 11 | 2.6
5 | -6 | 1.7
5 | -3 | 3.6
5 | -2 | 5.6
5 | 10 | 6
5 | 11 | 3
(10 rows)
Does not matter driving side driving topology¶
Traveling from point
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edges ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[5, -1], ARRAY[-2, -3, -6, 10, 11], 'R');
start_vid | end_vid | agg_cost
-----------+---------+----------
-1 | -6 | 2.1
-1 | -3 | 4
-1 | -2 | 4.8
-1 | 10 | 6.4
-1 | 11 | 3.4
5 | -6 | 1.7
5 | -3 | 3.6
5 | -2 | 4.4
5 | 10 | 6
5 | 11 | 3
(10 rows)
See Also¶
Indices and tables