TransWikia.com

How to integrate some Python code in "PythonCaller" FME?

Geographic Information Systems Asked by gisgis on September 17, 2020

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

One Answer

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" (ie 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

Correct answer by Mark Ireland on September 17, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP