def showShortestPath(self, startPoint, endPoint): # taken from Qgis Py Cookbook #startPoint = self.ptList[0][0].asPoint() #endPoint = self.ptList[1][0].asPoint() tiedPoints = self.director.makeGraph(self.builder, [startPoint, endPoint]) tStart, tStop = tiedPoints graph = self.builder.graph() idxStart = graph.findVertex(tStart) tree = QgsGraphAnalyzer.shortestTree(graph, idxStart, 0) idxStart = tree.findVertex(tStart) idxEnd = tree.findVertex(tStop) if idxEnd == -1: raise Exception('No route!') # Add last point route = [tree.vertex(idxEnd).point()] # Iterate the graph while idxEnd != idxStart: edgeIds = tree.vertex(idxEnd).incomingEdges() if len(edgeIds) == 0: break edge = tree.edge(edgeIds[0]) route.insert(0, tree.vertex(edge.fromVertex()).point()) idxEnd = edge.fromVertex() # This may require coordinate transformation if project's CRS # is different than layer's CRS for p in route: self.rbShortPath.addPoint(p) return route
def calcShortestTree(self, startpoint_id, criterion): tree = QgsGraphAnalyzer.shortestTree(self.network, startpoint_id, criterion) return tree