I am trying to make these two methods variable, so the user could choose which method he wants to use. If the one with 3 classes or with 5.

    arr = arcpy.da.FeatureClassToNumPyArray(in_features, field)

    arcpy.AddField_management(in_features, "Class", "SHORT", 5, "", "","", "NULLABLE")

    methods = Method

    arrp = arcpy.da.FeatureClassToNumPyArray(in_features, field)
    arr = np.array(arrp,np.float)

    ##3 ranks
    p1 = np.percentile(arr, 33)  # rank = 1
    p2 = np.percentile(arr, 67)  # rank = 2
    p3 = np.percentile(arr, 100)  # rank = 3

    #use cursor to update the new rank field
    if Method[0] == "3":
        with arcpy.da.UpdateCursor(in_features , [field,'Class']) as cursor:
            for row in cursor:
                if row[0] < p1:
                    row[1] = 1  #rank 0
                elif p1 <= row[0] and row[0] < p2:
                     row[1] = 2
                     row[1] = 3
    print Method

    #arrp = arcpy.da.FeatureClassToNumPyArray(in_features, field)
    #arr = np.array(arrp,np.float)

    ##to create 5 ranks
    p1 = np.percentile(arr, 20)  # rank = 1
    p2 = np.percentile(arr, 40)  # rank = 2
    p3 = np.percentile(arr, 60)  # rank = 3
    p4 = np.percentile(arr, 80)  # rank = 4
    p5 = np.percentile(arr, 100) # rank = 5

    #use cursor to update the new rank field
    if Method[0] == "5":
        with arcpy.da.UpdateCursor(in_features , [field,'Class']) as cursor:
            for row in cursor:
                if row[0] < p1:
                    row[1] = 1  #rank 0
                elif p1 <= row[0] and row[0] < p2:
                     row[1] = 2
                elif p2 <= row[0] and row[0] < p3:
                     row[1] = 3
                elif p3 <= row[0] and row[0] < p4:
                     row[1] = 4
                     row[1] = 5
    print Method

and adjust this:

    methods = [3,5]

    Method = arcpy.Parameter(
        displayName="Choose Only One Number Of Classes",
    Method.filter.type = "Valuelist"
    Method.filter.list = methods

The script runs but creates just classes according to Method 5.

1 Answer 1


To create a Python Toolbox with the interface you require you need to set multiValue to True and then set the filter for the parameter, below is dome example code:

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool]

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = " My Tool"
        self.description = "Test tool"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""

        # Your code to create a list of rank values goes here
        example = [1,2,3]

        RankParam = arcpy.Parameter(displayName="Choose a Rank", name="method", datatype="Long", parameterType="Required", direction="Input", multiValue=True, enabled=True)
        RankParam.filter.type = "ValueList"
        RankParam.filter.list = example
        params = [RankParam]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""

    def execute(self, parameters, messages):
        """The source code of the tool."""

This creates the following interface: Example Python Toolbox interface

Here you will be allowed to select one or more values. If you only want one selected then you need to put some error trapping in updateParameters.

  • great, it works well :) However, how can I connect this chechbox with my method "arr" and "arrp" that I mentioned on the top? Sorry, Im a newbie in Python.
    – monami
    Mar 31, 2016 at 15:24
  • Your code is updating fields, you will not want that to run every time you run this tool, that should be a one time operation. I would recommend you pre-compute that rank field. Also your rank is only ever 1 2 or 3 so why not just set the filter to be that as that never changes?
    – Hornbydd
    Mar 31, 2016 at 15:46
  • its supposed to be just "one time action". So when i run this tool I get one output with those three classes, which were dissolved and selected by location. So I wanted to make two options, to select whether the user want to see 3 classes or 5 which signify the continuity of the values.
    – monami
    Mar 31, 2016 at 15:58
  • OK then I would abandon this approach and have a simple drop down with 3 or 5. Your update code then goes into the execute() function. I would recommend looking at the Help file and look at the example code in the Python Toolbox section to understand how you get a parameters value, in this case the layer you want to apply the update and the number of classes.
    – Hornbydd
    Mar 31, 2016 at 16:18
  • yes it is in execute() function, I have edited the post, so you can see the updated version. Even though it runs, it creats just one class with 0 values. I dont know what can be wrong, I have tried every possible option. :(
    – monami
    Mar 31, 2016 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.