pgRouting logo

pgRouting Quickstart

pgRouting is an extension and adds routing functionality to PostGIS/PostgreSQL databases.

This Quick Start describes how to open a database with the command line and run a basic shortest path query with the sample test data.

Running pgRouting

  • Open a Applications ‣ Accessories ‣ Terminal window and connect to the pgrouting database:
psql -U user pgrouting
  • Type \d will list you all available tables:
                      List of relations
 Schema |        Name         |   Type   |  Owner
--------+---------------------+----------+----------
 public | classes               | table | user
 public | geography_columns   | view    | user
 public | geometry_columns      | table | user
 public | nodes                 | table | user
 public | spatial_ref_sys       | table | user
 public | types                 | table | user
 public | vertices_tmp          | table | user
 public | vertices_tmp_id_seq | sequence | user
 public | ways                  | table | user
(9 rows)
  • Run the Dijkstra shortest path function:
SELECT * FROM shortest_path('
                SELECT gid as id,
                         source::integer,
                         target::integer,
                         length::double precision as cost
                        FROM ways',
                100, 600, false, false);
     vertex_id | edge_id |       cost
    -----------+---------+---------------------
           100 |    1457 |  0.0152981335887719
           554 |     543 |  0.0658986376594475
           553 |     542 |  0.0720522950545032
           ... |     ... |  ...
          1803 |    1902 |  0.1000754339802650
           600 |      -1 |                   0
(77 rows)
  • Try the wrapper function to get the routing result in a different format:
SELECT gid, AsText(the_geom) AS the_geom
        FROM dijkstra_sp('ways', 100, 600);
  gid   |                              the_geom
--------+---------------------------------------------------------------
    533 | MULTILINESTRING((-105.0205242 39.7613979,-105.0205115 39.7620158))
    534 | MULTILINESTRING((-105.0205115 39.7620158,-105.0205151 39.7632155))
    535 | MULTILINESTRING((-105.0205151 39.7632155,-105.0205098 39.764433))
        ... | ...
   9395 | MULTILINESTRING((-104.9921535 39.7209342,-104.9921516 39.722527))
   9400 | MULTILINESTRING((-104.9921793 39.7147876,-104.9923595 39.714781))
(76 rows)
  • With \q command leave the PostgreSQL shell.

What next?

  • pgRouting Website - Visit the project website http://www.pgrouting.org to learn more about pgRouting.
  • Workshop on DVD - Included on this LiveDVD is the FOSS4G 2010 pgRouting workshop “FOSS4G routing with pgRouting tools, OpenStreetMap road data and GeoExt”, available in: /usr/share/pgrouting/workshop/

Note

To update to the latest version of the tutorial, open a terminal window, then run sudo apt-get update && apt-get upgrade pgrouting-workshop

Copyright & Disclaimer