Skip to main content
Solved

How to manipulate attribute values of all features in PythonCaller

  • May 23, 2023
  • 2 replies
  • 152 views

mzuer
Contributor
Forum|alt.badge.img+5

I would like to manipulate whole ''column'' (list of attribute values) in a PythonCaller but I don't really understand how I can do that.

 

For example in the example here below, I create a new attribute in `input` (=iterating over each each feature, right ?) ; what I am trying to do 

 

1) in `close` : to print all unique values of the newly created attribute

(WITHOUT iterating over each feature, so to work directly on the list of all attribute values that my data contain)

 

2) in `close` (or somewhere else) : to add directly a new attribute to my data by applying a function on the firstly created attribute (again WITHOUT iterating over each feature)

 

Is it possible to do all these things in a Python caller ? 

 

import fme
import fmeobjects
def processFeature(feature):
    pass
 
class FeatureProcessor(object):
    def __init__(self):
        pass
    def input(self,feature):        
        progs_dict = globals()[fme.macroValues['VARNAME_DICT']]
        all_prog_cols = list(progs_dict.keys())
        all_col_attributes = filter(
            lambda x: x in all_prog_cols,
            feature.getAllAttributeNames())
        value_tmp = map(str, map(feature.getAttribute, all_col_attributes))
        value = [i for i in value_tmp if i == 'X']
        if len(value) == 0:
            value = [progs_dict['aucun']]
        feature.setAttribute('MYNEWATTR', ';'.join(value))
        self.pyoutput(feature)
        
    def close(self):
     #### EX 1) I WOULD LIKE TO PRINT ALL UNIQUE VALUES OF THE NEWLY CREATED ATTRIBUTE
      #print(', '.join(set(self.MYNEWATTR)))  # <<< this does not work
 
       #### EX 2) WOULD IT BE POSSIBLE TO CREATE HERE A NEW ATTRIBUTE BASED ON THE NEWLY CREATED ONE WIHTOUT ITERATING OVER ALL FEATURES
      #self.ANOTHERATTR = map(my_python_func,  self.MYNEWATTR) #<<< does not work
 
        pass

 

 

Best answer by mzuer

thanks for the explanation !

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.

2 replies

debbiatsafe
Safer
Forum|alt.badge.img+21

Hello @mzuer​ 

For example 1, self.MYNEWATTR does not exist and will error when called in the close method. You would want to define the self.MYNEWATTR set in the __init__ method and then add ';'.join(value) to this set to accumulate unique values to print in the close method.

import fme
import fmeobjects
 
def processFeature(feature):
    pass
 
class FeatureProcessor(object):
    def __init__(self):
        self.MYNEWATTR = {}
        
    def input(self,feature):        
        progs_dict = globals()[fme.macroValues['VARNAME_DICT']]
        all_prog_cols = list(progs_dict.keys())
        all_col_attributes = filter(
            lambda x: x in all_prog_cols,
            feature.getAllAttributeNames())
        value_tmp = map(str, map(feature.getAttribute, all_col_attributes))
        value = [i for i in value_tmp if i == 'X']
        if len(value) == 0:
            value = [progs_dict['aucun']]
        feature.setAttribute('MYNEWATTR', ';'.join(value))
        self.MYNEWATTR.add(';'.join(value))
        self.pyoutput(feature)
        
    def close(self):
        #### EX 1) I WOULD LIKE TO PRINT ALL UNIQUE VALUES OF THE NEWLY CREATED ATTRIBUTE
        print(self.MYNEWATTR) 
        #### EX 2) WOULD IT BE POSSIBLE TO CREATE HERE A NEW ATTRIBUTE BASED ON THE NEWLY CREATED ONE WIHTOUT ITERATING OVER ALL FEATURES
        #self.ANOTHERATTR = map(my_python_func,  self.MYNEWATTR) #<<< does not work 
        pass

For example 2, no, it is not possible to add/alter attribute values of features without performing a function directly on the features like through fmeobjects.FMEFeature.setAttribute().


mzuer
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • Best Answer
  • June 30, 2023

thanks for the explanation !