Graphics Reference
In-Depth Information
The Find Shortest Path action
This is the last feature of the ForestTrails that we will have to implement. When the user
clicks on this toolbar icon, we want to calculate the shortest available path between the giv-
en start and end points. Fortunately, the QGIS network analysis library will do the actual
calculation for us. All we have to do is run the shortest path calculation on the track layer,
build the LineString that corresponds to this shortest path, and display that LineString geo-
metry in our memory-based map layer.
All of this logic will be implemented within the findShortestPath() method. We'll
start our implementation with a bit of housekeeping: if the user unchecks the Find Shortest
Path toolbar icon, we clear the shortest path memory layer, switch back to the panning
mode, and redraw the map canvas to show the map without the previous path:
def findShortestPath(self):
if not self.actionFindShortestPath.isChecked():
self.clearMemoryLayer(self.shortestPathLayer)
self.setPanMode()
self.mapCanvas.refresh()
return
The rest of the method will be executed when the user clicks on the Find Shortest Path
toolbar action to check it. Add the following code to your method:
directionField = self.trackLayer.fieldNameIndex(
"direction")
director = QgsLineVectorLayerDirector(
self.trackLayer, directionField,
TRACK_DIRECTION_FORWARD,
TRACK_DIRECTION_BACKWARD,
TRACK_DIRECTION_BOTH, 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = self.mapCanvas.mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)
Search WWH ::




Custom Search