Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

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.

share|improve this answer
    
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 1 hour ago
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 1 hour ago

Your toolbox will 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 that you are passing the parameters list to your arcpy.SomeTool calls in your execute method, instead of a single parameter, and you need to either:

  1. change the execute method 2nd argument from parameters to params OR
  2. change params to parameters in the body of the code for the execute 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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