You are on page 1of 20

Workshop: FOSS4G routing with pgRouting tools and OpenStreetMap road data

2009/11/02 and 2009/11/0

FOSS4G routing with pgRouting tools and OpenStreetMap road data


Presenter:

Daniel Kastl

Further authors:

Claude Philipona Frdric Junod Anton Patrushev

Abstract
pgRouting adds routing functionality to PostGI ! "his introductory #or$shop #ill sho# you ho#! It gives a practical e%a&ple of ho# to use pgRouting #ith 'pen treet(ap road net#or$ data! It e%plains the steps to prepare the net#or$ data) &a$e routing *ueries) assign costs to the net#or$ lin$s and &odify your results through #rapper functions!

Description
+avigation for road net#or$s re*uires co&ple% routing algorith&s that support turn restrictions and even ti&e,dependent attri-utes! pgRouting is an e%tendi-le open,source li-rary that provides a variety of tools for shortest path search! "he li-rary in its current version is an e%tension of Postgre ./ and PostGI ! It0s predecessor 1pgDi2$stra1 #as #ritten -y ylvain Pasche fro& Ca&ptoca&p! It #as then e%tended -y 'r$ney 3Japan4 and rena&ed to pgRouting! An introduction #ill give an overvie# of the pro2ect history) develop&ent tea&) infrastructure) productive environ&ents and scope of use! "he #or$shop #ill e%plain a-out shortest path search #ith pgRouting in real road net#or$s and ho# the data structure is i&portant to get faster routing results! Also you #ill learn a-out difficulties and li&itations of i&ple&enting pgRouting functionality in GI applications! "o give a practical e%a&ple of ho# to perfor& shortest,path searches #ith pgRouting) the #or$shop &a$es use of 'pen treet(ap road net#or$ data! "he 'pen treet(ap co&&unity creates their o#n road data that is freely availa-le for a rapidly gro#ing nu&-er of areas! 5e #ill use 'pen treet(ap data of Capeto#n for this #or$shop! 6ou #ill learn ho# to convert the data into the re*uired for&at and ho# to cali-rate the data #ith 1cost1 attri-utes! Further&ore #e #ill e%plain the difference of the three &ain routing algorith&s 1Di2$stra1) 1A, tar1 and 1 hooting, tar1! 7y the end of the #or$shop you #ill have a good understanding of ho# to use pgRouting and ho# to get your net#or$ data prepared! 5hile si&ilar to the last years #or$shop 15e-,-ased Routing: An Introduction to pgRouting #ith 'pen/ayers1) this year0s pgRouting #or$shop #ill 8 in regard to the students feed-ac$ 8 focus on pgRouting itself and net#or$ data issues! 'pen/ayers &ap client #ill still -e used to display the route on a &ap! Due to ti&e li&itation installation of pgRouting is not part of this #or$shop! An installation #ith pgRouting #ill -e provided for you as #ell as the 'pen treet(ap sa&ple data for Capeto#n!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page :=:;

Arramagong Live DVD


Arra&agong is a self,contained live D>D) -ased on ?u-untu <!;9) that allo#s you to try a #ide variety of open source geospatial soft#are #ithout installing anything! It provides effective tools for a range of geospatial use cases! Application pass#ords are in the pass#ords!t%t file on the des$top! (ost of the /ive D>D applications can -e installed on Apple ' ? and (icrosoft 5indo#s as #ell! Arra&agong co&es #ith (acintosh /eopard= no# /eopard and 5indo#s ?P=>ista installers for these applications in the 5indo#sInstallers and (acInstallers folders of the D>D! @o# to get started: A! Insert D>D and re-oot :! Press enter to start up B! At splash screen press Cnter to login 9! 5elco&e to /ive D>D 'ptional:

Change $ey-oard layout to Japanese A;D,$ey Connect to net#or$ If userna&e or pass#ord is re*uired) use

username: user password: user

Get the Source

"he #or$shop docu&entation can -e also found here:


http:==pgrouting!postl-s!org=#i$i=5or$shopF' http:==pgrouting!postl-s!org=#i$i=' ("utorial

9G:;;E

Further informations

pgRouting pro2ect #e-site: http:==pgrouting!postl-s!org

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page B=:;

About pgRouting
pgRouting is an e%tension of PostGI and adds routing functionality to PostGI =Postgre ./! pgRouting is a further develop&ent of pgDi2$stra 3-y Ca&ptoca&p4! It is currently developed and &aintained -y 'r$ney) JAPA+! pgRouting core functionality

hortest Path Di$stra: routing algorith& #ithout heuristics hortest Path A, tar: routing for large datasets 3#ith heuristics4 hortest Path hooting, tar: routing #ith turn restrictions 3#ith heuristics4 "raveling alesperson Pro-le& 3" P4 Driving Distance calculation 3Isolines4

Database routing approach


Accessi-le -y &ultiple clients through JD7C) 'D7C) or directly using Pl=pg ./! "he clients can either -e PCs or &o-ile devices! Fses PostGI for its geographic data for&at) #hich in turn uses 'GC0s data for&at 5ell Kon#n "e%t 35K"4 and 5ell Kno#n 7inary 35K74! "his allo#s usage of e%isting open data converters! 'pen ource soft#are li$e *GI and uDig can &odify the data=attri-utes) Data changes can -e reflected instantaneously through the routing engine! "here is no need for precalculation! "he 1cost1 para&eter can -e dyna&ically calculated through ./ and its value can co&e fro& &ultiple fields or ta-les!

