Skip to main content
Question

Make Python Script more efficiant?

  • July 19, 2019
  • 7 replies
  • 17 views

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 = []

 

nearestList = []

 

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!

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

7 replies

stalknecht
Contributor
Forum|alt.badge.img+21
  • Contributor
  • July 21, 2019

Maybe the ShortestPathFinder or the NeighbourFinder can help you.


  • Author
  • July 21, 2019

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!


ebygomm
Influencer
Forum|alt.badge.img+45
  • Influencer
  • July 21, 2019

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?


  • Author
  • July 21, 2019

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.


ebygomm
Influencer
Forum|alt.badge.img+45
  • Influencer
  • July 21, 2019

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


david_r
Celebrity
  • July 22, 2019

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


  • Author
  • July 22, 2019

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!