4

I am still an arcpy newbie, but I think I have a basic understanding of fundamentals including syntax, variables, modules, etc. My question is regarding the usage of parameters while creating a tool.

I am trying to create a tool that will calculate and summarize the area of a feature layer "levels". I have the script working for the calculation and summarization of the feature layer which outputs as a table, but I am not able to get the parameter inputs for the tool working. Syntax checks out (using PyScripter as IDE) but when I try to run the tool in ArcMap "This tool has no parameters" appears. Ideally, I would like the user to input the feature layer "levels" as a parameter and return the summary table, but a message window showing area summary would be even better if thats possible. Please let me know what might be going wrong with the parameters, or how I could change the output to a message window.

import arcpy


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

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


class LevelsCalcTool(object):
    def __init__(self):
        self.label = "LevelsCalcTool"
        self.description = "Calculates and summarizes Levels' area in square meters"
        self.canRunInBackground = False

    def getParameterInfo(self):

        param0 = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input"
        )

        params = [param0]

        return params


    def execute(self, parameters, messages):
        """The source code of the tool."""
        arcpy.AddField_management(params,"area","DOUBLE","#","#","#","#","NULLABLE","NON_REQUIRED","#")
        arcpy.CalculateField_management(params,"area","!shape.area@squaremeters!","PYTHON_9.3","#")
        arcpy.Statistics_analysis(params, "out_table.dbf", [["area", "SUM"]])

        return
6

Your toolbox will not work properly unless you change the class name to Toolbox as described in the help:

To ensure the Python toolbox is recognized correctly by ArcGIS, the toolbox class must remain named Toolbox.

Once I made that change, your tool opened correctly:

enter image description here

A couple more comments are:

  1. you are passing the parameters list to your arcpy.SomeTool calls in your execute method, instead of a single parameter, i.e.arcpy.AddField_management(params[0], etc...)
  2. you define parameters as the 2nd argument of the execute method but attempt to use params in the body of the method. You need to either:

    1. change the 2nd argument of the execute method from parameters to params OR
    2. change params to parameters in the body of the method

Fixed 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 = "GeoArea"
        self.alias = "Tom"

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


class LevelsCalcTool(object):
    def __init__(self):
        self.label = "LevelsCalcTool"
        self.description = "Calculates and summarizes Levels' area in square meters"
        self.canRunInBackground = False

    def getParameterInfo(self):

        param0 = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input"
        )

        params = [param0]

        return params


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

        return
5

As you've posted it here, it looks like there's a problem with the formatting on your getParameterInfo method. You need to make sure that your indentation is consistent throughout the file. I think it should look like this:

def getParameterInfo(self):
    param0 = arcpy.Parameter(
        displayName="Input Features",
        name="in_features",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")


    params = [param0]

    return params

As posted, it looks like the return params statement is not actually part of getParameterInfo, which would mean that that function is returning None. That would explain why you don't see any parameters in your tool.

2
  • Good catch and thanks for the reminder regarding indentation, but unfortunately I am still getting the same message when attempting to execute the tool after fixing indentation. "This tool has no parameters". The tool dialog box opens when attempting to execute the tool and indicates the script was successfully run. – tm1265 Nov 30 '15 at 22:23
  • 1
    I think you want to pull out the element of params in your execute method. e.g., arcpy.AddField_management(params[0],"area","DOU... (@user63149) – Paul H Nov 30 '15 at 22:26

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.