pgRouting pro2ect #e-site: http:==pgrouting!postl-s!org

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page 9=:;

About OpenStreetMap
!OpenStreetMap is a pro"e#t ai$ed s%uarel& at #reating and pro'iding (ree geographi# data su#h as street $aps to an&one who wants the$)! !*he pro"e#t was started +e#ause $ost $aps &ou think o( as (ree a#tuall& ha'e legal or te#hni#al restri#tions on their use, holding +a#k people (ro$ using the$ in #reati'e, produ#ti'e or une-pe#ted wa&s)! G ource: http:==#i$i!openstreet&ap!org=inde%!php=PressH OpenStreetMap uses a topological data structure:

+odes are points #ith a geographic position! 5ays are lists of nodes) representing a polyline or polygon! Relations are groups of nodes) #ays and other relations #hich can -e assigned certain properties! "ags can -e applied to nodes) #ays or relations and consist of na&eIvalue pairs!

"his is ho# nodes) #ays and relations are descri-ed in the 'pen treet&ap ?(/ file:
<?xml version='1.0' encoding='UTF-8'? <osm version='0.!' genera"or='#$%&' ... <node id=''!'()10*(' "imes"amp=''008-0+-18T1):,!:0*-00:00' user='.ussell /loran' visi0le='"rue' la"='-++.)')1*0'' lon='18.,'!18*!' <"ag 1='crea"ed203' v='#$%&' 4 <4node <node id=''!'()10**' "imes"amp=''008-0+-18T1):,!:0!-00:00' user='.ussell /loran' visi0le='"rue' la"='-++.)+0!1(,' lon='18.,'*!(('' <"ag 1='crea"ed203' v='#$%&' 4 <4node <node id=''!'()10*!' "imes"amp=''008-0+-18T1):,!:0,-00:00' user='.ussell /loran' visi0le='"rue' la"='-++.)+0,18' lon='18.,'+1'01' <"ag 1='crea"ed203' v='#$%&' 4 <4node ... <node id=''*0+***,+' "imes"amp=''008-0,-'8T10:!):11-01:00' user='5drian Fri"6' visi0le='"rue' la"='-++.)'8(+1+' lon='18.,1!'!1' <"ag 1='crea"ed203' v='#$%&' 4 <"ag 1='name' v='%ou"6 57rican &useum' 4 <"ag 1='"ourism' v='museum' 4 <4node ... <wa3 id=''*+!8(''' "imes"amp=''008-08-1(T1!:,1:!!-01:00' user='5drian Fri"6' visi0le='"rue' <nd re7=''88(8(*))' 4 <nd re7=''88(8(*)!' 4 <"ag 1='6ig6wa3' v='residen"ial' 4 <"ag 1='name' v='8uinea Fowl /rescen"' 4 <4wa3 <wa3 id=''*+!8('+' "imes"amp=''008-08-1(T1!:,1:!*-01:00' user='5drian Fri"6' visi0le='"rue' <nd re7=''88(8(*)*' 4 <nd re7=''88(8(('+' 4 <nd re7=''88(8((',' 4 <nd re7=''88(8(('8' 4 <nd re7=''88(8(('!' 4 <nd re7=''88(8(*((' 4

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page J=:;

