1

I'm creating an ArcGIS tool that will allow users to figure out how much nitrogen that a field needs based on interpolated points (Kriging). I will have the following parameters:

#numeric variables
yieldGoal = arcpy.GetParameterAsText(0)
organicMatter = arcpy.GetParameterAsText(1)

#text variable
previousCrop = arcpy.GetParameterAsText(2)

To figure out how much nitrogen is already in the soil, I have to do some Kriging:

n_krig = Kriging(soilPoints, field, kModelOrdinary, cellSize, kRadius, outRaster)

The next step is to calculate the amount of nitrogen that the soil needs. The equation takes into account previous crops, because some can add nitrogen to the soil, and others do not.

I want users to be able to choose the previous crop that was grown with the previousCrop = arcpy.GetParameterAsText(2) parameter, and based on that selection, I want to create another numeric variable pca that will serve as a nitrogen amount for that previous crop selection. The 'pca' variable can then be plugged into the final equation:

fertilizer = ((1.6 * yieldGoal) - (organicMatter * 20) - (n_krig) - (pca))

How can I define pca?

I tried to write an ifstatement to define the variable, but it doesn't work:

#Original parameter:
previousCrop = arcpy.GetParameterAsText(0)

#If statement
if previousCrop == str("Corn"):
    pca == int(0)
elif previousCrop == str("Wheat"):
    pca == int(0)
elif previousCrop == str("Milo"):
    pca == int(0)
elif previousCrop == str("Sunflowers"):
    pca == int(0)
elif previousCrop == str("Soybeans"):
    pca == int(40)
elif previousCrop == str("Alfalfa: >5 plants/square foot"):
    pca == int(120)
elif previousCrop == str("Alfalfa: 2-5 plants/square foot"):
    pca == int(80)
elif previousCrop == str("Alfalf: 2-3 plants/square foot"):
    pca == int(40)
elif previousCrop == str("Alfalfa: <2 plants/square foot"):
    pca == int(0)
else:
    pca == int(0)
1

Here are a few things I want to point out:

  • To assign variables you should use the = operator instead of the == operator (which is a comparison operator).
  • There is no need to specify int nor str.
  • You can refactor your code as you are assigning 0 and 40 multiple times.

Your code could look something like:

if previousCrop == "Alfalfa: >5 plants/square foot":
    pca = 120
elif previousCrop == "Alfalfa: 2-5 plants/square foot":
    pca = 80
elif previousCrop in ("Alfalf: 2-3 plants/square foot", "Soybeans"):
    pca = 40
else:
    pca = 0
| improve this answer | |

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.