Professional Documents
Culture Documents
A Beginner's Guide To PgRouting - Free and Open Source GIS Ramblings
A Beginner's Guide To PgRouting - Free and Open Source GIS Ramblings
A Beginner's Guide To PgRouting - Free and Open Source GIS Ramblings
Installing pgRouting
pgRouting can be downloaded from www.pgrouting.org (http://www.pgrouting.org/download.html).
Building from source is covered by pgRouting documentation
(http://www.pgrouting.org/docs/1.x/install.html). If youre using Windows, download the binaries and copy
the .dlls into PostGIS lib folder, e.g. C:\Program Files (x86)\PostgreSQL\8.4\lib.
Start pgAdmin and create a new database based on your PostGIS template. (I called mine
routing_template.) Open a Query dialog, load and execute the three .sql files located in your pgRouting
download (routing_core.sql, routing_core_wrappers.sql, routing_topology.sql). Congratulations, you now
have a pgRouting-enabled database.
1/18
8/6/12
Now, we create a table containing all the unique network nodes (start and end points) and well also give
them an id:
1 CREATE TABLE node AS
2
SELECT row_number() OVER (ORDER BY foo.p)::integer AS id,
3
foo.p AS the_geom
4
FROM (
5
SELECT DISTINCT road_ext.startpoint AS p FROM road_ext
6
UNION
7
SELECT DISTINCT road_ext.endpoint AS p FROM road_ext
8
) foo
9
GROUP BY foo.p;
Finally, we can combine our road_ext view and node table to create the routable network table:
1 CREATE TABLE network AS
2
SELECT a.*, b.id as start_id, c.id as end_id
3
FROM road_ext AS a
4
JOIN node AS b ON a.startpoint = b.the_geom
5
JOIN node AS c ON a.endpoint = c.the_geom;
2/18
8/6/12
09 false,
10 false);
(http://underdark.files.wordpress.com/2011/02/pgrouting1.png)
Route from node #1 to node #5110
For further pgRouting-related posts check my list of pgRouting posts
(http://underdark.wordpress.com/tag/pgrouting/).
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
3/18
8/6/12
4/18
8/6/12
5/18
8/6/12
That being said, if you just need to model which method (bus vs train) is more efficient, combining the
2 layers into 1 as underdark stated could be used by then adding different cost weightings on the ares
that effectively says its faster to take a train than a bus. Then theoretically starting at Point A you
would take the bus to the closest train terminal, ride the train, stop at the next train station, then take
the bus to your destination.
As underdark stated, you would have to take extra care to deal with bus route/train route node
intersections that were not actually stops.
RJay 2011-04-12 at 11:11
Thank you both. I was totally stuck. But now I can give this a try.
4. Robert 2011-04-25 at 17:16
I keep getting an error when trying to run shortest distance. Im on pgsql 8.4.8.
ERROR: function shortest_path(unknown, integer, integer, boolean, boolean) does not exist
LINE 1: SELECT * FROM shortest_path(
^
HINT: No function matches the given name and argument types. You might need to add explicit type
casts.
underdark 2011-04-25 at 20:14
Could you post the full query?
5. Pingback: Catchment Areas with pgRouting driving_distance() | Free and Open Source GIS Ramblings
6. Kirill Blajko 2011-05-18 at 16:11
Hello, I have the same problem!
SELECT * FROM shortest_path(
SELECT gid as id,
source::integer,
target::integer,
length::double precision as cost
FROM ways,
605, 359, false, false);
returns
SQL error:
ERROR: syntax error at or near SELECT
LINE 2: SELECT gid as id,
and if I change the whole sub-select
SELECT gid as id, source::integer, target::integer, length::double precision as cost FROM ways
with just 1, the error is
ERROR: function shortest_path(integer, integer, integer, boolean, boolean) does not exist
LINE 1: SELECT * FROM shortest_path(1,
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
6/18
8/6/12
7/18
8/6/12
8/18
8/6/12
Id guess that you are trying to route to a node which does not exist in your graph. Check which node
id you chose as a target of the route.
13. Sandeep 2011-09-07 at 15:20
Hi,
Thanks for the wonderful post. It was really useful.
But i have a road network shape file whose poly lines are very long and not from junctions to junction. So
even if a long road intersects many roads, it will have only one start_id and end_id. How do i consider the
intersections in the middle. Do i have to split the poly line at the junctions? Please help
underdark 2011-10-06 at 14:37
Hi Sandeep,
Yes, you have to split the lines at the junctions. One way to do this is to use GRASS GIS clean
topology.
14. Ariel 2011-10-06 at 14:00
Hi, thanks for this very helpful tutorial. After doing every thing you posted, i finnish with a lot of doubts
because the shortest_path function returns me an error Target vertex was not found.. When i made a
hand test, connecting by myself the result set of roads, i realized that the nodes table has his own ID
column (related with the row number) wich is different of the original ID of the rows fields. So for example
a street in Buenos aires argentina the end_id was related with a street in Cordoba (800 km of distance
between them). For that reason i need some help to understand how to connect them.
Thanks
Ariel
15. Peter Granberg 2011-10-07 at 13:23
Hi!
Can PGrouting handle Turn Restrictions? And do PGrouting has logic for handle vehicle types restrictions,
eg. this road segment is bus and taxi-only?
Thanks
/Peter
underdark 2011-10-07 at 13:32
Hi Peter,
Both should be possible. While simple Dijkstra doesnt handle turn restrictions, there are other
algorithms available that can handle turn restrictions. For bus or taxi-only segments, it should be
possible to handle that when selecting the network for the routing function.
16. Nick 2011-10-08 at 07:56
This article has gotten me ALOT closer, however I am still missing a critical step somewhere. I do not have
a shape_leng value in a table. Where did ways come from? Is that the table from the shp2pgql import?
Once i have that, then i think that shortest_path will work. Then my next step is how to use lat/lon pairs to
specify start and end to calc the distance. Eventually i want to use driving directions. Any suggestions are
much appreciated!
underdark 2011-10-08 at 09:31
The shape_leng is an attribute of the dataset I used in this example. Your data will have different
cost attributes but if its road network data, there should be some kind of road length value.
The post doesnt mention any ways table. The shp2pgsql import is called road in this example. (If
you are referring to the comments, some users seem to have called their import ways.)
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
9/18
8/6/12
For your next step: So far, I used a method of finding the closest network node to a given set of
coordinates to get start and end node for the routing.
Nick 2011-10-08 at 16:33
Hi underdark- thank you for your reply. I apologize for the basic questions, but where does this
shape_leng / road length value come from? Is this something that i have to run a specific sql
query with postgis functions on? Is there an example somewhere?
I followed the post, however i am running in ubuntu, and dont have PostGIS Manager application
to load the data. In the post, there are 3 queries to run, the second creating network table, but
after running that i do not have a length column, so the 3rd query for shortest_path fails.
underdark 2011-10-08 at 17:15
shape_leng is already contained in the original Shapefile downloadable from National Land
Survey of Finland. If you are using a different dataset for this tutorial, you have to create your ownl
length column. You can calculate the length of a line using PostGIS function ST_Length().
PostGIS Manager is a QGIS Plugin. You can install it in Ubuntu.
Nick 2011-10-08 at 17:59
Yes, i am from the US, so i wanted to use census Tiger shapefiles. I am able to run ST_Length,
however per the documentation this says that it returns 2D length in spatial reference units. How
can i convert these into feet / miles / m / km?
Regarding the QGIS, i am running a headless server. I dont need the visuals, but i guess they
could help.
Nick 2011-10-08 at 18:01
Tiger shapefiles claim to be NAD83 (SRID=4269)
underdark 2011-10-08 at 18:39
The TIGER file I just downloaded contains an attribute called LENGTH. I assume they all look
same. Just use that.
Nick 2011-10-08 at 19:35
Which tiger data did you use? I have the 2010st data (.shp, .prj, etc). Specifically i have arbitrarily
selected: tl_2010_17023_roads to work with. When using the shp2pgsql, i do not get Length. I do
have linearid. Sorry for the basic questions, i am stuck here though. What method are you using
to see the Length attribute in the tiger data? Can you send me the name of the file you are
using?
underdark 2011-10-08 at 19:58
I downloaded a file from http://arcdata.esri.com/data/tiger2000/tiger_download.cfm
and checked available attributes using QGIS. Maybe you should run your first tests on a desktop
system before migrating to a headless server.
17. Joe Larson 2011-10-27 at 22:32
forgot closing semi-colon on create the routable network table code snippet. Feel free to delete this post.
underdark 2011-10-28 at 13:50
Thanks Joe,
I added all missing semi-colons.
18. Qiki (@qikialg) 2011-11-10 at 10:44
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
10/18
8/6/12
11/18
8/6/12
21. Pingback: A First Glimpse at pgRouting Layer for QGIS | Free and Open Source GIS Ramblings
22. Etienne DELAY (@ElCep) 2011-12-05 at 22:35
Thank you very much for this article! It allowed me to start. Can I draw your article to write in French?
That can be helpfull for some french people!!!
thank you
underdark 2011-12-06 at 11:26
Feel free to translate any of my posts into French. (For a list of all pgRouting-related posts see
http://underdark.wordpress.com/tag/pgrouting/.) I would just ask you to provide a link back to the
original post.
Etienne DELAY (@ElCep) 2011-12-07 at 01:15
yes of course :)
23. huma saffdar 2011-12-06 at 06:57
i have got a problem in running Dijkstra
SELECT * FROM shortest_path(
SELECT gid as id,
start_id::int4 AS source,
end_id::int4 AS target,
FROM network,
605, 359, false, false);
ERROR: syntax error at or near FROM
LINE 5: FROM network
^
i have got there error
what do i do?
underdark 2011-12-06 at 11:29
There must be no trailing comma after the last field name. So, no comma after target.
You will also need a cost field. It is mandatory.
24. sarah 2011-12-06 at 07:01
I download a data from osm and it does not contain length attribute so how can i run this algorithm
underdark 2011-12-06 at 11:33
You could use ST_Length() to calculate the length of the road in PostGIS.
25. Pingback: l'amnagerie Blog Archive Utilisation de pgRouting
26. Sarah 2011-12-11 at 15:11
SELECT assign_vertex_id(Roads, 0.00001, the_geom, gid);
when i run this query i got this error
NOTICE: CREATE TABLE will create implicit sequence vertices_tmp_id_seq for serial column
vertices_tmp.id
CONTEXT: SQL statement CREATE TABLE vertices_tmp (id serial)
PL/pgSQL function assign_vertex_id line 14 at EXECUTE statement
ERROR: query string argument of EXECUTE is null
CONTEXT: PL/pgSQL function assign_vertex_id line 20 at EXECUTE statement
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
12/18
8/6/12
13/18
8/6/12
14/18
8/6/12
Network. Im trying to create a network of the spanish roads, the shp file size was 220MB, I loaded it
successfully to my db (after doing all steps described in your tutorial) and when I excecuted the Create
statement for table network. The query started and It has been working for almost 15 hours long. Im not
sure if this is wrong (even though I believe it is) What do you think? should I stop the process and start
again? or Should I leave it workign for the weekend and maybe it will be finished on monday morning.
Thank you
underdark 2012-01-20 at 14:17
Considering the size of your file it could well take more than 15 hours. Id leave it running until
Monday. If it hasnt successfully completed until then, please look into assign_vertex_id() function
which should be able to do the same thing.
Santiago 2012-01-20 at 14:36
Thak you very much for your promtly response. I will leave this working until monday and let you
know as soon it is finished (or not hehehe). Also I want to tell you that probably Ill be using your
post A Closer Look at Alpha Shapes in pgRouting into a project in which I need to find some
influence areas within some boundaries around a point.
Your doing a terrific job here. !!!
Cheers
34. stefano 2012-01-20 at 22:27
Hi,
Im trying to follow your tutorial but Im new to pgRouting and open source GIS in general, then Im
having some dufficulties
What exactly is the road network shapefile from National Land Survey of Finland (NLS) that you used in
this guide?
thanks
Stefano
underdark 2012-01-21 at 12:10
You can download the road network shapefile from the NLS website mentioned above. Any other oad
network shapefile will do as well. Some probably even have start and end nodes defined, then you can
skip the first part.
35. James David Smith 2012-01-27 at 18:35
Hi underdark,
Not really a problem as such, more a comment and thanks. After being stubborn and looking for workarounds for a while, I have finally uninstalled PostgresSQL 9.0 and gone back to 8.4 so that I can use
PGRouting and have managed to get it all set-up I think after following your tutorial.
Instead of using the sample data however, I decided to use a Shapefile of RoadLinks for London.
I first download the Integrated Transport Layer from Ordnance Survey:
http://www.ordnancesurvey.co.uk/oswebsite/products/os-mastermap/itn-layer/index.html
Then I prepared it for use in ArcGIS using the ESRI Productivity Suite and ESRI White Paper:
http://www.esriuk.com/literature/documents/ITN_wp.pdf
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
15/18
8/6/12
The result being I had a Network_Dataset that I could use with the Network Analyst tool within ArcGIS.
However I wanted to do things that cannot be done in ArcGIS so I am now trying to move this network
into PGRouting. So what I did was I loaded the Network Dataset into ArcGIS and then exported the part
of the network file called RoadLinks as a spapefile. The columns in this table were as follows:
Label
Node1
Node2
TurnRestrictions
OneWay
EdgeLength
and a few others.
So having exported this as a shape, I then used the PostGIS ShapeLoader to load it into a table that I had
created using the routing_template table from your tutorial. I then earlier today did the Start and End
Geometries query as well as the Unique Network Nodes query and both seemed to execute properly. I
am now running the Create Network query and waiting for the results. It had been about an hour so far,
but as the original shapefile was around 330MB I expect that I might have to come back on Monday to see
how it has gone.
Any thoughts/hints/tips on what I have done so far?
Thanks again,
James
underdark 2012-01-27 at 20:34
Hi James,
Is Node1 the starting nodes id and Node2 the end nodes id? If yes, you dont need to create the
network from scratch. Everything you need is already there and you can start routing.
What end result are you going for?
Best wishes,
Anita
James David Smith 2012-01-29 at 14:27
Hi Anita.
Thanks for the reply. I didnt apprecaite about not needing to build from scratch. I set it going as I
left work on Friday and wont be back there until Monday morning so I will stop it then if it hasnt
finished already. Building the network from scratch wont have done any harm though I presume?
It will essentially just have re-recreated the network again yes?
End result? Long story, but initally I want to do some routing and the output to be quite detailed,
which I dont get in ArcGIS. What I mean is that I want the result of my query from point a to
point b to be broken down and show the length of each road section that was travelled upon. So
depart from point a, travel 15 metres along road id 1, 20 metres along road id 3, 150 metres along
road id 3, arrive at point b. And to store each of these bits as differnt rows in a new table along
with a journey ID or something like that so that I know that they are part of the same journey.
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
16/18
8/6/12
Then to repeat this a few thousand times. I will then bring in some data from another table about
the amount of air pollution on each of those roads so that I can calculate how much air pollution
someone has been exposed to in each journey.
THanks
James
James Smith 2012-02-06 at 17:32
Hi Anita,
If I dont bother with creating the network from scrath and just use the table I already have (with
Node1, Node 2 as the start and end nodes) then when I try and run this query I get the error
message ERROR: value 4000000023178687 is out of range for type integer. I understand why
this is, its that the data in my Node column isnt suitable for converstion to a Int4 data type. But
Im not sure how to fix this. Any ideas?
SELECT * FROM shortest_path(
SELECT gid AS id,
node1::int4 AS source,
node2::int4 AS target,
shape_leng::float8 AS cost
FROM londonroadlinks_n,
1,
5110,
false,
false);
THanks
James
underdark 2012-02-06 at 19:42
Looks like you have to recreate int4 ids for the nodes. Which range do the node ids currently cover?
Maybe it can be as simple as subtracting a certain value.
James Smith 2012-02-07 at 00:01
Hi Anita. Yes I think that you might be right. The nodes are numbers like 400000046578 and
400000046579 etc. I guess I might create two new columns like Node1_new and Node 2_new and
insert into them the existing Node IDs but with 400000000000 removed from the number. If I did
this do I then need to repeat the CREATE VIEW and CREATE TABLE node SQL statements?
Thanks
James
underdark 2012-02-07 at 09:11
You dont need to run either of those queries. The goal of these queries is to create node IDs, but
you already have suitable node IDs (if you substract 400000000000).
36. Paritosh Kowli 2012-02-01 at 12:32
Can you help me out if i have 3 points(startpoint,endpoints and viapoint(midpoint)) how to route this and
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
17/18
8/6/12
find driving distance for same. And via point depends on me.
underdark 2012-02-01 at 17:08
I guess you could split it up into two separate routing operations.
Paritosh Kowli 2012-02-02 at 05:34
Can explain me in brief(i.e. Can you help me out if i have 3 points(startpoint,endpoints and
viapoint(midpoint)) how to route this and find driving distance for same. And via point depends on
me.)
37. underdark 2012-02-07 at 09:14
Comments on this post are disabled now.
Please use http://gis.stackexchange.com for questions.
Comments are closed.
underdark.wordpress.com/2011/02/07/a-beginners-guide-to-pgrouting/
18/18