Calculating the shortest paths with the QGIS network analysis library
As mentioned in the recipe, Calculating the shortest paths using the Road graph plugin, QGIS comes with a network analysis library, which can be used from the Python console, inside plugins, to process scripts, and basically anything else that you can think of. In this recipe, we will introduce the usage of the network analysis to compute the shortest paths in the Python console.
Getting ready
To follow this recipe, load network_pgr.shp
from the sample data.
How to do it…
Instead of typing or copying the following script directly in the Python console, we recommend opening the Python console editor using the Show editor button on the left-hand side of the Python console:
Paste the following script into the editor:
import processing from processing.tools.vector import VectorWriter from PyQt4.QtCore import * from qgis.core import * from qgis.networkanalysis import * # create the graph layer = processing.getObject('network_pgr...