<"ag 1='6ig6wa3' v='residen"ial' 4 <"ag 1='name' v='$ld Farm .oad' 4 <4wa3 ... <rela"ion id=''()',' "imes"amp=''008-08-1(T0):!+:,8-01:00' user='5drian Fri"6' visi0le='"rue' <mem0er "3pe='wa3' re7=',)),+(8' role='ou"er' 4 <mem0er "3pe='wa3' re7='!0'',8,' role='innner' 4 <"ag 1='"3pe' v='mul"ipol3gon' 4 <4rela"ion <rela"ion id='''(1+' "imes"amp=''008-0(-'(T'':,):'!-01:00' user='5drian Fri"6' visi0le='"rue' <mem0er "3pe='wa3' re7=''!81(()*' role='ou"er' 4 <mem0er "3pe='wa3' re7=''!81(()(' role='inner' 4 <"ag 1='"3pe' v='mul"ipol3gon' 4 <4rela"ion ... <4osm

"he ' ( data can -e do#nloaded fro& 'pen treet(ap #e-site using an API 3see http:==#i$i!openstreet&ap!org=inde%!php=' (KProtocolK>ersionK;!D4) or #ith so&e other ' ( tools) for e%a&ple J' ( editor! +ote: "he API has a do#nload siLe li&itation) #hich can &a$e it a -it inconvenient to do#nload e%tensive areas #ith &any features! 5hen using the os&:pgrouting converter 3see later4) #e ta$e only nodes and #ays of types and classes specified in 1&apconfig!%&l1 file to -e converted to pgRouting ta-le for&at:
<?xml version=91.09 encoding=9UTF-89? <con7igura"ion <"3pe name=96ig6wa39 id=919 <class name=9mo"orwa39 id=91019 4 <class name=9mo"orwa32lin19 id=910'9 4 <class name=9mo"orwa32:unc"ion9 id=910+9 4 ... <class name=9road9 id=91009 4 <4"3pe <"3pe name=9:unc"ion9 id=9,9 <class name=9rounda0ou"9 id=9,019 4 <4"3pe <4con7igura"ion

Detailed description of all possi-le types and classes can -e found here: http:==#i$i!openstreet&ap!org=inde%!php=(apKfeatures

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page D=:;

Installation of pgRouting
3on F-untu <!;9 3#ith Postgre ./ E!B4 #ith " P and Driving Distance 3C%tras44 "o -uild pgRouting the follo#ing li-raries are re*uired: pgRouting source code 3http:==pgrouting!postl-s!org4 C and CMM co&pilers Postgre ./ version NI E!; PostGI version NI A!; "he 7oost Graph /i-rary 37G/4! >ersion NI A!BB #hich contains the astar!hpp 3http:==###!-oost!org=li-s=graph=doc=inde%!ht&l4 For " P 3optional4: "he Genetic Algorith& Ftility /i-rary 3GAF/4 3http:==gaul!sourceforge!net4 For Driving Distance 3optional4: Co&putational Geo&etry Algorith&s /i-rary 3CGA/4 version NI B!: 3http:==###!cgal!org4

For installation instructions on other platfor&s ta$e a loo$ at the pgRouting docu&entation pages: http:==pgrouting!postl-s!org=#i$i=pgRoutingDocsOInstallation

pgRouting on the /ive D>D has -een installed li$e this:

1. Install required pac ages


sudo ap"-ge" ins"all 0uild-essen"ial su0version cma1e sudo ap"-ge" ins"all li00oos"-grap6; sudo ap"-ge" ins"all pos"gres<l-8.+-pos"gis pos"gres<l-server-dev-8.+

For Driving Distance algorith& 3optional4 CGA/ li-rary can -e easily installed using the F-untu &ultiverse repository!
sudo ap"-ge" ins"all li0cgal;

For " P algorith& 3optional4


wge" 6""p:44downloads.source7orge.ne"4gaul4gaul-devel-0.18,)-0."ar.g=? mod"ime=111,1*+,'(>0ig2mirror=0 "ar -x=7 gaul-devel-0.18,)-0."ar.g= cd gaul-devel-0.18,)-04 .4con7igure --disa0le-slang ma1e sudo ma1e ins"all

!. "ompile pg#outing core $%ith &S' and DD flag on(


svn c6ec1ou" 6""p:44pgrou"ing.pos"l0s.org4svn4pgrou"ing4"run1 pgrou"ing cd pgrou"ing4 cma1e -?@ATB2T%C=$D -?@ATB2??=$D . ma1e sudo ma1e ins"all

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page P=:;

). Setup 'ostgreS*L
et local data-ase connections to 1trust1 in 1pgKh-a!conf1 to -e a-le to #or$ #ith Postgre ./ as user 1postgres1! "hen restart Postgre ./!
sudo gedi" 4e"c4pos"gres<l48.+4main4pg260a.con7 sudo 4e"c4ini".d4pos"gres<l-8.+ res"ar"

+. "reate routing database


"o test your installation create a first routing data-ase and load the routing functions into this data-ase!
crea"ed0 -U pos"gres rou"ing crea"elang -U pos"gres plpgs<l rou"ing

Add PostGI functions


ps<l -U pos"gres -7 4usr4s6are4pos"gres<l-8.+-pos"gis4lwpos"gis.s<l rou"ing ps<l -U pos"gres -7 4usr4s6are4pos"gres<l-8.+-pos"gis4spa"ial2re72s3s.s<l rou"ing

Add pgRouting functions


ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2core.s<l rou"ing ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2core2wrappers.s<l rou"ing ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2"opolog3.s<l rou"ing

Add " P functions 3optional4


ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2"sp.s<l rou"ing ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2"sp2wrappers.s<l rou"ing

Add Driving Distance functions 3optional4


ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2dd.s<l rou"ing ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2dd2wrappers.s<l rou"ing

+o# #e can proceed to the ne%t step to load the net#or$ data!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page E=:;

oad your networ! data and create a networ! topology


o&e net#or$ data already co&es #ith a net#or$ topology that can -e used #ith pgRouting i&&ediately! 7ut usually the data is in a different for&at than #e need for pgRouting! 'ften net#or$ data is stored in the hape file for&at 3!shp4 and #e can use PostGI 0 shape:postgres*l converter to i&port the data into the data-ase! 'pen treet(ap stores its data as ?(/ and it has its o#n i&porting tools for Postgre ./ data-ase! /ater #e #ill use the os&:pgrouting converter! 7ut it does &uch &ore than the -asic steps for si&ple routing) so #e #ill start this #or$shop #ith the &ini&u& re*uired attri-utes!

Load the net%or data


After creating the #or$shop data-ase and adding the PostGI and pgRouting functions to this data-ase 3see previous chapter4) #e load the sa&ple data to our data-ase:
ps<l -U pos"gres rou"ing Ei 46ome4<user 4wa3s2wi"6ou"2"opolog3.s<l

