2

I'm trying to create a column with integer values based on a small function that basically uses conditional statements. In short, a reclassification. My attempt below:

def getValue(Etat):
    if Etat == 'Bon':
        value = 1
    elif Etat == 'Moyen':
        value = 2
    elif Etat == 'Mauvais':
        value = 3
    elif Etat == 'Ruine':
        value = 4

value = getValue(<Etat>)

While I do not get any errors, the problem is that the new field is not being updated with new values. My thoughts are that it can't find the original field: "Etat", the source.

How should I go about it? Also, what I don't understand how is going to make the updates to the field, surely something is missing.

1

Alright, due to my lack of not paying enough attention I managed to solved the problem.

I wasn't returning the value, only making assignments.

  • Could you help me? I'm trying to use the same toolbox to write the layer name in a new attribute column. I insert in the Formula field the same expression used in field calculator: "value = @layer_name". Unfortunately, python does not recognize the variable @layer_name. – christian.gobel Mar 25 at 20:04
  • @christian.gobel Hi. I could try but I'm entirely sure I understood everything. If I'm not mistaken @ character shouldn't be used as part of variable name unless that is some sort of new feature for qgis/arcgis which I am not familiar with. Could you share a little bit more of what you are trying to do? – Geosphere Mar 28 at 18:21
  • sure. I posted my question [here] (gis.stackexchange.com/questions/316642/…). I Understand the @ are variables function from QGIS environment. That's why not work in python expression. – christian.gobel Mar 29 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.