2013-09-01

Extract List Attribute Elements in Python Script

=====
2013-12-23: FME 2014 supports <null>; a list attribute can also contain <null> elements. But FMEFeature.getAttribute and setAttribute methods do not treat <null> elements as <null>.
We should be aware this point in FME 2014+.
See more information here > Null in FME 2014: Handling Null with Python / Tcl
=====

To extract list attribute elements and create non-list attributes storing the values:
Simple List: _list{i} --> attr_i
# Example 1
def extractListElements(feature):
    i = 0
    while True:
        v = feature.getAttribute('_list{%d}' % i)
        if v == None:
            break
        feature.setAttribute('attr_%d' % i, v)
        i += 1

# Example 2
def extractListElements(feature):
    list = feature.getAttribute('_list{}')
    if list != None:
        for i, v in enumerate(list):
            feature.setAttribute('attr_%d' % i, v)

Complex List: _list{i}.attr --> attr_i
# Example 3
def extractComplexListElements(feature):
    i = 0
    while True:
        v = feature.getAttribute('_list{%d}.attr' % i)
        if v == None:
            break
        feature.setAttribute('attr_%d' % i, v)
        i += 1

# Example 4
def extractComplexListElements(feature):
    list = feature.getAttribute('_list{}.attr')
    if list != None:
        for i, v in enumerate(list):
            feature.setAttribute('attr_%d' % i, v)

Nested List: _list{i}.sub{j} --> attr_ij
# Example 5
def extractNestedListElements(feature):
    i = 0
    while True:
        sub = feature.getAttribute('_list{%d}.sub{}' % i)
        if sub == None:
            break
        for j, v in enumerate(sub):
            feature.setAttribute('attr_%d%d' % (i, j), v)
        i += 1

Related articles:
Community > Set a list attribute in Python
Community > FME Terminologies about List Attribute

No comments:

Post a Comment