2013-07-13

BulkAttributeRenamer + AttributeAccumulator creates single record from multiple records which are storing attributes of a feature dispersedly

Sometimes, we have a table that stores attributes of a feature in multiple rows dispersedly like this:
id, attrName, attrValue
001, aaa, 1
001, bbb, 2
002, aaa, 3
002, bbb, 4

How to transform this table to the following structure?
id, aaa, bbb
001, 1, 2
002, 3, 4

One possible way is to accumulate attributes grouped by id, and then to restructure the table using a Python script:
-----
import fmeobjects
def restructureTable(feature):
    names = feature.getAttribute('_list{}.attrName')
    values = feature.getAttribute('_list{}.attrValue')
    for name, value in zip(names, values):
        feature.setAttribute(name, value)
-----

It works well but is a little inefficient, because it needs a list attribute which will not be used afterward. But I didn't know better ways.
Now,  I've learned a solution using the BulkAttributeRenamer with the following parameter settings:
Selected Attributes: attrValue
Action: Regular Expression Replace
Text to Find: .*
String: attrName

After renaming, I can create a table having the preferable schema by the AttributeAccumulator without creating list attributes.

Community > RecordBuilder / CSV import
Community > Attribute renaming using attribute values

No comments:

Post a Comment