Skip to main content
Solved

rename feature attribute with python

  • December 14, 2019
  • 10 replies
  • 215 views

charry
Supporter
Forum|alt.badge.img+5

how rename feature attribute with python

 

Best answer by takashi

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

10 replies

takashi
Celebrity
  • Best Answer
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation


charry
Supporter
Forum|alt.badge.img+5
  • Author
  • Supporter
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

0684Q00000ArM6tQAF.png

ERROR:

Python Exception <IndentationError>: expected an indented block (<string>, line 10)

Error executing string `import<space>fme<lf>import<space>fmeobjects<lf>#<space>Template<space>Function<space>interface:<lf>#<space>When<space>using<space>this<space>function<comma><space>make<space>sure<space>its<space>name<space>is<space>set<space>as<space>the<space>value<space>of<lf>#<space>the<space><apos>Class<space>or<space>Function<space>to<space>Process<space>Features<apos><space>transformer<space>parameter<space><lf>def<space>processFeature<openparen>feature<closeparen>:<lf><space><space><space><space>a=feature.getAttributeNullMissingAndType<openparen><apos>BZ<apos><closeparen><lf><space><space><space><space>if<space>a<openbracket>1<closebracket>==true:<lf><space><space><space><space>feature.performFunction<openparen><apos><at>RenameAttributes<openparen><apos>KD<apos><comma><apos>BZ<apos><closeparen><apos><closeparen><lf>#<space><space><space><space><space><space><space><space>v<space>=<space>feature.getAttribute<openparen><apos>BZ<apos><closeparen><lf>#<space><space><space><space><space><space><space><space>feature.setAttribute<openparen><apos><u5907><u6ce8><apos><comma><space>v<closeparen><lf>#<space><space><space><space><space><space><space><space>feature.removeAttribute<openparen><apos>BZ<apos><closeparen><lf><space><space><space><space><space><space><space>#<space>feature.setAttribute<openparen><apos><u5907><u6ce8><apos><comma>feature.getAttribute<openparen>feature.getAttribute<openparen><apos>BZ<apos><closeparen><closeparen><closeparen><space><lf><space><space><space><lf><lf>'

Factory proxy not initialized

PythonCaller (PythonFactory): PythonFactory failed to process feature

A fatal error has occurred. Check the logfile above for details

 


charry
Supporter
Forum|alt.badge.img+5
  • Author
  • Supporter
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

why I Can't execute successfully


takashi
Celebrity
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

firstly check if you have set the Class or Function to Process Features parameter appropriately.


charry
Supporter
Forum|alt.badge.img+5
  • Author
  • Supporter
  • December 14, 2019

firstly check if you have set the Class or Function to Process Features parameter appropriately.

First time use PythonCaller, can you provide some detailed operations?thanks


takashi
Celebrity
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

Then, correct indentations.


charry
Supporter
Forum|alt.badge.img+5
  • Author
  • Supporter
  • December 14, 2019

Then, correct indentations.

https://docs.safe.com/fme/html/FME_FactFunc/index.html function usage depend performFunction ?


takashi
Celebrity
  • December 14, 2019

Then, correct indentations.

No, the @RenameAttributes function is an FME function (not Python), and the document you link describes FME functions.


takashi
Celebrity
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

There are excellent tutorials on Python and FME basics, I'd recommend you to start learning from here.

https://knowledge.safe.com/articles/706/python-and-fme-basics.html 

https://knowledge.safe.com/articles/60319/pythoncaller-transformer.html 


takashi
Celebrity
  • December 14, 2019

Hi @charry, there are at least two ways.

1. Create new attribute, remove old attribute.

def renameExample(feature):
    v = feature.getAttribute('oldName')
    feature.setAttribute('newName', v)
    feature.removeAttribute('oldName')

0684Q00000ArJtAQAV.png

 

2. Rename attribute using the FME function @RenameAttributes

def renameExample2(feature):
    feature.performFunction('@RenameAttributes(newName,oldName)')

0684Q00000ArJSsQAN.png

See here to learn more about FME Functions: FME Factory and Function Documentation

Alternatively you can use the AttributeManager transformer or the AttributeRenamer transformer, if you just need to rename attributes.

Of course it's good to learn Python, but it would take time.