I built a PlugIn with the 'native:shortestpathpointtopoint' tool to find the shortest path between two points on a multipolyline. When I run the Plugin I want to get a message with information about wether the params are valid and the processing is actualliy feasable!

I can check the params for correct datatype, but how can i pre-check if the 'native:shortestpathpointtopoint' will actually work or not without getting a critical error. It wont work, when the two points are not connected by the polyline. I don't want a cryptic QGIS error but rather a: 'Pls check your points' messagebox.

I tried stuff like this but those are already pretty desperate tries:

def getPath(self):

    print('Pathfinder START')

    params = {
        'INPUT': lyr,
        'STRATEGY': 0,
        'TOLERANCE': 2,
        'START_POINT': self.Startpunkt,
        'END_POINT': self.Endpunkt,
        'OUTPUT': os.path.dirname(__file__) + '\\shortestPath\\shortestPath.shp'
    if 'native:shortestpathpointtopoint'.canExecute():

        processing.run('native:shortestpathpointtopoint', params)

        print('Check your points')

    print('Pathfinder END')
  • 1
    Instead of if ....canExecute() ... else:, have you tried try: processing.run(...) except my_error_type: print(....) ? – J. Monticolo Aug 8 '19 at 12:01
  • 1
    yeah tried that before but didn't work. But somehow it works now! try: processing.run('native:shortestpathpointtopoint', params) QMessageBox.information(None, ...') except: QMessageBox.critical(None, '...!') This works perfectly!! So easy... thank you – MarcoGIS Aug 8 '19 at 12:49
  • So, can you post your valid code and description as answer please ? And welcome to GIS StackExchange. – J. Monticolo Aug 8 '19 at 13:11

It works fine this way:

            processing.run('native:shortestpathpointtopoint', params)

            QMessageBox.critical(None, '...', '...!')

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.