pgr_withPointsCost  Proposed  pgRouting Manual (3.3)
pgr_withPointsCost  Proposed
pgr_withPointsCost
 Calculates the shortest path and returns only the aggregate cost of the shortest path(s) found, for the combination of points given.
Warning
Proposed functions for next mayor release.

They are not officially in the current release.

They will likely officially be part of the next mayor release:

The functions make use of ANYINTEGER and ANYNUMERICAL

Name might not change. (But still can)

Signature might not change. (But still can)

Functionality might not change. (But still can)

pgTap tests have being done. But might need more.

Documentation might need refinement.

Availability

Version 3.2.0

New proposed function:

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(s) found.
 The main characteristics are:


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.

Vertices of the graph are:

positive when it belongs to the edges_sql

negative when it belongs to the points_sql


Process is done only on edges with positive costs.

Values are returned when there is a path.

The returned values are in the form of a set of (start_vid, end_vid, agg_cost) .

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 \(\infty\)



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 start_vids or end_vids is ignored.

The returned values are ordered:

start_vid ascending

end_vid ascending


Running time: \(O( start\_vids * (V \log V + E))\)

Signatures
Summary
pgr_withPointsCost(edges_sql, points_sql, from_vid, to_vid [, directed] [, driving_side])
pgr_withPointsCost(edges_sql, points_sql, from_vid, to_vids [, directed] [, driving_side])
pgr_withPointsCost(edges_sql, points_sql, from_vids, to_vid [, directed] [, driving_side])
pgr_withPointsCost(edges_sql, points_sql, from_vids, to_vids [, directed] [, driving_side])
pgr_withPointsCost(Edges SQL, Points SQL, Combinations SQL [, directed] [, driving_side] [, details])
RETURNS SET OF (start_vid, end_vid, agg_cost)
Note
There is no details flag, unlike the other members of the withPoints family of functions.
Using defaults
pgr_withPointsCost(edges_sql, points_sql, start_vid, end_vid)
RETURNS SET OF (start_vid, end_vid, agg_cost)
 Example :

From point \(1\) to point \(3\)

For a directed graph.

The driving side is set as b both. So arriving/departing to/from the point(s) can be in any direction.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
1, 3);
start_pid end_pid agg_cost
++
1 3 3.2
(1 row)
One to One
pgr_withPointsCost(edges_sql, points_sql, from_vid, to_vid [, directed] [, driving_side])
RETURNS SET OF (seq, node, edge, cost, agg_cost)
 Example :

From point \(1\) to vertex \(3\) on an undirected graph.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
1, 3,
directed := false);
start_pid end_pid agg_cost
++
1 3 1.6
(1 row)
One to Many
pgr_withPointsCost(edges_sql, points_sql, from_vid, to_vids [, directed] [, driving_side])
RETURNS SET OF (start_vid, end_vid, agg_cost)
 Example :

From point \(1\) to point \(3\) and vertex \(5\) on a directed graph.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
1, ARRAY[3,5]);
start_pid end_pid agg_cost
++
1 3 3.2
1 5 1.6
(2 rows)
Many to One
pgr_withPointsCost(edges_sql, points_sql, from_vids, to_vid [, directed] [, driving_side])
RETURNS SET OF (start_vid, end_vid, agg_cost)
 Example :

From point \(1\) and vertex \(2\) to point \(3\) on a directed graph.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[1,2], 3);
start_pid end_pid agg_cost
++
1 3 3.2
2 3 2.6
(2 rows)
Many to Many
pgr_withPointsCost(edges_sql, points_sql, from_vids, to_vids [, directed] [, driving_side])
RETURNS SET OF (start_vid, end_vid, agg_cost)
 Example :

From point \(1\) and vertex \(2\) to point \(3\) and vertex \(7\) on a directed graph.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[1,2], ARRAY[3,7]);
start_pid end_pid agg_cost
++
1 3 3.2
1 7 3.6
2 3 2.6
2 7 3
(4 rows)
Combinations SQL
pgr_withPointsCost(Edges SQL, Points SQL, Combinations SQL [, directed] [, driving_side] [, details])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
 Example :

