Skip to main content
Solved

python caller with three arguments

  • February 18, 2020
  • 4 replies
  • 11 views

Forum|alt.badge.img

Hello!

I would like to calculate the difference of z value between two neighboor points in the following way:

- test if the extreme_ is LOKMIN

- if it is LOKMIN , then take the two neighbor value (the points are sorted), take the higher (so the bigger number) and exract them each other.

Here is my attempt:

import fme
import fmeobjects
from functools import reduce
from math import sin, pi
def left_slope(feature_a, feature_b, feature_c):
    a_x = float(feature_a.getAttribute('z_uj'))
    a_sz = feature_a.getAttribute('_szelsoertek')
    b_x = float(feature_b.getAttribute('z_uj'))
    b_sz= feature_b.getAttribute('_szelsoertek')
    c_x = float(feature_c.getAttribute('z_uj'))
    if b_sz == "LOKMIN":
        k = c_x - a_x
        #_nyomvalyu = b_x - a_x
        feature_b.setAttribute('_nyomvalyu', k)
    return feature_a
    return feature_c
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.features:
            self.pyoutput(feature)

 

But it gives an error, can you help me?

Python Exception <TypeError>: left_slope() takes exactly 3 arguments (2 given)
Traceback (most recent call last):
  File "<string>", line 24in close
TypeError: left_slope() takes exactly 3 arguments (2 given)
PythonCaller_5(PythonFactory): PythonFactory failed to close properly
PythonCaller_5(PythonFactory): A fatal error has occurred. Check the logfile above for details

Best answer by paalped

Here is a quick  solution to Your question:

import fme
import fmeobjects

def mid(feature_a, feature_b, feature_c):
    a_x  = float(feature_a.getAttribute('z_uj'))
    a_sz = feature_a.getAttribute('_szelsoertek')
    b_x  = float(feature_b.getAttribute('z_uj'))
    b_sz = feature_b.getAttribute('_szelsoertek')
    c_x  = float(feature_c.getAttribute('z_uj'))
    if b_sz == "LOKMIN":
        k = c_x - a_x
        #_nyomvalyu = b_x - a_x
        feature_b.setAttribute('_nyomvalyu', k)

class FeatureProcessor(object):
    def __init__(self):
        self.features = []
    def input(self,feature):
        self.features.append(feature)
    def close(self):
        end_index = len(self.features)
        num_args = 3 
        for i in range(end_index - num_args):
            mid(*self.features[i:i+num_args])
        for feature in self.features:
            self.pyoutput(feature)

 

 

View original
Did this help you find an answer to your question?

4 replies

becchr
Influencer
Forum|alt.badge.img+25
  • Influencer
  • February 18, 2020

hi @gylona, if i understand well what you try to do, i think you can avoid python by using the 'adjacent feature attributes' possibilities of FME?

https://www.safe.com/blog/2017/08/adjacentfeatureattributes-evangelist166/

 


david_r
Evangelist
  • February 18, 2020

The function left_slope passed to reduce() should only have two parameters. Check the documentation to reduce(): https://docs.python.org/3/library/functools.html#functools.reduce

A more general comment: you have two consecutive return statements (lines 15 and 16), but only the first will ever be executed. If you need to return both feature_a and feature_c, do (if reduce allows it):

return feature_a, feature_c

paalped
Contributor
Forum|alt.badge.img+5
  • Contributor
  • Best Answer
  • February 19, 2020

Here is a quick  solution to Your question:

import fme
import fmeobjects

def mid(feature_a, feature_b, feature_c):
    a_x  = float(feature_a.getAttribute('z_uj'))
    a_sz = feature_a.getAttribute('_szelsoertek')
    b_x  = float(feature_b.getAttribute('z_uj'))
    b_sz = feature_b.getAttribute('_szelsoertek')
    c_x  = float(feature_c.getAttribute('z_uj'))
    if b_sz == "LOKMIN":
        k = c_x - a_x
        #_nyomvalyu = b_x - a_x
        feature_b.setAttribute('_nyomvalyu', k)

class FeatureProcessor(object):
    def __init__(self):
        self.features = []
    def input(self,feature):
        self.features.append(feature)
    def close(self):
        end_index = len(self.features)
        num_args = 3 
        for i in range(end_index - num_args):
            mid(*self.features[i:i+num_args])
        for feature in self.features:
            self.pyoutput(feature)

 

 


paalped
Contributor
Forum|alt.badge.img+5
  • Contributor
  • February 19, 2020
paalped wrote:

Here is a quick  solution to Your question:

import fme
import fmeobjects

def mid(feature_a, feature_b, feature_c):
    a_x  = float(feature_a.getAttribute('z_uj'))
    a_sz = feature_a.getAttribute('_szelsoertek')
    b_x  = float(feature_b.getAttribute('z_uj'))
    b_sz = feature_b.getAttribute('_szelsoertek')
    c_x  = float(feature_c.getAttribute('z_uj'))
    if b_sz == "LOKMIN":
        k = c_x - a_x
        #_nyomvalyu = b_x - a_x
        feature_b.setAttribute('_nyomvalyu', k)

class FeatureProcessor(object):
    def __init__(self):
        self.features = []
    def input(self,feature):
        self.features.append(feature)
    def close(self):
        end_index = len(self.features)
        num_args = 3 
        for i in range(end_index - num_args):
            mid(*self.features[i:i+num_args])
        for feature in self.features:
            self.pyoutput(feature)

 

 

A better less memory intensive way would be to:

 

import fme
import fmeobjects

def mid(feature_a, feature_b, feature_c):
       a_x  = float(feature_a.getAttribute('z_uj'))
    a_sz = feature_a.getAttribute('_szelsoertek')
    b_x  = float(feature_b.getAttribute('z_uj'))
    b_sz = feature_b.getAttribute('_szelsoertek')
    c_x  = float(feature_c.getAttribute('z_uj'))
       if b_sz == "LOKMIN":
        k = c_x - a_x
        #_nyomvalyu = b_x - a_x
        feature_b.setAttribute('_nyomvalyu', k)

      
class FeatureProcessor(object):

    def __init__(self):
        self.container = []
        self.max_items = 3

   def input(self,feature):
        if len(self.container) < self.max_items:
            self.container.append(feature)
        else:
            mid(*self.container)
            temp = self.container.pop(0)
            self.container.append(feature)
            feature = temp
            self.pyoutput(feature)
   
    def close(self):
        for feature in self.container:
            self.pyoutput(feature)

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings