1

Using PyQGIS I can calculate the shortest path from point to point. But I need to calculate the shortest path based on two network layers: A and B. So that the result should be the shortest path from point to point following the lines of either layer A or layer B.

Here is the code I am using for calculating the shortest path using one network layer:

params = {'DEFAULT_DIRECTION' : 2, 'DEFAULT_SPEED' : 50, 'DIRECTION_FIELD' : None, 'START_POINT' : srcPointStr, 
          'END_POINT' : destinationPointStr, 'INPUT' : QgsProcessingFeatureSourceDefinition('duct_b9e0ae0f_53bf_4875_9f30_e6e5e43060dc', True), 
          'OUTPUT' : 'TEMPORARY_OUTPUT', 'SPEED_FIELD' : None, 'STRATEGY' : 0, 'TOLERANCE' : 0, 'VALUE_BACKWARD' : '', 'VALUE_BOTH' : '', 
          'VALUE_FORWARD' : ''}
try:
   result = processing.run('qgis:shortestpathpointtopoint', params)
except QgsProcessingException:
   self.showErrorMessage("Error occures during Shortest Path Calculation.", "Error")

I have tried to pass an array of layers IDs as the INPUT, but it throws an error:

"Unable to execute algorithm Could not load source layer for INPUT: invalid value"
0

I would suggest using from networkx.algorithms.shortest_paths.generic import shortest_path as it utilizes Dijkstra's Theorem in an easily packaged way. Also, try dissolving your input edge layer (guessing from your variable names, maybe your road file?) into itself to see if that makes it easier for your computer to read. This can easily be done in QGIS using the dissolve function and helped me a lot when I was doing some shortest pathfinding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.