Sunteți pe pagina 1din 20

1 Install pgRouting (2013) for Spatial Databases Lecturer Pat Browne pgRouting is already installed on the cork server.

So if you are working the the cork server please do not us this information. This note describes how to install pgRouting onyour own machine in new database. We then show how to execute simple network query. ou could also install pgRouting in your existing postgis database. The note is intended as general advice it is not a lab. 1)Install pgRouting Step 1! "opy and un#ip.

$se pgRouting%1.&'(pg%).*.+ from studentdisrib or http!,,www.pgrouting.org, -ote that this is compatable with .ostgreS/0 ).* The 1ip contains source S/0 files and Windows 200 files

$se Windows 3xtract 4ll command 5or un#ip6 to copy all the files a temporary folder 5say pgTemp6. The extraction process will make two sub%folders called 7lib8 and 7share8 ou should copy the 200s and S/0 files provided in these folders the correct location under your .ostgreS/0 installation9 which should be! "opy the 200 files to "!:.rogram ;iles:.ostgreS/0:).*:lib "opy the S/0 files to "!:.rogram ;iles:.ostgreS/0:).*:share:contrib These folders are shown below!

Start pg4dmin <<<9 a graphical tool for administering .ostgreS/0 databases. This tool is accessible from the Windows Start | All Programs Menu and is necessary for this tutorial. <t is assumed that the reader is familiar with the =icrosoft Windows operating system. <t is assumed that the pgRouting files have been installed in the correct folders as above. Step 2! "reate a database called 7testdb8 using pg4dmin <<< and add .ost><S functionality via the default template. 4 template can be used to construct many database with the same properties.

?pen pg4dmin <<< from the Windows Start =enu 57Start%@.rograms% @.ostgreS/0 ).*%@pg4dmin <<<86. "onnect to your database by double clicking it in the obAect browser. ou may need to enter password information. <n pg4dmin <<<9 right click on 72atabases8 in the table and click 7-ew 2atabaseB8.

'

-ame the database 7testdb8 and for the template9 select 7template(postgis8. "lick 7?C8.

our pg4dmin should look something like this!

Step 3! 4dd the core pgRouting functionality to the newly created database.

<n pg4dmin <<<9 select the newly created 7testdb8 database in the obAect browser. 0ook at the top toolbar in pg4dmin <<<. There is a S/0 query tool. "lick on this tool to open it9 or click 7Tools%@/uery Tool8 from the application menu. <n the S/0 query tool window9 click 7;ile%@?pen8 and select

7"!:.rogram ;iles:.ostgreS/0:).*:share:contrib:routing(core.sql8 5the directory structure may differ depending on where you installed your software6.

To execute this query9 click the green 7play8 button or navigate the application menu by clicking 7/uery%@3xecute8. Repeat the same process for

7"!:.rogram ;iles:.ostgreS/0:).*:share:contrib:routing(core(wrappers.sql8. 7"!:.rogram ;iles:.ostgreS/0:).*:share:contrib:routing(topology.sql8.

-ow the routing functionality is available to 7testdb8. The output from the execution of the three above S/0 files should beD Query returned successfully with no result in XXms.

E Step 4! ou do not have to do this step as the S/0 file is on the course web page9 but it is useful to know how to convert a shape file to an S/0 file. "onvert the crude polyline shapefile 5edges.shp6 to an equally crude S/0 file so that it can be imported to the database. -ote9 at point this file does not have any network topology9 and it cannot be used for routing.

"opy the edges shape file from the course web page to 7"!:.rogram ;iles:.ostgreS/0:).*:bin8 ;rom the Windows command prompt9 make sure that 7"!:.rogram ;iles:.ostgreS/0:).*:bin@8 is displayed as your current directory location. 4n alternate option would be to add that directory to the system path. To make the S/0 file from the shape file enter this command! shp+pgsql %s ++FG %i %< edges.shp edges @ edges.sql

The successful execution of this command should output this to the screen! Shapefile type! 4rc .ostgis type! =$0T<0<-3STR<->H+I

-?T3! <n the above command9 7%s ++FG8 sets the spatial reference 5SR<26 for the dataset9 which happens to be J-42)' , Texas -orth "entral 5ft$S6J. Kow do we find the SR<2 for our preferred proAection in pg4dminL
o o

<n pg4dmin <<<9 look at the top toolbar. There is a S/0 query tool. =ake sure that a .ost><S%enabled database is selected 5i.e. 7template(postgis8 or 7testdb869 and click on the S/0 query tool to bring up the query window 5or click JTools%@/uery ToolJ6. 3nter the following code in the S/0 query tool to find all spatial reference systems that have 7Texas8 in the WCT 5Well%Cnown Text6 definition! S303"T M ;R?= spatial(ref(sys WK3R3 srtext 0<C3 5NOTexasON6D

o o

To execute this query9 click the 7play8 button or navigate the application menu by clicking 7/uery%@3xecute8. This S/0 query will return a list of spatial references with SR<2s. Take your pick.

G Step ! <mport the crude S/0 file to the database as a new table using the S/0 query tool in pg4dmin <<<.

<n the S/0 /uery tool window9 click 7;ile%@?pen8 and navigate to your newly created S/0 file 57"!:.rogram ;iles:.ostgreS/0:).*:bin:edges.sql86 and click J?penJ. "lick the 7play8 button 5or 7/uery%@3xecute86 to create your new table in the database. ou may have to refresh the view to see the 7edges8 table in the 7testdb8 database. 4lternatively cd 7"!:.rogram ;iles:.ostgreS/0:).*:bin8 cmdD psql %d testdb %$ postgres %f edges.sql

Step !! Piewing edges in ?penQump.

;rom main menu bar in ?penQump select 0ayer R 4dd 2atastore 0ayer!

=ake a new connection to new database!

Run the query

ou should see the map

Step "! .repare the new table for 2iAkstra by adding source9 target9 and length columns. <n this example 7length8 will be the cost of the edges. This step is required because as it stands the edges table does not contain topology 5or connectivity information6.

<n the S/0 shell9 enter the following code and execute! \c testdb ALTER TA LE edges A!! "#L$M% source integer& ALTER TA LE edges A!! "#L$M% target integer& ALTER TA LE edges A!! "#L$M% lengt' double precision&

Step #! "reate the network topology in the 7edges8 table. This S/0 command also populates the 7length8 field which is to be the edge cost in the network topology.

<n the S/0 query tool9 enter the following code and execute!
SELE"T assign()erte*(id+,edges,- ..../- ,t'e(geom,- ,gid,0& $P!ATE edges SET lengt' 1 lengt'+t'e(geom0&

S Step $! "reate indices for source9 target9 and geometry columns in the 7edges8 table.

<n the S/0 shell9 enter the following code and execute! "REATE 2%!E3 source(id* #% edges+source0& "REATE 2%!E3 target(id* #% edges+target0& "REATE 2%!E3 geom(id* #% edges $S2%4 42ST+t'e(geom 42ST(4E#METR5(#PS0&

Step 10! .erform the routing operation and store the results in a 7diAkstra(result8 table.

<n the S/0 shell9 enter the following code and execute! !R#P TA LE 26 E32STS di7s8tra(result& "REATE TA LE di7s8tra(result+gid int90 wit' oids&

SELE"T Add4eometr:"olumn+,di7s8tra(result,,t'e(geom,- ,;;<=,- ,M$LT2L2%ESTR2%4,- ;0& 2%SERT 2%T# di7s8tra(result+t'e(geom0 SELE"T t'e(geom 6R#M di78stra(sp+,edges,- >;- ?>0&

-ow the result of the shortest path from node E+ to node 'E can be viewed in ?penQump by connecting to the JtestdbJ database and adding the 7diAkstra(result8 table to ?penQump using!.

SELE"T source-target-lengt'-asbinar:+d.t'e(geom0 6R#M di7s8tra(result AS d- edges&

Kere is an example of the results 5start node! E+9 end node! 'E6.

1&

ou can view vertices and edges by right clicking on the resulting layer and selecting "hange Styles

. Then select labels and choose the appropriate label.

11

What is happening hereL

%&a'ples of route fin(ing using Iris) roa( (ata* Kere we look in more detail at the topology. 4s in the above edges example9 we must add the appropriate columns and create topology. We must have the road data loaded into the testdb database. 4t the command prompt change directory! cd "@\Program 6iles\PostgreSAL\B.9\bin $se the following command at the command prompt in! psql Cd testdb C$ postgres CD roads.sql

-ow login into .ostgreS/0 and connect to testdb.

1+ \c testdb ALTER TA LE roads A!! "#L$M% source integer& ALTER TA LE roads A!! "#L$M% target integer& CC a column called row alread: e*ists in roads. CC but we would li8e to calculate t'e distances CC so we rename t'e old lengt' and ma8e a new lengt' ALTER TA LE roads RE%AME "#L$M% lengt' T# lengt'old& ALTER TA LE roads A!! "#L$M% lengt' double precision& <n the S/0 shell9 enter the following two commands. The first S/0 command creates the network topology in the 7roads8 table. The second S/0 command populates the 7length8 field which will be used as the edge cost in the network topology.
SELE"T assign()erte*(id+,roads,- ..../- ,t'e(geom,- ,gid,0& $P!ATE

roads SET lengt' 1 lengt'+t'e(geom0&

"heck the lengths have been calculated select length5the(geom6 from roadsD -ote the actual vertex ids may differ on your map so you should check visually for the appropriate numbers9 before running the following query To view the shortest path from 2ublin 5?B/0 to Waterford 5GG&6 in text format9 issue the following command in S/0 shell! SELE"T E 6R#M s'ortest(pat'+, SELE"T gid as idsource@@integertarget@@integerlengt'@@double precision as cost 6R#M roads,?B/- ==.- Dalse- Dalse0& The output consists of vertices9 edges9 and distances 5cost6 )erte*(id | edge(id | cost CCCCCCCCCCCFCCCCCCCCCFCCCCCCCCCCCCCCCCCC ?B/ | ?G. | ///?.;<99;<G><G> ?BB | ?BG | ?;>..?=</G;G9<.? ?B> | ?B= | /?>..</>99/;G/9/ ?B? | ?B> | >>;.>GB9B>?/>B=B

1' ?B9 | #r in PgAdmin ?B< | B9.B=9GG.?>9<?<;

So'e ot)er Iris) locations to +uer,1111111111111111111111111111111 ?B/ 1 Source in !ublin </ 1 source in "or8 9B 1 source in 4alwa: >>. 1 Source in Limeric8 ==. 1 source in WaterDord -ote the actual vertex ids may differ on your map so you should check visually in ?penQump. >et the appropriate numbers9 before running the queries.

.ow to grap)icall, /iew a route* To view the route from 2ublin to "ork in ?penQump! ;irst create a view in S/0 shell then the view is queries in ?penQump

"REATE H2EW dublin(cor8 AS + SELE"T E 6R#M s'ortest(pat'+, SELE"T gid as idsource@@integertarget@@integerlengt'@@double precision as cost 6R#M roads,-

1* ?B/- </- Dalse- Dalse00& Then run the following query in ?penQump Run 2atastore /uery select asbinar:+r.t'e(geom0 Drom dublin(cor8 dc- roads r w'ere r.gid 1 dc.edge(id& Which should give!

Run in S/0 shell. "REATE H2EW dublin(we*Dord AS + SELE"T E 6R#M s'ortest(pat'+, SELE"T gid as idsource@@integertarget@@integerlengt'@@double precision as cost 6R#M roads,?B/- ==.- Dalse- Dalse00& Then run the following query in ?penQump Run 2atastore /uery select asbinar:+r.t'e(geom0Drom dublin(we*Dord dw- roads r w'ere r.gid 1 dw.edge(id& Run in S/0 shell. "REATE H2EW dublin(galwa: AS + SELE"T E 6R#M s'ortest(pat'+, SELE"T gid as idsource@@integer-

1E target@@integerlengt'@@double precision as cost 6R#M roads,?B/- 9B- Dalse- Dalse00& Then run the following query in ?penQump Run 2atastore /uery select asbinar:+r.t'e(geom0Drom dublin(galwa: dg- roads r w'ere r.gid 1 dg.edge(id& Waterford to >alway "REATE H2EW wat(gal AS + SELE"T E 6R#M s'ortest(pat'+, SELE"T gid as idsource@@integertarget@@integerlengt'@@double precision as cost 6R#M roads,==.- 9B- Dalse- Dalse00& SELE"T asbinar:+r.t'e(geom0 r.gid 1 dc.edge(id& 6R#M wat(gal dc- roads r WIERE

0n(erstan(ing t)e topolog, We look at the relationship between the edges9 sources9 and targets. select target Drom roads w'ere source 1 ?B/& >ives! ')+9'))9 'FE Then if we check each of these we find that their source includes ')1 select source from roads where target T ')+D source %%%%%%%% ')1 ')S 'S& 5' rows6 select source from roads where target T '))D source %%%%%%%% ')1 51 row6 select source from roads where target T 'FED

1G source %%%%%%%% ')1 51 row6 The following is a graphical representation of how edges 5gids69 sources and targets are represented. select gid-source-target Drom road w'ere source1?B/& gid | source | target CCCCCFCCCCCCCCFCCCCCCCC ?B9 | ?B/ | ?B; ?G. | ?B/ | ?BB <G. | ?B/ | ?<> +? rows0

Labelled with Edges (gid):

Labelled with Source

1F

Labelled with Target

1) Road classification -ational .rimary 5-19-119 -*6

1easuring t)e lengt) of a pat) The first example only computes the lengths of individual sections of the path. Select asbinar:+geomunion+r.t'e(geom-r.t'e(geom00lengt'+geomunion+r.t'e(geom-r.t'e(geom00J/... as dist

1S Drom dublin(cor8 as dc- roads as r w'ere r.gid 1 dc.edge(id&

$se the following to compute the length of the shortest path from 2ublin to "ork. -ote9 the 2ublin("ork view must exist. select asbinar:+Mem4eom$nion+r.t'e(geom00lengt'+Mem4eom$nion+r.t'e(geom00J/... as dist Drom dublin(cor8 as dc- roads as r w'ere r.gid 1 dc.edge(id& The straight lines distance is about +1E%++& Cilometres

+&

S-ar putea să vă placă și