1

I am attempting to create a script with arc toolbox in which part of the script populates a probability field based on thresholds on a percentage field. I would like the thresholds for the ranges upon which to classify the probability field to be parameters that the user can input in the tool. However, when I attempt to run the tool, it does not properly update the fields. Instead it fills with the last value (in the case of my script below, it populates everything with "L"). The script runs fine (through toolbox or stand-alone) if I replace the parameters in the script with values. I have tried defining these threshold parameters as "double" or "any value" in arc toolbox, both with no luck. Does anyone know why I am having this problem?

# populate occurrence probability based on percentage thresholds
with arcpy.da.UpdateCursor(zoneTable, ["PERCENTAGE", "probability"]) as cursor:
    for row in cursor:
        if (row[0] >= highThreshold):
            row[1] = "H"
        elif (row[0] < highThreshold and row[0] >= medThreshold):
            row[1] = "M"
        else:
            row[1] = "L"
        cursor.updateRow(row)

highThreshold = arcpy.GetParameterAsText(2)
medThreshold = arcpy.GetParameterAsText(3)

enter image description here

2

you use arcpy.GetParameterAsText() so your threeshold would be a string not float

use arcpy.GetParameter() with input parameter data type defined as double instead

# populate occurrence probability based on percentage thresholds
import arcpy

highThreshold = arcpy.GetParameter(2)
medThreshold = arcpy.GetParameter(3)

with arcpy.da.UpdateCursor(zoneTable, ["PERCENTAGE", "probability"]) as cursor:
    for row in cursor:
        if (row[0] >= highThreshold):
            row[1] = "H"
        elif (row[0] < highThreshold and row[0] >= medThreshold):
            row[1] = "M"
        else:
            row[1] = "L"
        cursor.updateRow(row)
    #delete the cursor when finished
    del cursor
  • Thank you Below the Radar! I knew I had missed something relatively simple. I just needed another set of eyes. One question about your code to understand a bit better (I'm relatively new to Python): do I still need to be as diligent about deleting the cursor if I include the with statement? Thanks again! – mmoore Jul 29 '16 at 15:19
  • you are welcome. If you work in a SDE enterprise stored in a database, deleting the cursor may prevent locks on the table – Below the Radar Jul 29 '16 at 15:21
  • Okay, great. That is good to know. – mmoore Jul 29 '16 at 15:22

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.