Skip to main content

Hey!

I've written this Python script that is comparing three diffrent path distances from a starting point and the three nearest target Points, and choosing the shortest one to be output. Does anyone have a clue if I can make it more efficient? Atm it takes a long time since it iterates through ca 82000*82000 features...

 

class FeatureProcessor(object):

 

def __init__(self):

 

self.featureList = ]

 

 

def input(self,feature):

 

self.featureList.append(feature)

 

 

def close(self):

 

r = int(len(self.featureList)/3)

 

for i in range(1, r+1):

 

valueList = a]

 

nearestList = a]

 

for feature in self.featureList:

 

j = int(feature.getAttribute('gid'))

 

if j == i:

 

nearestList.append(feature)

 

for feature in nearestList:

 

value = feature.getAttribute('Totalt_Avstånd')

 

valueList.append(value)

 

minValue = min(valueList)

 

for feature in nearestList:

 

if feature.getAttribute('Totalt_Avstånd') == minValue:

 

self.pyoutput(feature)

 

If someone has a different solution than using Python for this, please feel free to suggest it!

Thank you!

Maybe the ShortestPathFinder or the NeighbourFinder can help you.


Maybe the ShortestPathFinder or the NeighbourFinder can help you.

I’m using both of them before getting input into the python script. My problem is how to choose the shortest path of each starting point and its three nearest target points!


Can you give a bit more information on the input going into the pythoncaller? Do you have 3 features per 'gid' and need to find the shortest?


Can you give a bit more information on the input going into the pythoncaller? Do you have 3 features per 'gid' and need to find the shortest?

Exactly, so if we would say that I start with 2 different starting points with gid-attribute 1 and 2, I use the neighborfinder to get three nearest target points to each starting point. Then I create from to lines between the starting points and their three targets which results in 6 features that I send to shortestpathfinder. The output from that is what is send into the pythoncaller, and I want to find the shortest path of the three paths connected to each starting point.


Exactly, so if we would say that I start with 2 different starting points with gid-attribute 1 and 2, I use the neighborfinder to get three nearest target points to each starting point. Then I create from to lines between the starting points and their three targets which results in 6 features that I send to shortestpathfinder. The output from that is what is send into the pythoncaller, and I want to find the shortest path of the three paths connected to each starting point.

Sorting by length and then using a duplicate filter or sampler grouping by the 'gid' would identify the shortest


Have a look at the reply I provided here, it should be fairly easy to adapt to your use case. It uses the scipy package which are calling compiled C libraries, and should therefore be lightening fast.

https://knowledge.safe.com/questions/94411/find-the-nearest-neighbor-of-a-nearest-neighbor-of.html


Sorting by length and then using a duplicate filter or sampler grouping by the 'gid' would identify the shortest

Sometimes it is that easy, thank you!


Reply