4

I'm trying to use pgrouting pgr_trsp function but result is useless. Exactly same code with pgr_dijkstra working OK and find shortest path between start and stop but with pgr_trsp it is look like random jumps around map.

I'm using this code

SELECT a.seq, a.id1 AS node, a.id2 AS to, a.cost, cr_2po_4pgr.osm_name, ST_AsText(ST_GeomFromEWKB(decode(cr_2po_4pgr.the_geom,'hex'))) as geodata FROM pgr_dijkstra ('SELECT gid as id, source, target, cost FROM cr_2po_4pgr', $start,$end,false, false) as a left join cr_2po_4pgr on a.id2 = cr_2po_4pgr.gid

Based on documentation trsp should be almost same as dijkstra but i can use turn restriction code. But even basic is not working. What i'm doing wrong ?

Matus
  • 77
  • 6
  • GREAT EXAMPLE! That´s what we need! Is it necessary to create the restriction table? Is it possible to have the restrictions in the same table? –  May 08 '14 at 12:35
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. – radouxju May 08 '14 at 13:12

1 Answers1

7

You're probably confusing node ids with edges ids (the algorithm works with edges rather than nodes).

Anyway, I think the documentation is not really good at that example. Try this instead:

Create the restriction table this way:

CREATE TABLE restrictions (
        rid serial,
        to_cost double precision,
        target_id integer,
        via_path text
    );

INSERT INTO restrictions VALUES (1, 1000, 11, '8,4,1');

You have the sample network here. So, that restriction says: if you come along edges 1 - 4 - 8, then you can pass trough edge 11 only with a cost of 1000.

That implies that, for example, if you followed the nodes 1 - 2 - 5 - 6, then going to node 11 will cost 1000 (I want to avoid that turn).

Now, try this query (no turn restrictions)

SELECT seq, id1 AS node, id2 AS edge, cost
FROM pgr_trsp(
        'SELECT id, source, target, cost FROM edge_table',
        1, 11, false, false
);

You will get the node path 1 - 2 - 5 - 6 - 11.

If you apply the turn restriction's table, the step from node 6 to node 11 will be really expensive. So, the algorithm will avoid it. Let's try it

SELECT seq, id1 AS node, id2 AS edge, cost
FROM pgr_trsp(
        'SELECT id, source, target, cost FROM edge_table',
        1, 11, false, false,
        'SELECT to_cost, target_id, via_path FROM restrictions'
);

And the node path will be 1 - 2 - 5 - 10 - 11

Jorge Arévalo
  • 1,739
  • 2
  • 16
  • 32