简体   繁体   中英

How do I calculate shortest path between Origin - Destination pairs using pgRouting?

First of all I just want to state that I'm very new to GIS and that I'm probably not that great at the terminology yet, so bear with me.

I'm having my internship right now and have been tasked with making a bike commuting potential analysis. The data I'm using is road layer (which I have already created a topology for using pgr_createTopology) and two point layers for where individuals live and work created from the centroids of 500x500m squares.

I have managed to do some sort of calculation between my two point layers using pgr_dijkstraCost that looks like this:

SELECT * 
  FROM pgr_dijkstraCost(
        'SELECT gid AS id,
                source,
                target,
                extlen / 1.3 / 60 AS cost 
           FROM roads',
        array(select source FROM living),
        array(select target FROM work),
        directed := false);

The source and target value in the living and work test tables has a value from 1 to 50 since I initially though that I could make the calculation by calculating when source and target has the same value. I now know that's not possible since pgr_dijkstra wont allow calculations when they are the same. The result I'm getting right now is for every combination I don't want. The final calculation will be for around 300 000 pairs.

So is there a way for me to only do the calculation on specified pairs and not for every possible combination?

Starting from Version 3.1 there is this signature

pgr_dijkstra(Edges SQL, Combinations SQL, end_vids, [, directed])
RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost)
OR EMPTY SET

example usage (taken from the pgRouting documentation)

CREATE TABLE combinations_table (
source BIGINT,
target BIGINT
);

INSERT INTO combinations_table (source, target) 
VALUES (1, 2), (1, 4), (2, 1), (2, 4), (2, 17);

SELECT * FROM pgr_dijkstra(
    'SELECT id, source, target, cost, reverse_cost FROM edge_table',
    'SELECT * FROM combinations_table',
    FALSE
);


seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost
----+----------+-----------+---------+------+------+------+----------
 1  |        1 |         1 |       2 |    1 |    1 |    1 |        0
 2  |        2 |         1 |       2 |    2 |   -1 |    0 |        1
 3  |        1 |         1 |       4 |    1 |    1 |    1 |        0
 4  |        2 |         1 |       4 |    2 |    2 |    1 |        1
 5  |        3 |         1 |       4 |    3 |    3 |    1 |        2
 6  |        4 |         1 |       4 |    4 |   -1 |    0 |        3
 7  |        1 |         2 |       1 |    2 |    1 |    1 |        0
 8  |        2 |         2 |       1 |    1 |   -1 |    0 |        1
 9  |        1 |         2 |       4 |    2 |    2 |    1 |        0
10  |        2 |         2 |       4 |    3 |    3 |    1 |        1
11  |        3 |         2 |       4 |    4 |   -1 |    0 |        2
(11 rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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