[QGIS-Developer] Reusing network graph

Prem Kumar prem.netgis at gmail.com
Thu Dec 30 05:46:41 PST 2021


Hi Team,

I have been trying to get the shortest path for a lot of pairs of start and
end points for a given road layer. Although I'm able to achieve this by
creating the *QgsVectorLayerDirector *and Graph object for every pair of
start and end points.

However, I could not reuse the graph object or director object for all the
pairs of start and end points, eventually the code is as slow as executing
the* 'native:shortestpathpointtopoint'* processing tool
programmatically for each pair of start and end points.

Essentially, I'm trying to improve the performance by reusing the graph
objects. Can someone help me whether there is a possibility to reuse the
graph object for large sets of pairs? Here is the code snippet I have used.

vectorLayer = QgsVectorLayer('testdata/network.gpkg|layername=network_lines',
'lines')director = QgsVectorLayerDirector(vectorLayer, -1, '', '', '',
QgsVectorLayerDirector.DirectionBoth)strategy =
QgsNetworkDistanceStrategy()director.addStrategy(strategy)builder =
QgsGraphBuilder(vectorLayer.sourceCrs())startPoint =
QgsPointXY(1179661.925139,5419188.074362)endPoint =
QgsPointXY(1180942.970617,5420040.097560)tiedPoints =
director.makeGraph(builder, [startPoint, endPoint])tStart, tStop =
tiedPointsgraph = builder.graph()idxStart =
graph.findVertex(tStart)idxEnd = graph.findVertex(tStop)(tree, costs)
= QgsGraphAnalyzer.dijkstra(graph, idxStart, 0)

-Prem
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20211230/acf709c0/attachment.html>


More information about the QGIS-Developer mailing list