在一个查询中使用PgRouting获得多个最短路径?


12

我想一次在多个源和目标对上运行最短路径算法,并获得结果表,然后对其进行处理。

我该怎么做呢?该查询不起作用:

SELECT a.source, a.target, paths.* 
FROM all_to_all a, shortest_path_astar('...', a.source, a.target, false, false) paths;

ERROR:  function expression in FROM cannot refer to other relations of same query level

(顺便说一句,all_to_all并不是字面意思,:)只是一些随机对。

这也不起作用:

SELECT * 
FROM all_to_all a, (
   SELECT * FROM shortest_path_astar('...', a.source, a.target, false, false) yyy
) AS t2;

----请您详细说明一下吗?我有同样的问题,但不能正确地获得这些对?(摘自
这篇

Answers:


13

就像是

SELECT 
  source, 
  target,
  (SELECT SUM(cost) FROM  -- or whatever you want to do with the routing result
     (SELECT * FROM shortest_path_astar('...',
       source,
       target,
       false,
       false)
     ) AS foo 
  ) AS cost
FROM all_to_all;

4

这是一个查询,它返回所有源-目标组合的所有段:

SELECT
    source,
    target,
    shortest_path_astar('SELECT gid AS id, length AS cost, * FROM ways', source, target, false, false) AS segment
FROM
    all_to_all

难以置信,与SQL语法不一致,但是可以!

source | target | segment
-------+--------+----------------
     1 |      4 | (1, 2, 0.1357)
     1 |      4 | (2, 3, 0.2468)
     1 |      4 | (3, 4, 0.9)
     1 |      4 | (4, -1, 0)
other sources & targets here
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.