+ote: "he ./ du&p file #as &ade fro& a data-ase #hich already had PostGI functions loaded) so it #ill report errors during i&port that these functions already e%ist! 6ou can ignore these errors! /et0s see #itch ta-les have -een created:
Ed Fis" o7 rela"ions %c6ema G Dame G T3pe G $wner ---------------------------------------------pu0lic G geome"r32columns G "a0le G pos"gres pu0lic G spa"ial2re72s3s G "a0le G pos"gres pu0lic G wa3s G "a0le G pos"gres H+ rowsI Ed wa3s Ta0le 9pu0lic.wa3s9 /olumn G T3pe G &odi7iers ----------------------------------------gid G in"eger G no" null leng"6 G dou0le precision G name G c6arac"erH'00I G "6e2geom G geome"r3 G Andexes: 9wa3s2p1e39 C.A&5.J KLJM 0"ree HgidI /6ec1 cons"rain"s: 9en7orce2dims2"6e2geom9 /BL/K HndimsH"6e2geomI = 'I 9en7orce2geo"3pe2"6e2geom9 /BL/K Hgeome"r3"3peH"6e2geomI = '&UFTAFADL%T.AD8'::"ex" $. "6e2geom A% DUFFI 9en7orce2srid2"6e2geom9 /BL/K HsridH"6e2geomI = ,+'*I

"reate net%or topolog,


@aving your data i&ported into a Postgre ./ data-ase usually re*uires one &ore step for pgRouting! 6ou have to &a$e sure that your data provides a correct net#or$ topology) #hich consists of lin$s #ith source and target ID each!
F' 9G:;;< "o$yo='sa$a 8 pgRouting #or$shop Page <=:;

If your net#or$ data doesn0t have such net#or$ topology infor&ation already you need to run the 1assignKverte%Kid1 function! "his function assigns a source and a target ID to each lin$ and it can 1snap1 near-y vertices #ithin a certain tolerance!
assign2ver"ex2idH'<"a0le 'M 7loa" "oleranceM '<geome"r3 column'M '<gid 'I

First #e have to add source and target colu&n) then #e run the assignKverte%Kid function !!! and #ait!
5FTL. T5NFL wa3s 5?? /$FU&D source in"egerO 5FTL. T5NFL wa3s 5?? /$FU&D "arge" in"egerO %LFL/T assign2ver"ex2idH'wa3s'M 0.00001M '"6e2geom'M 'gid'IO

+ote: "he di&ension of the tolerance para&eter depends on your data pro2ection! Fsually it0s either 1degrees1 or 1&eters1! 7ecause ' ( data has a very good *uality for Cape to#n #e can choose a very s&all 1snapping1 tolerance: ;!;;;;A degrees! Add indices Fortunately #e didn0t need to #ait too long -ecause the data is s&all! 7ut your net#or$ data &ight -e very large) so it0s a good idea to add an inde% on source) target and geo&etry colu&n!
/.L5TL AD?LP source2idx $D wa3sHsourceIO /.L5TL AD?LP "arge"2idx $D wa3sH"arge"IO /.L5TL AD?LP geom2idx $D wa3s U%AD8 8A%TH"6e2geom 8A%T28L$&LT.J2$C%IO

After these steps our routing data-ase loo$ li$e this:


Ed Fis" o7 rela"ions %c6ema G Dame G T3pe G $wner ---------------------------------------------------pu0lic G geome"r32columns G "a0le G pos"gres pu0lic G spa"ial2re72s3s G "a0le G pos"gres pu0lic G ver"ices2"mp G "a0le G pos"gres pu0lic G ver"ices2"mp2id2se< G se<uence G pos"gres pu0lic G wa3s G "a0le G pos"gres H! rowsI Ed wa3s Ta0le 9pu0lic.wa3s9 /olumn G T3pe G &odi7iers ----------------------------------------gid G in"eger G no" null leng"6 G dou0le precision G name G c6arac"erH'00I G "6e2geom G geome"r3 G source G in"eger G "arge" G in"eger G Andexes: 9wa3s2p1e39 C.A&5.J KLJM 0"ree HgidI /6ec1 cons"rain"s: 9en7orce2dims2"6e2geom9 /BL/K HndimsH"6e2geomI = 'I 9en7orce2geo"3pe2"6e2geom9 /BL/K Hgeome"r3"3peH"6e2geomI = '&UFTAFADL%T.AD8'::"ex" $. "6e2geom A% DUFFI 9en7orce2srid2"6e2geom9 /BL/K HsridH"6e2geomI = ,+'*I

+o# #e are ready for routing #ith Di2$stra algorith&Q


F' 9G:;;< "o$yo='sa$a 8 pgRouting #or$shop Page A;=:;

pgRouting with Di"!stra algorith#


Di2$stra algorith& #as the first algorith& i&ple&ented in pgRouting! It doesn0t re*uire &ore attri-utes than source and target ID) and it can distinguish -et#een directed and undirected graphs! 6ou can specify if your net#or$ has 1reverse cost1 or not!
s6or"es"2pa"6H s<l "ex"M source2id in"egerM "arge"2id in"egerM direc"ed 0ooleanM 6as2reverse2cos" 0oolean I

+ote: ource and target IDs are verte% IDs! Fndirected graphs 31directed false14 ignores 1hasKreverseKcost1 setting

Shortest 'ath Di- stra core function


Cach algorith& has its core function 3i&ple&entation4) #hich is the -ase for its #rapper functions!
%LFL/T ; F.$& s6or"es"2pa"6H' %LFL/T gid as idM source::in"egerM "arge"::in"egerM leng"6::dou0le precision as cos" F.$& wa3s'M 10M '0M 7alseM 7alseIO ver"ex2id G edge2id G cos" ------------------------------------------10 G ')+ G 0.00!)!)*')+8',!+, ) G ,*+' G 0.08,*(+10+)',)(8( +)(, G ,*++ G 0.0(*!*+!0)0!1,+0+ '10( G ,*+, G 0.0(*+)!1!+18),)+( ... G ... G ... '0 G -1 G 0 H*+ rowsI

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page AA=:;

