2

I am using Network Analysis > Shortest path (point to point) to compute the distance between two points, following a street network.

Here is the code:

n=0
for f1 in layer1:
     #Coordinates from first layer
     geom1 = f1.geometry()
     x1=geom1.asPoint().x()
     y1=geom1.asPoint().y()
     x1str=str(x1)
     y1str=str(y1)
     coord1= x1str+","+y1str
     
     for f2 in layer2:
         #Coordinates from the second layer
         geom2 = f2.geometry()
         x2=geom2.asPoint().x()
         y2=geom2.asPoint().y()
         x2str=str(x2)
         y2str=str(y2)
         coord2= x2str+","+y2str
         n=n+1
         n=str(n)
         

         processing.run("native:shortestpathpointtopoint", {
            'INPUT':'./Do_Straßen_EPSG 25832.shp|layername=Do_Straßen_EPSG 25832',
            'STRATEGY':0,
            'DIRECTION_FIELD':'',
            'VALUE_FORWARD':'',
            'VALUE_BACKWARD':'',
            'VALUE_BOTH':'',
            'DEFAULT_DIRECTION':2,
            'SPEED_FIELD':'',
            'DEFAULT_SPEED':50,
            'TOLERANCE':0,
            'START_POINT':''+coord1+'',
            'END_POINT':''+coord2+'',
            'OUTPUT':'./ShortPath/layer'+n+'.shp'})
         
         n=int(n)

In my first trial, the iteration stopped after 207 features from 1500. The error message showed me, that there was no route, so my iteration stopped.

How can I build in a safe for that case, when no route was found?

I thought of an if statement, but I can't imagine the expression.

3
  • Maybe a try-catch structure instead of an if statement? That way you can handle whatever error is produced by no route and move on to the next feature. Sep 10 at 20:31
  • I've tried a try-except structure right now, I think it's what you meant with "catch". Tomorrow we will see if it's the right solution. Thanks
    – divko
    Sep 10 at 20:38
  • maybe the try-except-else-approach can help u
    – Taras
    Sep 11 at 7:25
4

Try something like that:

n=0
for f1 in layer1:
    p1 = f1.geometry().asPoint()
    x1, y1 = p1.x(), p1.y()
    coord1= f"{x1},{y1}"
     
    for f2 in layer2:
        p2 = f2.geometry().asPoint()
        x2, y2 = p2.x(), p2.y()
        coord2= f"{x2},{y2}"
        n += 1

        try:
            processing.run("native:shortestpathpointtopoint", {
                'INPUT':'./Do_Straßen_EPSG 25832.shp|layername=Do_Straßen_EPSG 25832',
                'STRATEGY':0,
                'DEFAULT_DIRECTION':2,
                'DEFAULT_SPEED':50,
                'TOLERANCE':0,
                'START_POINT':f"'{coord1}'", 
                'END_POINT':f"'{coord2}'",
                'OUTPUT':'./ShortPath/layer'+str(n)+'.shp'})
            
        except QgsProcessingException:
            print(f"There is no route from id_{f1.id()} to id_{f2.id()}.")
        except: # for other errors
            print(f"ERROR - id1:{f1.id()}, id2:{f2.id()}")

I removed unnecessary parts, like converting coords and n to string. No need to specify empty parameters in processing.run.

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.