This recipe shows you how to import a line layer into PostGIS and create a routable network out of it, which can be used by PostGIS's routing library, pgRouting. (For details about pgRouting, please visit the project website at http://docs.pgrouting.org.)
The installation of PostGIS with pgRouting won't be covered in detail here because instructions for the different operating systems can be found on the project's website at http://docs.pgrouting.org/2.0/en/doc/src/installation/index.html.
If you are using Windows, both PostGIS and pgRouting can be installed directly from the Stack Builder application, which is provided by the standard PostgreSQL installation, as described at http://anitagraser.com/2013/07/06/pgrouting-2-0-for-windows-quick-guide/.
To follow this exercise, you need a PostGIS database with pgRouting enabled. In QGIS, you should set up the connection to the database using the New button in the Add PostGIS Layers dialog. Additionally, you should load network_pgr.shp from the sample data.
These steps will create a routable network table in your PostGIS database:
network_pgr layer into your database, as shown in the following screenshot:
network_pgr has been imported successfully, open the SQL window of DB Manager by pressing F2, clicking on the corresponding toolbar button, or in the Database menu.network_pgr with QGIS's DB Manager, it creates the cost column as numeric. As pgRouting won't accept numeric, we will use Table | Edit Table in DB Manager to edit the cost column. Click on the Edit column button and change Type from numeric to double precision (which equals the required float8).network_pgr_vertices_pgr table, which contains the computed network nodes:SELECT pgr_createTopology('network_pgr',0.001);16 to the node number 9:SELECT pgr_dijkstra('SELECT id, source, target, cost
FROM network_pgr', 16, 9, false, false);This will result in the following:
(0,16,6,1) (1,17,7,1) (2,5,8,1) (3,6,9,1) (4,11,15,1) (5,9,-1,0)
The preceding pgr_dijkstra query consists of the following parts:
'SELECT id, source, target, cost FROM network_pgr': This is a SQL query, which returns a set of rows with the following columns:id: This is the unique edge ID (type int4)source: This is the ID of the edge source node (type int4)target: This is the ID of the edge target node (type int4)cost: This is the cost of the edge traversal (type float8)16, 9: These are the IDs of the route source and target nodes (type int4)false: This is true if the graph is directedfalse: If true, the reverse_cost column of the SQL-generated set of rows will be used for the cost of the traversal of the edge in the opposite directionThe results of pgr_dijkstra contain the list of network links that our route uses to get from the start to the destination. The four values in reach result row are as follows:
seq: This is the sequence number, which tells us the order of the links within the route starting from 0id1: This is the node IDid2: This is the edge IDcost: This is the cost of the link (can be distance, travel time, a monetary value, or any other measure that you chose)