3

I am trying to create a Python Toolbox tool which is supposed to add two fields State and StateAbb to selected Shapefile from selected combobox.

How I can add a Combobox which contains a list of US states to the Tool?

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 = "states"
        self.alias = "states"

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


class states(object):
    def __init__(self):
        self.label = "states Tool"
        self.description = "Add and Insert Value from Combo"
        self.canRunInBackground = False

    def getParameterInfo(self):

        param0 = arcpy.Parameter(
            displayName="1- State Street Layer",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Optional",
            direction="Input"
        )

        params = [param0]

        return params


    def execute(self, params, messages):
        """The source code of the tool."""
        arcpy.AddField_management(params[0],"area","TEXT","#","#","#","#","NULLABLE","NON_REQUIRED","#")
        arcpy.CalculateField_management(params[0],"area","!shape.area@squaremeters!","PYTHON_10.3","#")


        return
3

Use a "Value List"

param.filter.type = "ValueList"
param.filter.list = [a, list, of, states]

E.g.

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 = "states"
        self.alias = "states"

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



class States(object):

    def __init__(self):
        self.label = "states Tool"
        self.description = "Add and Insert Value from Combo"
        self.canRunInBackground = False
        self.states = {'Alabama': 'AL', 'Alaska': 'AK', 'Arizona': 'AZ', 'Arkansas': 'AR', 
                       'California': 'CA', 'Colorado': 'CO', 'Connecticut': 'CT', 
                       'Delaware': 'DE', 'Florida': 'FL', 'Georgia': 'GA', 'Hawaii': 'HI', 
                       'Idaho': 'ID', 'Illinois': 'IL', 'Indiana': 'IN', 'Iowa': 'IA', 
                       'Kansas': 'KS', 'Kentucky': 'KY', 'Louisiana': 'LA', 'Maine': 'ME', 
                       'Maryland': 'MD', 'Massachusetts': 'MA', 'Michigan': 'MI', 
                       'Minnesota': 'MN', 'Mississippi': 'MS', 'Missouri': 'MO', 
                       'Montana': 'MT', 'Nebraska': 'NE', 'Nevada': 'NV', 'New Hampshire': 'NH', 
                       'New Jersey': 'NJ', 'New Mexico': 'NM', 'New York': 'NY', 
                       'North Carolina': 'NC', 'North Dakota': 'ND', 'Ohio': 'OH', 'Oklahoma': 'OK', 
                       'Oregon': 'OR', 'Pennsylvania': 'PA', 'Rhode Island': 'RI', 
                       'South Carolina': 'SC', 'South Dakota': 'SD', 'Tennessee': 'TN', 
                       'Texas': 'TX', 'Utah': 'UT', 'Vermont': 'VT', 'Virginia': 'VA', 
                       'Washington': 'WA', 'West Virginia': 'WV', 'Wisconsin': 'WI', 
                       'Wyoming': 'WY', 'American Samoa': 'AS', 'District of Columbia': 'DC', 
                       'Guam': 'GU', 'Northern Mariana Islands': 'MP', 'Puerto Rico': 'PR', 
                       'United States Virgin Islands': 'VI'}

    def getParameterInfo(self):

        param0 = arcpy.Parameter(
            displayName="1- State Street Layer",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Optional",
            direction="Input"
        )
        param1 = arcpy.Parameter(
            displayName="State",
            name="state",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

        # Set a value list of States (and other US areas)
        param1.filter.type = "ValueList"
        param1.filter.list = sorted(self.states.keys())


        params = [param0, param1]

        return params


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

        state = params[1].valueAsText
        abbr = self.states[state]

        #Do something with state and abbr

        return

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.