3

Following lines of Python code joins some fields of attributes in single attribute field. It works just fine in ArcGIS but it would be even better if that would work also within FME PythonCaller.

In ArcGIS looks like this:

def merge_fields(*fields):
 return ','.join([f.strip() for f in fields if f.strip()])

in the Field that I want it to be merged, I write:

merge_fields(!FIELD_1!, !FIELD_2!, !FIELD_3!, !FIELD_4!)

The question is how these 3 lines of code could be integrated within PythonCaller. when I open PythonCaller looks like this:

enter image description here

example of my data: First 4 columns is what i have and column 5 is what i need.

enter image description here

asked Aug 9, 2020 at 9:27

2 Answers 2

3

More of an FME expert than Python... but that dialog is showing two different templates: one is for a function and the other for a class. You'd keep one and remove the other, depending on what you want your code to do.

Generally, a class is used when your script needs to process features in a group. Here it looks to me like you are processing each feature separately, so I would use the function template.

In short, delete lines 8 onwards and add your code between lines 6 and 7.

If you're using an ArcGIS specific function in your code, then you'll probably need to also add a line like Import ArcObjects after the two FME Import lines. You'll also need to make sure the version of Python used in FME is compatible with your Python code.

Your script doesn't look too complex, so it may actually be easier to use an FME transformer instead of the Python code. To concatenate attributes we'd usually use the StringConcatenator, but if you want to deal with empty fields, build them into a list with a ListBuilder or Populator and then use a ListConcatenator (which has an option to ignore empty fields).

EDIT: To get what you want using list transformers, try the following:

ListPopulator

enter image description here

ListConcatenator

enter image description here

The result will be this:

enter image description here

In my test workspace, all the attributes begin with "col" and are numbered sequentially. Also, missing values are "missing" (i.e. don't exist) and so don't get added to the list. If they existed as nulls or empty fields, then use the Drop Empty and Null Elements part of the ListConcatenator to exclude them.

You can find my workspace (FME 2020) here: https://www.dropbox.com/s/nek0bl468jk2mjs/AttributeConcatenation.fmw?dl=1

menes
1,4272 gold badges8 silver badges25 bronze badges
answered Aug 10, 2020 at 16:23
2
  • it works :-) thanks a lot for your countribution :-) I dont understand why there is a python caller transformer in FME if everything can be done even without python Commented Aug 11, 2020 at 20:40
  • 1
    Not everything can be done in FME. I often use python callers to run arcpy scripts inside my workflow. Also, if you're more familiar with python, sometimes it's easier to use it. Commented Aug 12, 2020 at 3:36
2

You can use the following to merge fields within the PythonCaller

If you want to dynamically fetch attribute names:

class FeatureProcessor:
 def __init__(self):
 pass
 def input(self, feature):
 all_col_attributes = filter(
 lambda name: name.startswith('col'), 
 feature.getAllAttributeNames())
 value = map(str, map(feature.getAttribute, all_col_attributes))
 feature.setAttribute('_concatenated', ','.join(value))
 self.pyoutput(feature)
 def close(self):
 pass

statically fetch values:

class FeatureProcessor:
 def __init__(self):
 pass
 def input(self, feature):
 col1 = feature.getAttribute('col1')
 col2 = feature.getAttribute('col2')
 col3 = feature.getAttribute('col2')
 value = ','.join(map(str, [col1, col2, col3]))
 feature.setAttribute('_concatenated', value)
 self.pyoutput(feature)
 def close(self):
 pass
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Oct 1, 2021 at 13:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.