Question

Dynamic attribute value renaming (adding a suffix)

  • 6 February 2014
  • 12 replies
  • 57 views

Badge

Hi there,

 

 

Simply renaming a value is quite easy using the AttributeValueMapper, however, I'm completely stumped as to how I should go about this when the source value are dynamic.

 

 

To get into details:

 

 

What I need is very basic. For example, If I have a value like "ROAD" I want to rename that value (and all other present values) to something like "ROAD_X". So I simply want to add the suffix, "_X" to the existing value.

 

 

This would be simple if my source was not dynamic, in the sense that it simply points to a folder, and whatever's in there gets run through the workspace.

 

 

So, is it possible to add "_X" or something similair to an unknown value with ValueMapper, or any other transformer?

 

 

Thanks in advance,

 


12 replies

Badge +21
BulkAttributeRenamer

 

 

Renames attributes by adding or removing prefixes or suffixes, or replacing text in regular expressions or character strings.

 

 

This transformer is useful if you need to quickly rename all your attributes. If the rename results in an empty attribute name, that attribute will be removed.
Badge
Hi SigTill,

 

 

Though I can see the usefullness of the BulkAttributeRenamer, It's the values of the attributes I need to rename, not the attributes themselves.

 

 

I'm also unsure as to the string value I would neet to use, as the source value is dynamic and unknown.

 

 

 

Userlevel 2
Badge +17
Hi,

 

 

If you just need to add suffix to attribute value unconditionally, I think the StringConcatenator can be used simply.

 

Or if you need to add suffix to specific value (e.g. "ROAD"), consider using "Conditional Value Setting" functionality in the AttributeCreator.

 

 

Takashi
Userlevel 4
Hi,

 

 

I think you will need to use a PythonCaller for this. Sample code:

 

--- 

 

import fmeobjects

 

 

# All attribute names starting with either of these strings will be left untouched

 

attributes_to_ignore = ("fme_", "geodb_")

 

 

# Suffix to add to attribute value

 

value_suffix = "_X"

 

 

def FeatureProcessor(feature):

 

    attributes = feature.getAllAttributeNames()

 

    for attribute in attributes:

 

        if not filter(attribute.startswith, attributes_to_ignore):

 

            value = feature.getAttribute(attribute)

 

            feature.setAttribute(attribute, str(value) + value_suffix)

 

--- 

 

 

You need to make sure to verify "attributes_to_ignore" so that you don't accidentally modify feature attributes necessary for the writer.

 

 

David
Badge +21
Ah, misread your question, sorry. Then I guess a Pythonscript might be the best option. 
Badge
Thanks guys, 

 

 

It seems the standard strin concatenator works. Had to play around a bit, but that's part of the fun.
Badge +3
I think Takashi is right.

 

 

U can simply use an attributecreator:

 

attribute name:  @Value(YourSourceVar) 

 

Value:@Value(YourSourceVar)_X
Badge +3
u are talking about the values and not the attributenames, right?
Badge
Yes indeed, the values.

 

 

Seems to be working now. What I use is simply @Value(FEATURE)_X, with feature being my dynamic values.

 

 

For interest's sake, can anyone say how do use that same string value to make a prefix, instead of a suffix? I'm relatively new to the whole concept.
Userlevel 4
Try

 

 

X_@Value(FEATURE)

 

 

David
Badge
Thanks David, works like a charm.

 

 

Can't believe it's that simple.
Userlevel 4
Welcome to FME, Robbie, it's a lot of fun to discover how easy certain things are :-)

 

 

I misunderstood your initial question a bit, obviously. But to clarify, the script I posted above does exactly the same, but it will add the suffix to any and all attribute values present on each feature, regardless. I'll leave it here for future reference, though.

 

 

David

Reply