Question

while loop

  • 26 February 2020
  • 1 reply
  • 7 views

Badge

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?


1 reply

Badge +3

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])

 

Reply