Di- stra .rapper functions


5rapper functions e%tend the core functions #ith transfor&ations) -ounding -o% li&itations) etc!! $rapper $I%&O'% bounding bo( 5rappers can change the for&at and ordering of the result! "hey often set default function para&eters and &a$e the usage of pgRouting &ore si&ple!
%LFL/T gidM 5sTex"H"6e2geomI 5% "6e2geom F.$& di:1s"ra2spH'wa3s'M 10M '0IO gid G "6e2geom -----------------------------------------------------------------------')+ G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0(,01) -++.),,+8++II ,*+' G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0((+88 -++.),+*18+II ,*++ G &UFTAFADL%T.AD8HH18.,0((+88 -++.),+*18+M18.,080')+ -++.),')(++II ... G ... (*' G &UFTAFADL%T.AD8HH18.,',1,'' -++.)1()'(!M18.,'+(,'+ -++.)18')**II (*1 G &UFTAFADL%T.AD8HH18.,',+!'+ -++.)1((1!,M18.,',1,'' -++.)1()'(!II H*' rowsI

$rapper $I%& bounding bo( 6ou can li&it your search area -y adding a -ounding -o%! "his #ill i&prove perfor&ance especially for large net#or$s!
%LFL/T gidM 5sTex"H"6e2geomI 5% "6e2geom F.$& di:1s"ra2sp2del"aH'wa3s'M 10M '0M 0.1IO gid G "6e2geom -----------------------------------------------------------------------')+ G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0(,01) -++.),,+8++II ,*+' G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0((+88 -++.),+*18+II ,*++ G &UFTAFADL%T.AD8HH18.,0((+88 -++.),+*18+M18.,080')+ -++.),')(++II ... G ... (*' G &UFTAFADL%T.AD8HH18.,',1,'' -++.)1()'(!M18.,'+(,'+ -++.)18')**II (*1 G &UFTAFADL%T.AD8HH18.,',+!'+ -++.)1((1!,M18.,',1,'' -++.)1()'(!II H*' rowsI

+ote: "he pro2ection of ' ( data is 1degree1) so #e set a -ounding -o% containing start and end verte% plus a ;!A degree -uffer for e%a&ple!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page A:=:;

pgRouting with A)Star algorith#


A, tar algorith& is another #ell,$no#n routing algorith&! It adds geographical infor&ation to source and target of each net#or$ lin$! "his ena-les the shortest path search to prefer lin$s #hich are closer to the target of the search!

'repare routing table for A/Star


For A, tar you need to prepare your net#or$ ta-le and add latitute=longitude colu&ns 3%A) yA and %:) y:4 and calculate their values!
5FTL. 5FTL. 5FTL. 5FTL. UC?5TL UC?5TL UC?5TL UC?5TL T5NFL T5NFL T5NFL T5NFL wa3s wa3s wa3s wa3s wa3s wa3s wa3s wa3s %LT %LT %LT %LT 5?? 5?? 5?? 5?? = = = = /$FU&D /$FU&D /$FU&D /$FU&D x1 31 x' 3' dou0le dou0le dou0le dou0le precisionO precisionO precisionO precisionO

x1 31 x' 3'

xHs"ar"poin"H"6e2geomIIO 3Hs"ar"poin"H"6e2geomIIO xHendpoin"H"6e2geomIIO 3Hendpoin"H"6e2geomIIO

+ote: 1endpoint341 function fails for so&e versions of Postgre ./ 3ie! E!:!J) E!A!<4! A #or$around for that pro-le& is using the 1Point+341 function instead:
UC?5TL UC?5TL UC?5TL UC?5TL wa3s wa3s wa3s wa3s %LT %LT %LT %LT x1 31 x' 3' = = = = xHCoin"DH"6e2geomM 3HCoin"DH"6e2geomM xHCoin"DH"6e2geomM 3HCoin"DH"6e2geomM 1IIO 1IIO DumCoin"sH"6e2geomIIIO DumCoin"sH"6e2geomIIIO

Shortest 'ath A/Star core function


hortest Path A, tar function is very si&ilar to the Di2$stra function) though it prefers lin$s that are close to the target of the search! "he heuristics of this search are predefined) so you need to reco&pile pgRouting if you #ant to &a$e changes to the heuristic function itself!
s6or"es"2pa"62as"arH s<l "ex"M source2id in"egerM "arge"2id in"egerM direc"ed 0ooleanM 6as2reverse2cos" 0oolean I

+ote:

ource and target IDs are verte% IDs! Fndirected graphs 31directed false14 ignores 1hasKreverseKcost1 setting

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page AB=:;

*(a#ple of A)Star core function


%LFL/T ; F.$& s6or"es"2pa"62as"arH' %LFL/T gid as idM source::in"egerM "arge"::in"egerM leng"6::dou0le precision as cos"M x1M 31M x'M 3' F.$& wa3s'M 10M '0M 7alseM 7alseIO ver"ex2id G edge2id G cos" ------------------------------------------10 G ')+ G 0.00!)!)*')+8',!+, ) G ,*+' G 0.08,*(+10+)',)(8( +)(, G ,*++ G 0.0(*!*+!0)0!1,+0+ ... G ... G ... '0 G -1 G 0 H*+ rowsI

$rapper function $I%& bounding bo( 5rapper functions e%tend the core functions #ith transfor&ations) -ounding -o% li&itations) etc!!
%LFL/T gidM 5sTex"H"6e2geomI 5% "6e2geom F.$& as"ar2sp2del"aH'wa3s'M 10M '0M 0.1IO gid G "6e2geom -----------------------------------------------------------------------')+ G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0(,01) -++.),,+8++II ,*+' G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0((+88 -++.),+*18+II ,*++ G &UFTAFADL%T.AD8HH18.,0((+88 -++.),+*18+M18.,080')+ -++.),')(++II ... G ... (*' G &UFTAFADL%T.AD8HH18.,',1,'' -++.)1()'(!M18.,'+(,'+ -++.)18')**II (*1 G &UFTAFADL%T.AD8HH18.,',+!'+ -++.)1((1!,M18.,',1,'' -++.)1()'(!II H*' rowsI

+ote: "here is currently no #rapper function for A, tar #ithout -ounding -o%) since -ounding -o%es are very useful to increase perfor&ance! If you don0t need a -ounding -o% Di2$stra #ill -e enough any#ay! "he pro2ection of ' ( data is 1degree1) so #e set a -ounding -o% containing start and end verte% plus a ;!A degree -uffer for e%a&ple!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page A9=:;

pgRouting with Shooting)Star algorith#


hooting, tar algorith& is the latest of pgRouting shortest path algorith&s! Its speciality is that it routes fro& lin$ to lin$) not fro& verte% to verte% as Di2$stra and A, tar algorith&s do! "his &a$es it possi-le to define relations -et#een lin$s for e%a&ple) and it solves so&e other verte%,-ased algorith& issues li$e 1parallel lin$s1) #hich have sa&e source and target -ut different costs!

'repare routing table for Shooting/Star


For hooting, tar you need to prepare your net#or$ ta-le and add the 1reverseKcost1 and 1toKcost1 colu&n! /i$e A, tar this algorith& also has a heuristic function) #hich prefers lin$s closer to the target of the search!
5FTL. T5NFL wa3s 5?? /$FU&D reverse2cos" dou0le precisionO UC?5TL wa3s %LT reverse2cos" = leng"6O 5FTL. T5NFL wa3s 5?? /$FU&D "o2cos" dou0le precisionO 5FTL. T5NFL wa3s 5?? /$FU&D rule "ex"O

Shortest 'ath Shooting/Star core function


hooting, tar algorith& introduces t#o ne# attri-utes:

rule: a string #ith a co&&a separated list of edge IDs) #hich descri-es a rule for turning restriction 3if you ca&e along these edges) you can pass through the current one only #ith the cost stated in toKcost colu&n4 to+cost: a cost of a restricted passage 3can -e very high in a case of turn restriction or co&para-le #ith an edge cost in a case of traffic light4

s6or"es"2pa"62s6oo"ing2s"arH s<l "ex"M source2id in"egerM "arge"2id in"egerM direc"ed 0ooleanM 6as2reverse2cos" 0oolean I

+ote:

ource and target IDs are lin$ Ids! Fndirected graphs 31directed false14 ignores 1hasKreverseKcost1 setting

*(a#ple for Shooting)Star ,rule, hootingR algorith& calculates a path fro& edge to edge 3not fro& verte% to verte%4! Colu&n verte%Kid contains start verte% of an edge fro& colu&n edgeKid! "o descri-e turn restrictions:
gid G source G "arge" G cos" G x1 G 31 G x' G 3' G "o2cos" G rule ------------------------------------------------------------------1' G + G 10 G ' G , G + G , G ! G 1000 G 1,

&eans that the cost of going fro& edge A9 to edge A: is A;;;) and
gid G source G "arge" G cos" G x1 G 31 G x' G 3' G "o2cos" G rule ------------------------------------------------------------------1' G + G 10 G ' G , G + G , G ! G 1000 G 1,M ,

&eans that the cost of going fro& edge A9 to edge A: through edge 9 is A;;;! If you need &ultiple restrictions for a given edge then you have to add &ultiple records for that
F' 9G:;;< "o$yo='sa$a 8 pgRouting #or$shop Page AJ=:;

edge each #ith a separate restriction!


gid G source G "arge" G cos" G x1 G 31 G x' G 3' G "o2cos" G rule ------------------------------------------------------------------11 G + G 10 G ' G , G + G , G ! G 1000 G , 11 G + G 10 G ' G , G + G , G ! G 1000 G 1'

&eans that the cost of going fro& either edge 9 or A: to edge AA is A;;;! And then you al#ays need to order your data -y gid #hen you load it to a shortest path function!! *(a#ple of Shooting)Star core function
%LFL/T ; F.$& s6or"es"2pa"62s6oo"ing2s"arH' %LFL/T gid as idM source::in"egerM "arge"::in"egerM leng"6::dou0le precision as cos"M x1M 31M x'M 3'M ruleM "o2cos" F.$& wa3s'M ')+M (*1M 7alseM 7alseIO ver"ex2id G edge2id G cos" ------------------------------------------,'+' G ')+ G 0.00!)!)*')+8',!+, +1,, G ')+ G 0.00!)!)*')+8',!+, ,'+' G ,*+' G 0.08,*(+10+)',)(8( ... G ... G ... !1 G (*1 G 0.0+0!')8,(8'+)!)* H*+ rowsI

$rapper function $I%& bounding bo( 5rapper functions e%tend the core functions #ith transfor&ations) -ounding -o% li&itations) etc!!
%LFL/T gidM 5sTex"H"6e2geomI 5% "6e2geom F.$& s6oo"ings"ar2spH'wa3s'M ')+M (*1M 0.1M 'leng"6'M "rueM "rueIO gid G "6e2geom -----------------------------------------------------------------------')+ G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0(,01) -++.),,+8++II ')+ G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0(,01) -++.),,+8++II ,*+' G &UFTAFADL%T.AD8HH18.,0(,1,) -++.),,++08M18.,0((+88 -++.),+*18+II ... G ... (*' G &UFTAFADL%T.AD8HH18.,',1,'' -++.)1()'(!M18.,'+(,'+ -++.)18')**II (*1 G &UFTAFADL%T.AD8HH18.,',+!'+ -++.)1((1!,M18.,',1,'' -++.)1()'(!II H*' rowsI

+ote: "here is currently no #rapper function for A, tar #ithout -ounding -o%) since -ounding -o%es are very useful to increase perfor&ance! If you don0t need a -ounding -o% Di2$stra #ill -e enough any#ay! "he pro2ection of ' ( data is 1degree1) so #e set a -ounding -o% containing start and end verte% plus a ;!A degree -uffer for e%a&ple!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page AD=:;

os#-pgrouting data con.erter


"his tool &a$es it easy to i&port 'pen treet(ap data and use it #ith pgRouting! It creates topology auto&atically and creates ta-les for feature types and road classes! os&:pgrouting #as pri&arily #ritten -y Daniel 5endt and is no# hosted on the pgRouting pro2ect site: http:==pgrouting!postl-s!org=#i$i=tools=os&:pgrouting

0o% to install $1buntu 2.3+(


Chec$ out the latest version fro& >+ repository:
svn c6ec1ou" 6""p:44pgrou"ing.pos"l0s.org4svn4pgrou"ing4"ools4osm'pgrou"ing4"run1 osm'pgrou"ing

Re*uired pac$ages=li-raries: A! Postgre ./ :! PostGI B! pgRouting 9! 7oost li-rary J! C%pat li-rary D! li-p* li-rary +ote: if you already co&piled pgRouting point A! to 9! should already -e installed! "hen co&pile
cd osm'pgrou"ing ma1e

In case 7oost li-rary is &issing:


sudo ap"-ge" upda"e sudo ap"-ge" ins"all li00oos"-grap6-dev

Get 4apan 5S6 data


Do#nload ' ( files of Japan fro& Cloud&ade server:
wge" 6""p:44downloads.cloudmade.com4asia4:apan4:apan.osm.0=' 0un=ip' :apan.osm.0='

0o% to use osm!pgrouting


A! First you need to create a data-ase and add PostGI and pgRouting functions:
crea"ed0 -U pos"gres osm crea"elang -U pos"gres plpgs<l osm ps<l -U pos"gres -7 4usr4s6are4pos"gres<l-8.+-pos"gis4lwpos"gis.s<l osm ps<l -U pos"gres -7 4usr4s6are4pos"gres<l-8.+-pos"gis4spa"ial2re72s3s.s<l osm ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2core.s<l osm ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2core2wrappers.s<l osm ps<l -U pos"gres -7 4usr4s6are4pos"l0s4rou"ing2"opolog3.s<l osm

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page AP=:;

:! 6ou can define the features and attri-utes to -e i&ported fro& the 'pen treet(ap ?(/ file in the configuration file 3default: &apconfig!%&l4 B! 'pen a ter&inal #indo# and run os&:pgrouting #ith the follo#ing para&ters
.4osm'pgrou"ing -7ile 46ome4<user 42apan!os& E -con7 mapcon7ig.xml E -d0name osm E -user user E -6os" local6os" E -clean

'ther availa-le para&eters are:


; re<uired: -7ile -d0name -user -con7 ; op"ional: -6os" 1'(.0.0.1I -por" -passwd -clean <7ile <d0name <user <7ile <6os" <por" <passwd -- name o7 3our osm xml 7ile -- name o7 3our da"a0ase -- name o7 "6e userM w6ic6 6ave wri"e access "o "6e da"a0ase -- name o7 3our con7igura"ion xml 7ile -- 6os" o7 3our pos"gres<l da"a0ase Hde7aul": -- por" o7 3our da"a0ase Hde7aul": !,+'I -- password 7or da"a0ase access -- drop peviousl3 crea"ed "a0les

9! Connect to your data-ase and see the ta-les that have -een created
ps<l -U pos"gres osm Ed Fis" o7 rela"ions %c6ema G Dame G T3pe G $wner ---------------------------------------------------pu0lic G classes G "a0le G pos"gres pu0lic G geome"r32columns G "a0le G pos"gres pu0lic G nodes G "a0le G pos"gres pu0lic G spa"ial2re72s3s G "a0le G pos"gres pu0lic G "3pes G "a0le G pos"gres pu0lic G ver"ices2"mp G "a0le G pos"gres pu0lic G ver"ices2"mp2id2se< G se<uence G pos"gres pu0lic G wa3s G "a0le G pos"gres H8 rowsI

+ote: If ta-les are &issing you &ight have forgotten to add PostGI or pgRouting functions to your data-ase! /et0s do so&e &ore advanced routing #ith those e%tra infor&ation a-out road types and road classes!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page AE=:;

Ad.anced usage of pgRouting shortest path search


An ordinary shortest path *uery #ith result usualy loo$s li$e this:
%LFL/T ; F.$& s6or"es"2pa"62s6oo"ing2s"arH '%LFL/T gid as idM sourceM "arge"M leng"6 as cos"M x1M 31M x'M 3'M ruleM "o2cos"M reverse2cos" F.$& wa3s'M 1)!!M !(8(M "rueM "rueIO ver"ex2id G edge2id G cos" ------------------------------------------81+, G 1)!! G 0.00)!',(!,*,810'() !,!) G 1)!* G 0.0*'80(!!*+11'8(1 81+( G 1)(* G 0.081'(8*+*(080'*8 !,!+ G (!8 G 0.0,'1(,('(0+!8'(' !,!* G ++** G 0.010,)+!(+'!1,8+1 1108* G ++*( G 0.11+,000+0''10,( ,,1* G +0* G 0.111*00+())!)'') ,,1) G +0( G 0.0880,11)('!1)!)! ,,'' G ,880 G 0.0'08!))11,+***++ !101 G *1' G 0.0)0*8!)88'+81,)! !10' G !(8( G 8008).88'0)1),!) H11 rowsI

"hat is usually called @'R"C " path) #hich &eans that a length of an edge is its cost!

"osts can be an,thing $7.eighted costs7(


7ut in real net#or$s #e have different li&itations or preferences for different road types for e%a&ple! In other #ords) #e #ant to calculate C@CAPC " path , a path #ith a &ini&al cost! "here is no li&itation in #hat #e ta$e as costs! 5hen #e convert data fro& ' ( for&at using the os&:pgrouting tool) #e get these t#o additional ta-les for road types and classes:
Ed classes id G name -----------------' G c3clewa3 1 G 6ig6wa3 , G :unc"ion + G "rac1"3pe Ed "3pes id G "3pe2id G name G cos" --------------------------------------------'01 G ' G lane G 1 '0, G ' G opposi"e G 1 '0+ G ' G opposi"e2lane G 1 '0' G ' G "rac1 G 1 11( G 1 G 0ridlewa3 G 1 11+ G 1 G 0us2guidewa3 G 1 118 G 1 G 03wa3 G 1 11! G 1 G ciclewa3 G 1 11* G 1 G 7oo"wa3 G 1 108 G 1 G living2s"ree" G 1 101 G 1 G mo"orwa3 G 0.' 10+ G 1 G mo"orwa32:unc"ion G 0.'

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page A<=:;

10' 11, 111 10* 10( 10( 100 100 10* 10) 11' 11) 10( 110 10, 10! ,01 ...

G G G G G G G G G G G G G G G G G

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ,

G G G G G G G G G G G G G G G G G

mo"orwa32lin1 pa"6 pedes"rian primar3 primar32lin1 residen"ial road unclassi7ied secondar3 service services s"eps "er"iar3 "rac1 "run1 "run12lin1 rounda0ou"

G G G G G G G G G G G G G G G G G

0.' 100 100 100 100 100 0.( 0.( 10 10 10 10 10 10 10 10 10

Road class is lin$ed #ith the #ays ta-le -y classKid field! Cost values for classes ta-le are assigned ar-itrary!
UC?5TL classes %LT cos"=1! @BL.L id +00O

For -etter perfor&ance it is #orth to create an inde% on id field of classes ta-le!


/.L5TL AD?LP class2idx $D wa3s HidIO

"he idea -ehind these t#o ta-les is to specify a factor to -e &ultiplied #ith the cost of each lin$ 3usually length4:
%LFL/T ; F.$& s6or"es"2pa"62s6oo"ing2s"arH '%LFL/T gid as idM class2idM sourceM "arge"M leng"6;c.cos" as cos"M x1M 31M x'M 3'M ruleM "o2cos"M reverse2cos";c.cos" as reverse2cos" F.$& wa3s wM classes c @BL.L class2id=c.id'M 1)!!M !(8(M "rueM "rueIO ver"ex2id G edge2id G cos" ------------------------------------------81+, G 1)!! G 0.00***(+'8'!+*(1)! !,!) G 1)!* G 0.0,+)*!'8),1()01 81+( G 1))' G 0.1'**,*'+0)+*(,( !,*, G (*' G 0.8'(8*8(0,808)(8 !,*( G (*+ G 0.1*(*!)0'!'8*,8 ... G ... G ... )()0 G !(8! G 0.001,'10(,*8'*8+(+ 8!,8 G !(8* G 0.000***08*8!)8,(*1 1*'1, G !(8( G 0.01*01()(*,18+8)' H*) rowsI

5e can see that the shortest path result is co&pletely different fro& the e%a&ple -efore! 5e call this 1#eighted costs1! Another e%a&ple is to restrict access to roads of a certain type:
UC?5TL classes %LT cos"=100000 @BL.L name FAKL 'mo"orwa3Q'O

"hrough su-*ueries you can 1&i%1 your costs as you li$e and this #ill change the results of your routing re*uest i&&ediately! Cost changes #ill affect the ne%t shortest path search) and there is no need to re-uild your net#or$!

F'

9G:;;< "o$yo='sa$a 8 pgRouting #or$shop

Page :;=:;

You might also like