Skip to main content
Solved

rename feature attribute with python


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

View original
Did this help you find an answer to your question?

10 replies

takashi
Influencer
  • 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
takashi wrote:

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
takashi wrote:

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
Influencer
  • December 14, 2019
takashi wrote:

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
takashi wrote:

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
Influencer
  • December 14, 2019
takashi wrote:

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
takashi wrote:

Then, correct indentations.

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


takashi
Influencer
  • December 14, 2019
takashi wrote:

Then, correct indentations.

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


takashi
Influencer
  • December 14, 2019
takashi wrote:

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
Influencer
  • December 14, 2019
takashi wrote:

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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings