简体   繁体   English

如何获得pgr_dijkstra灌浆中节点序列的距离?

[英]how to get the distance of sequence of nodes in pgr_dijkstra pgrouting?

I have an array of integers(nodes or destinations) ie array[2,3,4,5,6,8] that need to be visited in the given sequence. 我有一个整数数组(节点或目标),即需要按给定序列访问的array [2,3,4,5,6,8]。 What I want is, to get the shortest distance using pgr_dijkstra. 我想要的是使用pgr_dijkstra获得最短的距离。 But the pgr_dijkstra finds the shortest path for two points, therefore I need to find the distance of each pair using pgr_dijkstra and adding all distances to get the total distance. 但是pgr_dijkstra找到了两个点的最短路径,因此我需要使用pgr_dijkstra来找到每对的距离,并将所有距离相加以获得总距离。 The pairs will be like 两人会像
2,3 2,3
3,4 3,4

4,5 4,5

5,6 5,6

6,8. 6,8。 Is there any way to define a function that takes this array and finds the shortest path using pgr_dijkstra. 有什么方法可以定义使用此数组并使用pgr_dijkstra找到最短路径的函数。

Query is: for 1st pair(2,3) SELECT * FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads',2,3, false); 查询是: 对于第一对(2,3) SELECT * FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads',2,3, false);

for 2nd pair(3,4) SELECT * FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads'***,3,4,*** false) 对于第二对(3,4) SELECT * FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads'***,3,4,*** false)

for 3rd pair(4,5) SELECT * FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads'***,4,5,*** false) ; 对于第三对(4,5) SELECT * FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads'***,4,5,*** false) ;

NOTE: The array size is not fixed, it can be different. 注意:数组大小不是固定的,可以不同。

Is there any way to automate this in postgres sql may be using a loop etc? 有什么办法可以自动在postgres sql中使用循环等? Please let me know how to do it. 请让我知道该怎么做。 Thank you. 谢谢。

Using the solution provided in this post that makes use of a source table , it is possible to use your array. 使用此提供的解决方案 ,使得使用源的,可以使用您的阵列。 Note that the orig is your array omitting the last entry, and the dest omits the 1st entry. 请注意, orig是您省略最后一个条目的数组,而dest省略了第一个条目。

SELECT 
  source, 
  target,
  (SELECT SUM(cost) FROM  -- or whatever you want to do with the routing result
     (SELECT * 
      FROM pgr_dijkstra('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads',
                        orig,
                        dest, 
                        false))
     ) AS foo 
  ) AS cost
FROM (
  select unnest(myarray[:array_upper(myarray,1)-1])  as orig,
         unnest(myarray[2:]) as dest 
  from (select array[1,2,3,4] myarray) b) c;

If you want all pairs distance then use 如果您希望所有对都保持距离,请使用

select * from pgr_apspJohnson ('SELECT gid as id,source, target, rcost_len AS cost FROM finalroads) select * from pgr_apspJohnson('SELECT gid as id,source,target,rcost_len AS cost from finalroads)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM