4

So I'm trying to implement a routing algorithm in my QGIS 3 plugin that I'm writing using Python.

I think I could resolve all the problems concerning the break between QGIS 2 and 3, but my resulting tree gives me for all nodes in it just a -1 value which is for not connected nodes.

If I have for example a project that looks like this:

enter image description here

Where the red Square is my start point and the yellow circle my end point I get back a tree that looks like this [-1,-1,-1]

the code I use is the following:

First I select the Line layer and the startPt and endPt

director = QgsVectorLayerDirector(layer, -1, '', '', '', 3) 

strategy = QgsNetworkDistanceStrategy()

director.addStrategy(strategy)

builder = QgsGraphBuilder(crs) #crs = project.crs() which is in this case WGS84

tiedPoints = director.makeGraph(builder, [startPt, endPt])
graph = builder.graph()   

startVertex = graph.findVertex(startPt)

(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, startVertex,0)

I just don't know where it goes wrong.

1

I lost whole week to migrate my old dijkstra code from QGIS/Python 2x to 3x... Finally I found out that some issues in qgis.anylysis (QgsVectorLayerDirector, QgsGraphBuilder, QgsGraphAnalyzer) are still in test phase, so you might try to call built in processing on similar way I did:

            feedback = QgsProcessingFeedback()
            crsID = " [EPSG:3765]"
            fromPointAsString = str(fromPoint.x()) + ", " + str(fromPoint.y()) + crsID
            targetPointAsString = str(tgtPoint.x()) + ", " + str(tgtPoint.y()) + crsID
            SA = fromPointAsString 
            NA = targetPointAsString 
            parameters = {'INPUT': layer,
            'STRATEGY': 0,
            'DIRECTION_FIELD': '',
            'VALUE_FORWARD': '',
            'VALUE_BACKWARD': '',
            'VALUE_BOTH': '',
            'DEFAULT_DIRECTION': 2,
            'SPEED_FIELD': '',
            'DEFAULT_SPEED': 1,
            'TOLERANCE': 0,
            'START_POINT': SA,
            'END_POINT': NA,
            'OUTPUT': 'memory:'}
            try:
                branch = processing.run('qgis:shortestpathpointtopoint', parameters, feedback = feedback)
                QgsMessageLog.logMessage("Cost: " + repr(branch['TRAVEL_COST']))
                for ft in branch['OUTPUT'].getFeatures():
                    rememberedPaths.append(ft.geometry().asPolyline())
                    rememberedCosts.append(branch['TRAVEL_COST'])
                branch = None
            except QgsProcessingException as e:
                QgsMessageLog.logMessage("Path not found or some other error in dijkstra...")
                branch = None
    return rememberedPaths, rememberedCosts

PS: first you have to load and initialize:

import processing
processing.core.Processing.Processing.initialize()

PS1: I apologize for editing code above ('SA' and 'NA' strings, which mean 'FROM' and 'TO' on croatian), but 'TOLERANCE' problem of Yours makes me wonder if your topology is homogenous (without gaps, repeats, crossings, unequal number of decimals on start/end coordinate, etc.) I agree with You that old code in (QGIS2x/Python2x) has been much clearer, plus seemed that has worked faster too..

  • And that does work for you? I tried the same, but I rarely get good results without a tolerance value (1 or 2). My old dijkstra code worked so wonderfully. – AndreasK Feb 15 at 21:46
  • I haven't gotten the processing to work and gave up. My project is basically on hold until it works the way it did before – chrisd Mar 11 at 10:05

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.