Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgr_withPoints: Additional costs when a POI is on a traversed edge #2254

Closed
NasNasingud opened this issue Feb 28, 2022 · 2 comments
Closed

Comments

@NasNasingud
Copy link

Problem
The function pgr_withPoints does sometimes not output the correct cost. If a POI is located on an edge that is being traversed in the shortest path, the cost from that POI to the end of the node is added on top of the current edge cost.

To Reproduce
Let's produce the most basic graph possible: only one edge with cost 10:

CREATE TEMPORARY TABLE edge_table (id integer, source integer, target integer, cost float);
INSERT INTO edge_table VALUES (1, 10, 11, 10.0);

Further, we need a single POI on that edge:

CREATE TEMPORARY TABLE poi_table (pid integer, edge_id integer, fraction float);
INSERT INTO poi_table VALUES (20, 1, 0.6);

When performing a shortest path query from node 10 to 11, I expect the cost to be 10:

SELECT * FROM   pgr_withPoints(
'SELECT id, source, target, cost FROM edge_table',
'SELECT pid, edge_id, fraction FROM poi_table',
    10,
    11,
    false);

However, the cost is calculated as 14 (obviously 10 + (10 * (1 - 0.6))):

1	10	1	14.0	0.0
2	11	-1	0.0	10.0

The agg_cost is then correct again.

Workaround
When the POI query ignores POI 20, the shortest path is caluclated as expected:

SELECT *
FROM   pgr_withPoints(
'SELECT id, source, target, cost FROM edge_table',
'SELECT pid, edge_id, fraction FROM poi_table WHERE pid != 20',
    10,
    11,
    false);

Result:

1	10	1	10.0	0.0
2	11	-1	0.0	10.0

Platform/versions

SELECT version(); -- PostgreSQL 12.9
SELECT postgis_full_version(); -- POSTGIS="3.0.0 r17983"
SELECT pgr_version(); -- 3.3.0
@cvvergara
Copy link
Member

I just made a PR to develop branch #2257 that I think fixes this issue.
If you want to try that branch do:

git checkout -b cvvergara-new-pgr_trspVia_withPoints develop
git pull https://github.com/cvvergara/pgrouting.git new-pgr_trspVia_withPoints

Or wait until it is merged to develop.
note that the fix wont be available yet on pgRouting version 3.3.

@cvvergara
Copy link
Member

Never mind, I just ran your example on the code of the PR. And I am getting the result you expect.
Will create a pgtap test of this issue.

sampledata=# CREATE TEMPORARY TABLE edge_table (id integer, source integer, target integer, cost float);
CREATE TABLE
sampledata=# INSERT INTO edge_table VALUES (1, 10, 11, 10.0);
INSERT 0 1
sampledata=# CREATE TEMPORARY TABLE poi_table (pid integer, edge_id integer, fraction float);
CREATE TABLE
sampledata=# INSERT INTO poi_table VALUES (20, 1, 0.6);
INSERT 0 1
sampledata=# SELECT * FROM   pgr_withPoints(
sampledata(# 'SELECT id, source, target, cost FROM edge_table',
sampledata(# 'SELECT pid, edge_id, fraction FROM poi_table',
sampledata(#     10,
sampledata(#     11,
sampledata(#     false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |   10 |    1 |   10 |        0
   2 |        2 |   11 |   -1 |    0 |       10
(2 rows)
sampledata=# SELECT *
sampledata-# FROM   pgr_withPoints(
sampledata(# 'SELECT id, source, target, cost FROM edge_table',
sampledata(# 'SELECT pid, edge_id, fraction FROM poi_table WHERE pid != 20',
sampledata(#     10,
sampledata(#     11,
sampledata(#     false);
 seq | path_seq | node | edge | cost | agg_cost 
-----+----------+------+------+------+----------
   1 |        1 |   10 |    1 |   10 |        0
   2 |        2 |   11 |   -1 |    0 |       10
(2 rows)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants