[GIS] Python If else statements

arcgis-10.2arcgis-desktopfield-calculatorpython

I'm trying to populate a field from another. The catch is that it isn't 1 or 2 values. If my field A = 4 then I want it to return field B to 0. But if Field A is 1, 2 or 3 then I want the user to be able to use the domain drop down list to choose what field B is–because it could be one of 3 other values. Is that possible? Below is the code I think is a start. But I need it to return 1, 2 or 3 if WaterType = anything but 4.

def Reclass (IrrigType):
  if WaterType = 4:
     return 0
  else:
     return 1

Best Answer

First of all, be sure to use == for comparison expressions instead of =, which is for assignment.

If I've understood correctly, you should be able to use:

def Reclass(WaterType): 
    if WaterType == 4: return 0
    else: return WaterType

or (in shorter form)

def Reclass(WaterType): 
    return 0 if WaterType == 4 else WaterType

This will return 0 if WaterType = 4. If WaterType != 4 then it will return whatever the value was.

edit
Based on your comment, it sounds like your user is editing the features normally (using domains, etc) and you want to run a script at the end (ie. before posting or similar) to be sure that if WaterType = NI then IrrigType should also be NI otherwise leave what the user entered.

If that is the case, then you can use the below:

def Reclass(IrrigType,WaterType):
    if WaterType == 'NI': return 'NI'
    else return IrrigType

You'll run this to set the value of IrrigType, but you'll have to supply both the value of WaterType and the (current) IrrigType.

Related Question