[GIS] Why does the algorithm shortestpathpointtopoint in QGIS3 using pyqgis only work in the second run

pyqgisqgis-3qgis-processing

A part of my skript uses the shortestpathpointtopoint algorithm, but it does only work in the second run (Python Console within QGIS). The discussed part of the script looks like

points_lyr = QgsProject.instance().mapLayersByName(u'cabinets')[0]
network_lyr = QgsProject.instance().mapLayersByName(u'pipes')[0]

for f in points_lyr.getFeatures():
    if f.attribute('Art') == 2:
        pStart = QgsPointXY(f.geometry().asPoint().x(),f.geometry().asPoint().y())
    elif f.attribute('Art') == 4:
        pStop = QgsPointXY(f.geometry().asPoint().x(),f.geometry().asPoint().y())

print('Start: ', pStart)
print('Stop: ', pStop)

parameters = {'INPUT': network_lyr,
                'STRATEGY': 0,
                'DIRECTION_FIELD': '',
                'VALUE_FORWARD': '',
                'VALUE_BACKWARD': '',
                'VALUE_BOTH': '',
                'DEFAULT_DIRECTION': 2,
                'SPEED_FIELD': '',
                'DEFAULT_SPEED': 1,
                'TOLERANCE': 0,
                'START_POINT': pStart,
                'END_POINT': pStop,
                'OUTPUT': 'memory:'}

my_line = processing.run('qgis:shortestpathpointtopoint', parameters)

This works perfectly, but only in the second run. In the first run I get the following error:

Traceback (most recent call last): File
"C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode
exec(code, self.locals) File "", line 1, in File "", line 37, in File
"C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\tools\general.py",
line 96, in run
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File
"C:/PROGRA~1/QGIS3~1.4/apps/qgis/./python/plugins\processing\core\Processing.py",
line 139, in runAlgorithm
raise QgsProcessingException(msg)
_core.QgsProcessingException: Unable to execute algorithm Incorrect parameter value for START_POINT

The prints of start&stop work in the first run. I don't get what I am doing wrong here.

Best Answer

After trying another input-type, it worked

for f in points_lyr.getFeatures():
    if f.attribute('Art') == 2:
        #pStart = QgsPointXY(f.geometry().asPoint().x(), f.geometry().asPoint().y())
        pStart = str(f.geometry().asPoint().x()) + ',' + str(f.geometry().asPoint().y())
    else:
        continue

for f in points_lyr.getFeatures():
    if f.attribute('Art') == 4:
        #pStop = QgsPointXY(f.geometry().asPoint().x(), f.geometry().asPoint().y())
        pStop = str(f.geometry().asPoint().x()) + ',' + str(f.geometry().asPoint().y())

parameters = {'INPUT': network_lyr,
                'STRATEGY': 0,
                'DIRECTION_FIELD': '',
                'VALUE_FORWARD': '',
                'VALUE_BACKWARD': '',
                'VALUE_BOTH': '',
                'DEFAULT_DIRECTION': 2,
                'SPEED_FIELD': '',
                'DEFAULT_SPEED': 1,
                'TOLERANCE': 0,
                'START_POINT': pStart,
                'END_POINT': pStop,
                'OUTPUT': 'memory:'}

So it does not work in the first run, when I use QgsPointXY, but it works when the input is a string. Don't know why, but I can work with strings as well.

Related Question