Skip to main content
Question

while loop

  • February 26, 2020
  • 1 reply
  • 41 views

Forum|alt.badge.img

Hello!

I have a python script that calculates slope between points. I want to complete it with a while loop, to calculate between only those points where _lineID is equal, so like:

while _lineID == _lineID
     z_kulonbseg = b_z - a_z     
     slope_degree = (z_kulonbseg / a_t) * 100
     and so on....
import fme
import fmeobjects
from functools import reduce
from math import sin, pi
def left_slope(feature_a, feature_b):    
    a_x = float(feature_a.getAttribute('x'))    
    a_y = float(feature_a.getAttribute('y'))
    a_z = float(feature_a.getAttribute('z_uj'))
    a_t = float(feature_a.getAttribute('tavolsag_szomszed')) 
    b_x = float(feature_b.getAttribute('x'))
    b_y = float(feature_b.getAttribute('y'))
    b_z = float(feature_b.getAttribute('z_uj'))
    b_t = float(feature_b.getAttribute('tavolsag_szomszed'))
    ######  WHILE LOOP
    z_kulonbseg = b_z - a_z
    slope_degree = (z_kulonbseg / a_t) * 100
    if slope_degree > 0:
        elojel = "1"
    else:
        elojel = "0"
    feature_a.setAttribute('_slope', slope_degree)
    feature_a.setAttribute('_elojel', elojel)
    return feature_b 
class FeatureProcessor(object):
    def __init__(self):
        self.features = []
    def input(self,feature):
        self.features.append(feature)
    def close(self):
        reduce(left_slope, self.features) 
        for feature in self.feature
            self.pyoutput(feature)

 

Can you help me to correct it?

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.

1 reply

bwn
Evangelist
Forum|alt.badge.img+26
  • Evangelist
  • February 26, 2020

An alternate Solution, but the slope calculation formula and logic in the Python script doesn't appear to need Python.

This can be accomplished by:

  • Using Sorter to put the Points in order of processing
  • Sending these to AttributeCreator with Adjacent Feature Processing turned On

The Attribute Value of Slope then just becomes a Conditional Value (Right click the Attribute Value setting box to access this)

If LineID != LineID[-1] (ie. Preceding feature has a different LineID so this feature must be the first point in the slope gradient)

 

Then Slope = Null

Else Slope = (z - z[-1]) / (x - x[-1])