Two (source, target) combinations: (from point \(1\) to vertex \(3\) ), and (from vertex \(2\) to point \(3\) ) with right side driving topology.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
'SELECT * FROM ( VALUES (1, 3), (2, 3) ) AS t(source, target)',
driving_side => 'r');
start_pid end_pid agg_cost
++
1 3 6.4
2 3 2.6
(2 rows)
Parameters
Parameter 
Type 
Description 

Edges SQL 

Edges query as described above. 
Points SQL 

Points query as described above. 
Combinations SQL 

Combinations query as described below. 
start_vid 

Starting vertex identifier. When negative: is a point’s pid. 
end_vid 

Ending vertex identifier. When negative: is a point’s pid. 
start_vids 

Array of identifiers of starting vertices. When negative: is a point’s pid. 
end_vids 

Array of identifiers of ending vertices. When negative: is a point’s pid. 
directed 

(optional). When

driving_side 


Inner query
Edges query
Column 
Type 
Default 
Description 

id 

Identifier of the edge. 

source 

Identifier of the first end point vertex of the edge. 

target 

Identifier of the second end point vertex of the edge. 

cost 

Weight of the edge (source, target)


reverse_cost 

1 
Weight of the edge (target, source) ,

Where:
 ANYINTEGER :

SMALLINT, INTEGER, BIGINT
 ANYNUMERICAL :

SMALLINT, INTEGER, BIGINT, REAL, FLOAT
Points query
Description of the Points SQL query
 points_sql :

an SQL query, which should return a set of rows with the following columns:
Column 
Type 
Description 

pid 

(optional) Identifier of the point.

edge_id 

Identifier of the "closest" edge to the point. 
fraction 

Value in <0,1> that indicates the relative postition from the first end point of the edge. 
side 

(optional) Value in [‘b’, ‘r’, ‘l’, NULL] indicating if the point is:

Where:
 ANYINTEGER :

smallint, int, bigint
 ANYNUMERICAL :

smallint, int, bigint, real, float
Combinations query
Column 
Type 
Default 
Description 

source 

Identifier of the first end point vertex of the edge. 

target 

Identifier of the second end point vertex of the edge. 
Where:
 ANYINTEGER :

SMALLINT, INTEGER, BIGINT
Result Columns
Column 
Type 
Description 

start_vid 

Identifier of the starting vertex. When negative: is a point’s pid. 
end_vid 

Identifier of the ending point. When negative: is a point’s pid. 
agg_cost 

Aggregate cost from

Additional Examples
 Example :

From point \(1\) and vertex \(2\) to point \(3\) and vertex \(7\) , with right side driving topology
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[1,2], ARRAY[3,7],
driving_side := 'l');
start_pid end_pid agg_cost
++
1 3 3.2
1 7 3.6
2 3 2.6
2 7 3
(4 rows)
 Example :

From point \(1\) and vertex \(2\) to point \(3\) and vertex \(7\) , with left side driving topology
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[1,2], ARRAY[3,7],
driving_side := 'r');
start_pid end_pid agg_cost
++
1 3 4
1 7 4.4
2 3 2.6
2 7 3
(4 rows)
 Example :

From point \(1\) and vertex \(2\) to point \(3\) and vertex \(7\) , does not matter driving side.
SELECT * FROM pgr_withPointsCost(
'SELECT id, source, target, cost, reverse_cost FROM edge_table ORDER BY id',
'SELECT pid, edge_id, fraction, side from pointsOfInterest',
ARRAY[1,2], ARRAY[3,7],
driving_side := 'b');
start_pid end_pid agg_cost
++
1 3 3.2
1 7 3.6
2 3 2.6
2 7 3
(4 rows)
The queries use the Sample Data network.
See Also
Indices and tables