Supported versions: latest (3.8) 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0 main dev
Unsupported versions:2.6 2.5 2.4 2.3 2.2

pgr_withPointsCost

pgr_withPointsCost - 计算最短路径,并只返回所给点组合的最短路径的总成本。

可用性

Version 4.0.0

  • 函数正式发布。

  • Driving side parameter is unnamed and compulsory.

    • Valid values depend on kind of graph

  • Output columns standardized to (start_vid, end_vid, agg_cost)

  • Breaking change, signatures no longer available:

    • pgr_withpointscost(text,text,anyarray,anyarray,boolean,character)

    • pgr_withpointscost(text,text,anyarray,bigint,boolean,character)

    • pgr_withpointscost(text,text,bigint,anyarray,boolean,character)

    • pgr_withpointscost(text,text,bigint,bigint,boolean,character)

    • pgr_withpointscost(text,text,text,boolean,character)

版本3.2.0

  • 新提议的签名:

    • pgr_withPointsCost(组合)

版本 2.2.0

  • 新提议的函数。

描述

修改图形以包含由 points_sql 定义的点。使用 Dijkstra 算法,只返回找到的最短路径的总成本。

主要特点是:

  • 仅在具有正成本的边进行处理。

  • 它不返回路径。

  • 返回修改图中顶点对组合的最短路径成本之和。

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

  • 图的顶点是:

    • 当它属于edges_sql时为

    • 当它属于 points_sql时为

  • 当存在路径时返回值。

    • 当起始顶点和结束顶点相同时,就没有路径。

      • 非包含值 (v, v) 中的 agg_cost0

    • 当起始顶点和结束顶点不同且不存在路径时:

      • 非包含值 (u, v) 中的 agg_cost

    • 如果返回的值存储在表中,则唯一索引将是这一对: (start_vid, end_vid)

    • 对于 无向 图,结果是 对称 的。

      • (u, v)agg_cost(v, u) 相同。

  • For optimization purposes, any duplicated value in the input arrays of start vids or end vids or are ignored.

  • 返回值是有序的:

    • start_vid 升序

    • end_vid 升序

  • 运行时间: O(|start_vids|×(VlogV+E))

内部使用 Boost Graph Boost 图内部

签名

总结

pgr_withPointsCost(Edges SQL, Points SQL, start vid, end vid, driving side [options])
pgr_withPointsCost(Edges SQL, Points SQL, start vid, end vids, driving side [options])
pgr_withPointsCost(Edges SQL, Points SQL, start vids, end vid, driving side [options])
pgr_withPointsCost(Edges SQL, Points SQL, start vids, end vids, driving side [options])
pgr_withPointsCost(Edges SQL, Points SQL, Combinations SQL, driving side [options])
options: [directed]
返回 (start_vid, end_vid, agg_cost) 的集合
OR EMPTY SET

Note

与 withPoints 函数系列的其他成员不同,没有 详细信息 标志。

One to One

pgr_withPointsCost(Edges SQL, Points SQL, start vid, end vid, driving side [options])
options: [directed]
返回 (start_vid, end_vid, agg_cost) 的集合
OR EMPTY SET

From point 1 to vertex 10 with right driving side in directed 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, 10,'r');
 start_vid | end_vid | agg_cost
-----------+---------+----------
        -1 |      10 |      6.4
(1 row)

一对多

pgr_withPointsCost(Edges SQL, Points SQL, start vid, end vids, driving side [options])
options: [directed]
返回 (start_vid, end_vid, agg_cost) 的集合
OR EMPTY SET

无向图上的点 1 到点 3 和顶点 7

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

pgr_withPointsCost(Edges SQL, Points SQL, start vids, end vid, driving side [options])
options: [directed]
返回 (start_vid, end_vid, agg_cost) 的集合
OR EMPTY SET
示例:

From point 1 and vertex 6 to point 3 with right driving side in directed 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',
  ARRAY[-1, 6], -3, 'R');
 start_vid | end_vid | agg_cost
-----------+---------+----------
        -1 |      -3 |        4
         6 |      -3 |      2.6
(2 rows)

Many to Many

pgr_withPointsCost(Edges SQL, Points SQL, start vids, end vids, driving side [options])
options: [directed]
返回 (start_vid, end_vid, agg_cost) 的集合
OR EMPTY SET
示例:

From point 1 and vertex 6 to point 3 and vertex 1 with left side driving.

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)

组合

pgr_withPointsCost(Edges SQL, Points SQL, Combinations SQL, driving side [options])
options: [directed]
返回 (start_vid, end_vid, agg_cost) 的集合
OR EMPTY SET
示例:

两种组合

From point 1 to vertex 10, and from vertex 6 to point 3 with right side driving.

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)

参数

类型

描述

Edges SQL

TEXT

Edges SQL 如下所述

Points SQL

TEXT

Points SQL 如下所述

Combinations SQL

TEXT

Combinations SQL 如下所述

start vid

BIGINT

路径起始顶点的标识符。 负值用于点的标识符。

start vids

ARRAY[BIGINT]

起始顶点的标识符数组。 负值用于点的标识符。

end vid

BIGINT

路径结束顶点的标识符。 负值用于点的标识符。

end vids

ARRAY[BIGINT]

结束顶点的标识符数组。 负值用于点的标识符。

driving side

CHAR

值在 [r, R, l, L, b, B] 中表示驾驶的一侧是:

  • [r, R] 表示右侧驾驶(仅适用于有向图)

  • [l, L]表示左侧驾驶(仅适用有向图)

  • [b, B] 表示两侧通行(仅适用于无向图)

可选参数

类型

默认

描述

directed

BOOLEAN

true

  • true 时,该图被视为有 有向

  • 如果为 false ,则该图被视为 无向

内部查询

Edges SQL

类型

默认

描述

id

ANY-INTEGER

边的标识符。

source

ANY-INTEGER

边的第一个端点顶点的标识符。

target

ANY-INTEGER

边的第二个端点顶点的标识符。

cost

ANY-NUMERICAL

edge (source, target)的权重

reverse_cost

ANY-NUMERICAL

-1

边(target, source)的权重

  • 当为负时:edge (target, source) 不存在,因此它不是图的一部分。

其中:

ANY-INTEGER:

SMALLINT, INTEGER, BIGINT

ANY-NUMERICAL:

SMALLINT, INTEGER, BIGINT, REAL, FLOAT

Points SQL

参数

类型

默认

描述

pid

ANY-INTEGER

value

点的标识符。

  • 使用正值,因为内部将转换为负值

  • 如果列存在,则它不能为 NULL。

  • 如果列不存在,将自动给出连续的负

edge_id

ANY-INTEGER

距离该点“最近”的边的标识符。

fraction

ANY-NUMERICAL

<0,1> 中的值指示距边缘第一个端点的相对位置。

side

CHAR

b

[b, r, l, NULL] 中的值指示该点是否为:

  • r 在右边,

  • l 在左边,

  • b, NULL 在两边

其中:

ANY-INTEGER:

SMALLINT, INTEGER, BIGINT

ANY-NUMERICAL:

SMALLINT, INTEGER, BIGINT, REAL, FLOAT

分量 SQL

参数

类型

描述

source

ANY-INTEGER

出发顶点的标识符。

target

ANY-INTEGER

到达顶点的标识符。

其中:

ANY-INTEGER:

SMALLINT, INTEGER, BIGINT

结果列

(start_vid, end_vid, agg_cost) 的集合

类型

描述

start_vid

BIGINT

起始顶点的标识符。

end_vid

BIGINT

结束顶点的标识符。

agg_cost

FLOAT

start_vidend_vid 的总成本。

Note

当 start_vid 或 end_vid 列具有负值时,标识符用于点。

其他示例

Points SQL 中使用 pgr_findCloseEdges

求从顶点 1 到图中点 (2.9, 1.8) 的两个最近位置的路线成本。

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)

  • 1(2.9,1.8) 最近的边。

  • 2(2.9,1.8) 的下一个闭合边。

  • 靠近图表并不意味着路线更短。

右侧驾驶拓扑

从点 1 和顶点 5 出发,前往点 {2,3,6} 和顶点 {10,11}

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)

左侧驾驶拓扑

从点 1 和顶点 5 出发,前往点 {2,3,6} 和顶点 {10,11}

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)

与驱动端驱动拓扑无关

从点 1 和顶点 5 出发,前往点 {2,3,6} 和顶点 {10,11}

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)

另请参阅

索引和表格