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:
example of my data: First 4 columns is what i have and column 5 is what i need.
2 Answers 2
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
ListConcatenator
The result will be this:
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
-
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 pythongisgis– gisgis2020年08月11日 20:40:03 +00:00Commented Aug 11, 2020 at 20:40
-
1Not 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.Fezter– Fezter2020年08月12日 03:36:12 +00:00Commented Aug 12, 2020 at 3:36
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