The following will install
pgRouting and its dependencies including
PostgreSQL if not installed:
brew install pgrouting
osm2pgrouting is for importing OSM data into PostgreSQL database, and creating routable topology.
It is said that osm2pgrouting has limitation:
Can’t process “large” files, continents, big countries, or very large states.
For large files, we can use osm2po.
brew install osm2pgrouting
This is not the latest version (v2.0).
Note that a mapconfig.xml file is at:
(The following steps are based on version 2.1.)
Download code from repo
CMakeLists.txtto install app to
/usr/local/binand other files to
Add the following headers to
src/way.h (otherwise there will be errors)
#include <string> #include <sstream> #include <iostream>
Build and install
cmake -H. -Bbuild cd build/ make make install
Create new database if needed:
For each database that postgis and pgrouting are used, we should install corresponding extensions:
psql mydb -c "create extension postgis" psql mydb -c "create extension pgrouting"
For test of installation:
psql mydb -c "SELECT postgis_full_version()" psql mydb -c "SELECT pgr_version()"
OSM map data can be downloaded in many places e.g. Mapzen. We are interested in the OSM XML format.
osm2pgrouting installed manually:
osm2pgrouting --file beijing_china.osm \ --conf /usr/local/share/osm2pgrouting/mapconfig.xml \ --dbname mydb \ --username username \ --clean
If you are adding data, do not use flag
We could browse the tables with
psql mydb -c "\dt"
Two main tables:
ways: stores the edges
ways_vertices_pgr: stores the end nodes of edges
reverse_cost_s. According to post here:
For osm2pgrouting V2.1:
lengthis length of the segment in degree units
reverse_costis the length in degree units. (include the negative values for wrong way)
length_mis in meters (there is no cost_m or reverse_cost_m)
reverse_cost_sis in time: seconds units (using the maxspeed value that is in km/hr transforming it to meters/second and using the
For example, use pgRouting functions to calculate one-to-one shortest path: doc.
SELECT * FROM pgr_dijkstra( 'SELECT gid as id, source, target, cost, reverse_cost FROM ways', 100, 200, TRUE );