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
2 Answers 2
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:
A couple more comments are:
- 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...)
you define
parameters
as the 2nd argument of theexecute
method but attempt to useparams
in the body of the method. You need to either:- change the 2nd argument of the
execute
method fromparameters
toparams
OR - change
params
toparameters
in the body of the method
- change the 2nd argument of the
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
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.
-
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– tm12652015年11月30日 22:23:52 +00:00Commented Nov 30, 2015 at 22:23
-
1I think you want to pull out the element of
params
in yourexecute
method. e.g.,arcpy.AddField_management(params[0],"area","DOU...
(@user63149)Paul H– Paul H2015年11月30日 22:26:55 +00:00Commented Nov 30, 2015 at 22:26