Select to view content in your preferred language

Calculate field with Code Block doesn't work

1097
3
Jump to solution
11-14-2022 01:44 PM
JulietteLapeyre
Emerging Contributor

I am trying to calculate a field in a model builder, using if statements.

In the 'Calculate Field' window, I create a new attribute 'perturbateur' (string)

the formula to calulate this attribute refers to a code Block below, which shows no error. But I only get null values.

 

perturbat=

 

computeType(!FID_Buffer_coupes_Dissolve!, !FID_Reboisement_Dissolve!)

 

 

 

Code Block

 

def computeType(fid_c, fid_r):
    if fid_c== 1 and fid_r==1:
        perturbat = 'coupe et reboisement'
    elif fid_c== 1:
        perturbat = 'coupe'
    elif fid_r==1:
        perturbat = 'reboisement'

 

 

Could you help me understanding what is wrong ?

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

you put in two elif checks but you don't cover the case where 0's appear. 

More importantly, your def doesn't have any 'return' statements.

Your Arcade expression basically does an else if both are equal to 1, or the first doesn't equal 1. so to backtrack on your python code it would be

def computeType(fid_c, fid_r):
    if (fid_c == 1) and (fid_r==1):
        return 'coupe et reboisement'
    elif fid_c == 1:
        return 'coupe'
    return 'reboisement'

But I would check if you want fidc to return 'coupe' if it equals 0?  So play around with the possible combinations and what you actually want returned for all the cases of the two fieds ,,,, ie,

1  1

1  0

0  1

0  0


... sort of retired...

View solution in original post

3 Replies
JulietteLapeyre
Emerging Contributor

It works with Arcade, but I am still interested in knowing how to do with Python3

if ($feature.FID_Buffer_coupes_Dissolve==1 && $feature.FID_Reboisement_Dissolve==1) {
    return 'coupe et reboisement'
}
else if ($feature.FID_Buffer_coupes_Dissolve==1) {
    return 'coupe'
}
else {
    return 'reboisement'
}
0 Kudos
DanPatterson
MVP Esteemed Contributor

you put in two elif checks but you don't cover the case where 0's appear. 

More importantly, your def doesn't have any 'return' statements.

Your Arcade expression basically does an else if both are equal to 1, or the first doesn't equal 1. so to backtrack on your python code it would be

def computeType(fid_c, fid_r):
    if (fid_c == 1) and (fid_r==1):
        return 'coupe et reboisement'
    elif fid_c == 1:
        return 'coupe'
    return 'reboisement'

But I would check if you want fidc to return 'coupe' if it equals 0?  So play around with the possible combinations and what you actually want returned for all the cases of the two fieds ,,,, ie,

1  1

1  0

0  1

0  0


... sort of retired...
JulietteLapeyre
Emerging Contributor

It works great, thanks DanPatterson!

0 Kudos