Last Updated: February 25, 2016
·
1.943K
· Jérome Freyre

PGRouting Basic queries

pgRouting is an extension of PostGIS which is an extension of PostgreSQL :)

PostGIS permit user to access an awesome list of queries "geo-specialized" and pgRouting add the routing logic.

Installation

With pgRouting 2.0 install is really easy

sudo add-apt-repository ppa:georepublic/pgrouting
sudo apt-get update
sudo apt-get install postgresql-9.1-pgrouting

Build a network

Using osm2pgrouting you can import a extract of OpenStreetMap data into your database

sudo apt-get install osm2pgrouting
wget -O lausanne.osm http://overpass.osm.rambler.ru/cgi/xapi_meta?*[bbox=6.6072,46.5041,6.6593,46.5466]
osm2pgrouting -file ./fichier.osm
-conf ./mapconfig.xml
-dbname ma_base
-user user
-passwd user
-clean

Basic queries

Get the shortest path

SELECT st_union(route.the_geom)
FROM (
  SELECT * FROM pgr_dijkstra('ways', START_NODE, END_NODE)
) as route

Generate an isochrone

SELECT 1 as id, ST_ConcaveHull(st_union(the_geom), 0.95)
FROM (
  SELECT ways_vertices_pgr.the_geom
    FROM pgr_drivingDistance(
      'SELECT gid as id, source, target, st_astext(the_geom), ST_Length(the_geom::geography) as cost FROM ways', 
      NODE_ID, 
      250, 
      false, 
      false)
    INNER JOIN ways on (ways.gid = id2)
    INNER JOIN ways_vertices_pgr on (ways_vertices_pgr.id = ways.source)
) as t