1. Prepare Data

../_images/prepareData.png

To be able to use pgRouting, data has to be imported into a database.

1.1. Prepare the database

pgRouting is installed as extension. This requires:

  • PostgreSQL

  • PostGIS

These requirements are met on OSGeoLive. When the required software is installed, open a terminal window by pressing ctrl-alt-t and follow the instructions. Information about installing OSGeoLive can be found on OSGeoLive Installation.

Note

If OSGeoLive is not being used, please refer to the chapter’s appendix to set up the user user.

1.1.1. Create a pgRouting compatible database

Note

Depending on the postgres configuration -U <user> is needed on psql commands

 1# Create the database
 2createdb city_routing
 3
 4# login as user "user"
 5psql city_routing << EOF
 6
 7-- add PostGIS functions
 8CREATE EXTENSION postgis;
 9
10-- add pgRouting functions
11CREATE EXTENSION pgrouting;
12
13-- Inspect the pgRouting installation
14\dx+ pgrouting
15
16-- View pgRouting version
17SELECT pgr_version();
18
19EOF

Note

To exit the database use \q

1.2. Get the Workshop Data

The pgRouting workshop will make use of OpenStreetMap data, which is already available on OSGeoLive. This workshop will use the Prizren city data and is a snapshot of March 2023.

1.2.1. Getting the data

1.2.1.1. Option 1) When using OSGeoLive

OSGeoLive comes with osm data from the city of Prizren.

CITY="Prizren_XK"
bzcat ~/data/osm/$CITY.osm.bz2 > $CITY.osm

1.2.1.2. Option 2) Download data form OSGeoLive website

The exact same data can be found on the OSGeoLive download page.

1CITY="Prizren_XK"
2wget -N --progress=dot:mega "https://download.osgeo.org/livedvd/16/osm/$CITY.osm.bz2"
3
4bunzip2 -f "$CITY.osm.bz2"

1.2.1.3. Option 3) Download using Overpass XAPI

The following downloads the latest OSM data on using the same area. Using this data in the workshop can generate variations in the results, due to changes since March 2023.

CITY="Prizren_XK"
BBOX="21.1922265,42.0816364,20.2206175,42.6653875"
wget --progress=dot:mega -O "$CITY.osm" "http://www.overpass-api.de/api/xapi?*[bbox=\$\{BBOX\}][@meta]"

More information about how to download OpenStreetMap data can be found in https://wiki.openstreetmap.org/wiki/Downloading_data

An alternative for very large areas is to use the download services of Geofabrik.

1.3. Upload data to the database

The next step is to run osm2pgrouting converter, which is a command line tool that inserts the data in the database, “ready” to be used with pgRouting. Additional information about osm2pgrouting can be found at the Appendix: osm2pgrouting Import Tool

For this step:

  • the osm2pgrouting default mapconfig.xml configuration file is used

  • and the ~/Desktop/workshop/Prizren_XK.osm data

  • with the city_routing database

From a terminal window ctrl-alt-t.

1.3.1. Run the osm2pgrouting converter

1/usr/bin/osm2pgrouting \
2    -f "Prizren_XK.osm" \
3    -c "/usr/share/osm2pgrouting/mapconfig.xml" \
4    -d city_routing \
5    -U user \
6    -W user \
7    --clean

Note

Depending on the osm2pgrouting version -W password is needed

Output:

1/usr/bin/osm2pgrouting \
2    -f "Prizren_XK.osm" \
3    -c "/usr/share/osm2pgrouting/mapconfig.xml" \
4    -d city_routing \
5    -U user \
6    -W user \
7    --clean

1.3.2. Tables on the database

psql -d city_routing -c "\d"

If everything went well the result should look like this:

                   List of relations
 Schema |           Name           |   Type   | Owner  
--------+--------------------------+----------+--------
 public | configuration            | table    | user
 public | configuration_id_seq     | sequence | user
 public | geography_columns        | view     | runner
 public | geometry_columns         | view     | runner
 public | pointsofinterest         | table    | user
 public | pointsofinterest_pid_seq | sequence | user
 public | spatial_ref_sys          | table    | runner
 public | ways                     | table    | user
 public | ways_gid_seq             | sequence | user
 public | ways_vertices_pgr        | table    | user
 public | ways_vertices_pgr_id_seq | sequence | user
(11 rows)

1.4. Chapter: Appendix

OSGeoLive’s account name on the database is user. To easily use the workshop when not using OSGeoLive this extra steps are needed:

# work on the home folder
cd

# login to postgres
psql -U postgres

-- Create "user"
CREATE ROLE "user" SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN PASSWORD 'user';

-- exit psql
\q

# Add the user to .pgpass
echo :5432:*:user:user >> .pgpass