Skip to content

Commit

Permalink
fixed pgRouting#392 temporarily
Browse files Browse the repository at this point in the history
  • Loading branch information
geosanak committed Aug 20, 2015
1 parent 6ad3d95 commit 5f7bfbb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 97 deletions.
99 changes: 52 additions & 47 deletions src/trsp/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Name
Synopsis
-------------------------------------------------------------------------------

The turn restricted shorthest path (TRSP) is a shortest path algorithm that can optionally take into account complicated turn restrictions like those found in real work navigable road networks. Performamnce wise it is nearly as fast as the A* search but has many additional features like it works with edges rather than the nodes of the network. Returns a set of :ref:`pgr_costResult <type_cost_result>` (seq, id1, id2, cost) rows, that make up a path.
The turn restricted shorthest path (TRSP) is a shortest path algorithm that can optionally take into account complicated turn restrictions like those found in real work navigable road networks. Performamnce wise it is nearly as fast as the A* search but has many additional features like it works with edges rather than the nodes of the network. Returns a set of :ref:`pgr_costResult <type_cost_result>` (seq, id1, id2, cost) rows or a set of :ref:`pgr_costResult3 <type_cost_result3>` (seq, id1, id2, id3, cost) rows, that make up a path.

.. code-block:: sql
Expand All @@ -46,13 +46,13 @@ The turn restricted shorthest path (TRSP) is a shortest path algorithm that can
.. code-block:: sql
pgr_costResult[] pgr_trsp(sql text, vids integer[],
pgr_costResult3[] pgr_trsp(sql text, vids integer[],
directed boolean, has_reverse_cost boolean
[, turn_restrict_sql text]);
.. code-block:: sql
pgr_costResult[] pgr_trsp(sql text, eids integer[], pcts float8[],
pgr_costResult3[] pgr_trsp(sql text, eids integer[], pcts float8[],
directed boolean, has_reverse_cost boolean
[, turn_restrict_sql text]);
Expand Down Expand Up @@ -149,12 +149,13 @@ Another variant of TRSP allows to specify **EDGE id** together with a fraction t
:eids: ``int4`` An ordered array of **EDGE id** that the path has to traverse
:pcts: ``float8`` An array of fractional positions along the respective edges in ``eids``, where 0.0 is the start of the edge and 1.0 is the end of the eadge.

Returns set of :ref:`type_cost_result`:
Returns set of :ref:`pgr_costResult3[] <type_cost_result3>`:

:seq: row sequence
:id1: node ID
:id2: edge ID (``-1`` for the last row)
:cost: cost to traverse from ``id1`` using ``id2``
:id1: path ID
:id2: node ID
:id3: edge ID (``-1`` for the last row)
:cost: cost to traverse from ``id2`` using ``id3``


.. rubric:: History
Expand Down Expand Up @@ -230,65 +231,69 @@ An example query using vertex ids and via points:

.. code-block:: sql
select * from pgr_trsp(
'select eid as id, source::integer, target::integer,cost,
reverse_cost from edges1',
SELECT * FROM pgr_trsp(
'SELECT eid AS id, source::integer, target::integer, cost,
reverse_cost FROM edges1',
ARRAY[1,8,13,5]::integer[], -- array of vids
true, -- directed graph?
true, -- has_reverse_cost?
-- include the turn restrictions
'select to_cost, teid as target_id, feid ||
coalesce('',''||via,'''') as via_path from restrictions1');
seq | id1 | id2 | cost
-----+-----+-----+------
1 | 1 | 1 | 1
2 | 2 | 4 | 1
3 | 7 | 8 | 1
4 | 8 | 8 | 1
5 | 7 | 10 | 1
6 | 10 | 14 | 1
7 | 13 | 14 | 1
8 | 10 | 10 | 1
9 | 7 | 7 | 1
10 | 6 | 6 | 1
4 | 5 | -1 | 0
(11 rows)
'SELECT to_cost, teid AS target_id, feid ||
coalesce('',''||via,'''') AS via_path FROM restrictions1');
seq | id1 | id2 | id3 | cost
-----+-----+-----+-----+------
0 | 0 | 1 | 1 | 1
1 | 0 | 2 | 4 | 1
2 | 0 | 7 | 8 | 1
3 | 0 | 8 | -1 | 0
4 | 1 | 8 | 8 | 1
5 | 1 | 7 | 10 | 1
6 | 1 | 10 | 14 | 1
7 | 1 | 13 | -1 | 0
8 | 2 | 13 | 14 | 1
9 | 2 | 10 | 10 | 1
10 | 2 | 7 | 7 | 1
11 | 2 | 6 | 6 | 1
12 | 2 | 5 | -1 | 0
(13 rows)
An example query using edge ids and vias:

.. code-block:: sql
select * from pgr_trsp(
'select eid as id, source::integer, target::integer,cost,
reverse_cost from edges1',
SELECT * FROM pgr_trsp(
'SELECT eid AS id, source::integer, target::integer, cost,
reverse_cost FROM edges1',
ARRAY[1,11,6]::integer[], -- array of eids
ARRAY[0.5, 0.5, 0.5]::float8[], -- array of pcts
true, -- directed graph?
true, -- has_reverse_cost?
-- include the turn restrictions
'select to_cost, teid as target_id, feid ||
coalesce('',''||via,'''') as via_path from restrictions1');
seq | id1 | id2 | cost
-----+-----+-----+------
0 | -1 | 1 | 0.5
1 | 2 | 4 | 1
2 | 7 | 8 | 1
3 | 8 | 11 | 1
1 | 11 | 13 | 1
2 | 12 | 15 | 1
3 | 9 | 9 | 1
4 | 8 | 8 | 1
5 | 7 | 7 | 1
6 | 6 | 6 | 0.5
(10 rows)
'SELECT to_cost, teid AS target_id, feid ||
coalesce('',''||via,'''') AS via_path FROM restrictions1');
seq | id1 | id2 | id3 | cost
-----+-----+-----+-----+------
0 | 0 | -1 | 1 | 0.5
1 | 0 | 2 | 4 | 1
2 | 0 | 7 | 8 | 1
3 | 0 | 8 | 11 | 0.5
4 | 1 | -1 | 11 | 0.5
5 | 1 | 11 | 13 | 1
6 | 1 | 12 | 15 | 1
7 | 1 | 9 | 9 | 1
8 | 1 | 8 | 8 | 1
9 | 1 | 7 | 7 | 1
10 | 1 | 6 | 6 | 0.5
(11 rows)
The queries use the :ref:`sampledata` network.
The queries use the :ref:`sampledata` or ``src/trsp/test/trsp-any-00.data`` network.


See Also
-------------------------------------------------------------------------------

* :ref:`type_cost_result`
* :ref:`type_cost_result3`
59 changes: 19 additions & 40 deletions src/trsp/sql/routing_trsp_vias.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create or replace function pgr_trsp(sql text, vids integer[], directed boolean, has_reverse_cost boolean, turn_restrict_sql text DEFAULT NULL::text)
RETURNS SETOF pgr_costresult AS
RETURNS SETOF pgr_costresult3 AS
$body$
/*
* pgr_trsp(sql text, vids integer[], directed boolean, has_reverse_cost boolean, turn_restrict_sql text DEFAULT NULL::text)
Expand All @@ -12,32 +12,23 @@ $body$
declare
i integer;
rr record;
lrr record;
lrra boolean := false;
seq integer := 0;
res pgr_costresult3;

begin
-- loop through each pair of vids and compute the path
for i in 1 .. array_length(vids, 1)-1 loop
for rr in select * from pgr_trsp(sql, vids[i], vids[i+1], directed, has_reverse_cost, turn_restrict_sql) loop
-- filter out the individual path ends except the last one
-- we might not want to do this so we can know where the via points are in the path result
-- but this needs more thought
--raise notice 'rr: %', rr;
if rr.id2 = -1 then
lrr := rr;
lrra := true;
else
seq := seq + 1;
rr.seq := seq;
return next rr;
end if;
res.seq := seq;
res.id1 := i - 1;
res.id2 := rr.id1;
res.id3 := rr.id2;
res.cost := rr.cost;
seq := seq + 1;
return next res;
end loop;
end loop;

if lrra then
return next lrr;
end if;
return;
end;
$body$
Expand All @@ -51,7 +42,7 @@ $body$
----------------------------------------------------------------------------------------------------------

create or replace function pgr_trsp(sql text, eids integer[], pcts float8[], directed boolean, has_reverse_cost boolean, turn_restrict_sql text DEFAULT NULL::text)
RETURNS SETOF pgr_costresult AS
RETURNS SETOF pgr_costresult3 AS
$body$
/*
* pgr_trsp(sql text, eids integer[], pcts float8[], directed boolean, has_reverse_cost boolean, turn_restrict_sql text DEFAULT NULL::text)
Expand All @@ -65,9 +56,8 @@ $body$
declare
i integer;
rr record;
lrr record;
first boolean := true;
seq integer := 0;
res pgr_costresult3;

begin
if array_length(eids, 1) != array_length(pcts, 1) then
Expand All @@ -77,27 +67,16 @@ begin
-- loop through each pair of vids and compute the path
for i in 1 .. array_length(eids, 1)-1 loop
for rr in select * from pgr_trsp(sql, eids[i], pcts[i], eids[i+1], pcts[i+1], directed, has_reverse_cost, turn_restrict_sql) loop
-- combine intermediate via costs when cost is split across
-- two parts of a segment because it stops it and
-- restarts the next leg also on it
-- we might not want to do this so we can know where the via points are in the path result
-- but this needs more thought
--raise notice 'rr: %', rr;
if first then
lrr := rr;
first := false;
else
if lrr.id2 = rr.id2 then
lrr.cost := lrr.cost + rr.cost;
else
return next lrr;
lrr := rr;
end if;
end if;
res.seq := seq;
res.id1 := i - 1;
res.id2 := rr.id1;
res.id3 := rr.id2;
res.cost := rr.cost;
seq := seq + 1;
return next res;
end loop;
end loop;

return next lrr;

return;
end;
$body$
Expand Down
21 changes: 11 additions & 10 deletions src/trsp/test/trsp_vias-any-04.result
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---------------------------
0|-1|1|0.5
1|2|4|1
2|7|8|1
3|8|11|1
1|11|13|1
2|12|15|1
3|9|9|1
4|8|8|1
5|7|7|1
6|6|6|0.5
0|0|-1|1|0.5
1|0|2|4|1
2|0|7|8|1
3|0|8|11|0.5
4|1|-1|11|0.5
5|1|11|13|1
6|1|12|15|1
7|1|9|9|1
8|1|8|8|1
9|1|7|7|1
10|1|6|6|0.5
---------------------------

0 comments on commit 5f7bfbb

Please sign in to comment.