pgrouting: Using nodes instead of ways

I want to compute the shortest path between two points origin=(34.052299,-118.1812657) destination=(34.141132,-117.853179).

Below is my workflow.

Step 1: Find the nearest nodes.

origin: the following returns 590279764

SELECT id
FROM nodes
ORDER BY st_distance(the_geom, st_setsrid(st_makepoint(-118.1812657, 34.052299), 4326))
LIMIT 1


destination: the following returns 123007528

SELECT id
FROM nodes
ORDER BY st_distance(the_geom, st_setsrid(st_makepoint(-117.853179, 34.141132), 4326))
LIMIT 1


Step 2: Compute shortest path between origin and destination.

SELECT *
FROM pgr_dijkstra(
'SELECT gid as id, source, target, length AS cost FROM ways',
590279764, 123007528, false, false
);


However when I execute this I get the following error.

ERROR:  Start vertex was not found.

********** Error **********

ERROR: Start vertex was not found.
SQL state: XX000


Why is this happening?

1 Answer

You should make sure that you can find your ID either in the source or the target column of your ways table. The ways table is the relevant one where the shortest path algorithms looks for the node ID's.

The nodes table is not necessary for pgRouting.

• Aren't there potentially many 'ways' that contain the ID I'm looking for as either a source or target? Imagine a cross roads. Wouldn't all 4 'ways' that meet at the cross roads contain the same ID as either a source or target? I just care about the shortest path from A to B. So do I have to compute the shortest path from each 'way' that contains A as a source or target to each 'way' that contains B as a source or target and then take the minimum of all these shortest paths? – Cathal Coffey Oct 28 '13 at 5:01
• YES, your node ID can appear multiple times as source or target ID. And NO, if you make a shortest path request, then you will get the shortest path from A to B. Why should you have multiple shortest paths? – dkastl Oct 29 '13 at 15:06
• Because pgrouting does not compute the shortest path from A -> B it computes the shortest path from (the way closest to A) to the way closest to B. The way closest to A and the way closest to B are not guaranteed to be singular. Their may be many ways equal distance to A? – Cathal Coffey Oct 29 '13 at 16:31
• Both, A-Star as well as Dijkstra algorithm compute the shortest path between nodes, not ways. It's probably your application then, that selects the start and end point by the nearest way. – dkastl Oct 29 '13 at 17:19
• I have tried using the id column in the nodes table created by osm2pgrouting but it doesn't work. It does works when I use the source or target column from the ways table. – Cathal Coffey Oct 29 '13 at 18:12