2

I am trying to calculate a new field in ArcGIS 10.5 Desktop using the field calculator, based on the value in any or all of three other fields in the same table. I have not used Python for a while, and am forgetting how to do this.

In essence, this should calculate if any of three types of a crop are being grown in an area. If at least one has an area greater than 1 hectare, then it should return "1" (if two or three types are present, it should give the same answer). The input fields and criteria:

  • Dry season rice > 1 hectare
  • Wet season rice > 1 hectare
  • Upland rice > 1 hectare
  • If any of these are true, then return "1" to new field I have already created, otherwise, new field is 0.

I have tried a few options in Python, but none have worked so far, e.g.:

def calc(Dry_area_1,Wet_area_1,Up_rice_1):
    if Dry_Rice > 1:
        Grow_Rice = 1
    elif Wet_Rice > 1:
        Grow_Rice = 1
    elif Up_Rice > 1:
        Grow_Rice = 1
    else
        return 0

Am I overlooking something very simple at a late hour of the night here?

  • can you share the entire class and the part of your script you call the method calc ? Do you get some error in your log ? – Hugo Roussaffa - GeoDatup Sep 25 '17 at 7:13
  • When debugging if blocks, it's important to inspect for differences. Only one block uses return, which should indicate that you have a missing return Grow_Rice – Vince Sep 25 '17 at 11:04
  • @HugoRoussaffa, I should have included before, the code to call this was calc(!Grow_rice!), which I see from the answer below was mixing up the approach. The only error give was "000989 : Python syntax error: invalid syntax line 8", but I did not initially see a simple formatting/syntax error only on this line. – Dan Sep 27 '17 at 5:21
  • @Vince, thank you, I should have caught that, thank you for adding that note. – Dan Sep 27 '17 at 5:22
5

You are confusing the way you define the function.

Your function needs to reference the variables(fields) called by the function.

Your function calls Dry_area_1, Wet_area_1, Up_rice_1, yet you don't reference any of those in the function.

You also need to return a value, not assign it to a variable. You're trying to assign Grow_Rice, but you need to return a value instead.

I think this is what you're going for:

def calc(Dry_area_1, Wet_area_1, Up_rice_1):
    if Dry_area_1 > 1:
        return 1
    elif Wet_area_1 > 1:
        return 1
    elif Up_rice_1 > 1:
        return 1
    else:
        return 0

then in the expression box:

calc(!Dry_area_1!, !Wet_area_1!, !Up_rice_1!)
  • thank you for catching my simple mix up. Your code worked, with two modifications: (1) One of my variables had an extra underscore in the name so it was not referenced at first (but did not get flagged as an error), (2) a colon ":" was needed after the "else" in line 8. With those fixes, it worked. Thank you! – Dan Sep 27 '17 at 5:34
  • Great! Sorry I wrote this in a hurry and missed that last colon. I've edited my answer to include it. It's good to note that if your syntax is correct, you wont get any errors. However, that doesn't mean the calculation produced the correct result. Never assume a calc script has worked without checking the records. – jbalk Sep 27 '17 at 6:02

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.