I am trying to build a Python Toolbox, like in the attached example. I would like to implement an evaluation of mean like in Focal Statistics and subtraction like in Raster Calculator. In the documentation page is an example how to implement Focal Statistics, but this example is for Script Tool Wizard, not for Python Toolbox. Is there some example how to implement it and combine with raster calculator in Python Toolbox?
Below is attached my code. For now I implemented Focal Statistics, but it does not work.
Thanks in advance.
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 = "My" self.alias = "model" # List of tool classes associated with this toolbox self.tools = [MyModel] class MyModel(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "MyModel" self.description = "" self.canRunInBackground = True def getParameterInfo(self): # Workspace parameter param0 = arcpy.Parameter( displayName="Input Workspace", name="in_workspace", datatype="DEWorkspace", parameterType="Required", direction="Input") # Set the filter to accept only local (personal or file) geodatabases param0.filter.list = ["Local Database"] # First parameter param1 = arcpy.Parameter( displayName="Input Raster Dataset", name="in_rasterdataset", datatype=["DERasterDataset","DERasterCatalog"], parameterType="Required", direction="Input") in_features.filter.list = ["Raster"] # Second parameter param2 = arcpy.Parameter( displayName="Units", name="units", datatype="Double", parameterType="Optional", direction="Input", enabled=False) # Third parameter param3 = arcpy.Parameter( displayName="Output Raster Dataset", name="out_rasterdataset", datatype=["DERasterDataset","DERasterCatalog"], parameterType="Derived", direction="Output") out_features.filter.list = ["Raster"] param2.parameterDependencies = [param1.name] param2.schema.clone = True params = [param0, param1, param2, param3] 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.""" return def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def execute(self, parameters, messages): # Import system modules import arcgisscripting # Create the Geoprocessor object gp = arcgisscripting.create() try: # Set local variables InRaster = "in_rasterdataset" OutRaster = "out_rasterdataset" InNeighborhood = "NbrRectangle,3,3,Map" InNoDataOption = "DATA" # Check out the ArcGIS Spatial Analyst extension license gp.CheckOutExtension("Spatial") # Execute FocalStatistics gp.FocalStatistics_sa(InRaster, InNeighborhood,"", InNoDataOption) except: # If an error occured while running a tool, then print the messages. print gp.GetMessages() return
Solved! Go to Solution.
Right click your tool that has red X and click the only option from the context menu "Why..." This will give you the details of the error.
So what is the question? Are you getting errors? Sifting through the code isn't easy since it isn't formatted, use the syntax highlighting option in the advanced editor.
I changed the code in my previous post. I have a syntax error which I cannot find, I have an info that the error is in the 20th line of the code, but I do not see anything wrong in this line. Also I do not know how to implement second step (subtraction) without saving on disc the result from the first step.
Looks like indentation errors on lines 22 and 82 what is the error message you are getting
I have syntax error, because my Python Toolbox is crosses by red X. I corrected the indentation of lines - this was a copy-paste error, in my original code it was fine.
I'm not sure what the red X means did you move the script from its location and the tool box can no longer find it?
Right click your tool that has red X and click the only option from the context menu "Why..." This will give you the details of the error.
Thank you. I did not know about this option. I have found two mistakes which I corrected. But I still have two more errors, which I do not know how to remove. The errors are:
Traceback (most recent call last):
File "<string>", line 41, in getParameterInfo
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\arcobjects\_base.py", line 94, in _set
(attr_name, self.__class__.__name__))
NameError: The attribute 'list' is not supported on this instance of Filter.
In line 94 I tried to change:
InNeighborhood = "NbrRectangle,3,3,Map"
for
InNeighborhood = NbrRectangle(3, 3, "CELL"),
but it still does not work, so I do not know what is wrong. Any suggestions?
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 = "My" self.alias = "model" # List of tool classes associated with this toolbox self.tools = [MyModel] class MyModel(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "MyModel" self.description = "" self.canRunInBackground = True def getParameterInfo(self): # Workspace parameter param0 = arcpy.Parameter( displayName="Input Workspace", name="in_workspace", datatype="DEWorkspace", parameterType="Required", direction="Input") # Set the filter to accept only local (personal or file) geodatabases param0.filter.list = ["Local Database"] # First parameter param1 = arcpy.Parameter( displayName="Input Raster Dataset", name="in_rasterdataset", datatype=["DERasterDataset","DERasterCatalog"], parameterType="Required", direction="Input") param1.filter.list = ["Raster"] # Second parameter param2 = arcpy.Parameter( displayName="Units", name="units", datatype="Double", parameterType="Optional", direction="Input", enabled=False) # Third parameter param3 = arcpy.Parameter( displayName="Output Raster Dataset", name="out_rasterdataset", datatype=["DERasterDataset","DERasterCatalog"], parameterType="Derived", direction="Output") param3.filter.list = ["Raster"] param3.parameterDependencies = [param1.name] param3.schema.clone = True params = [param0, param1, param2, param3] 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.""" return def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def execute(self, parameters, messages): # Import system modules import arcgisscripting # Create the Geoprocessor object gp = arcgisscripting.create() try: # Set local variables InRaster = "in_rasterdataset" OutRaster = "out_rasterdataset" InNeighborhood = "NbrRectangle,3,3,Map" InNoDataOption = "DATA" # Check out the ArcGIS Spatial Analyst extension license gp.CheckOutExtension("Spatial") # Execute FocalStatistics gp.FocalStatistics_sa(InRaster, InNeighborhood,"", InNoDataOption) except: # If an error occured while running a tool, then print the messages. print gp.GetMessages() return
Karolina Korzeniowska:
Thank you. I did not know about this option. I have found two mistakes which I corrected. But I still have two more errors, which I do not know how to remove. The errors are:
Traceback (most recent call last):
File "<string>", line 41, in getParameterInfo
File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\arcobjects\_base.py", line 94, in _set
(attr_name, self.__class__.__name__))
NameError: The attribute 'list' is not supported on this instance of Filter.
You haven't specified the type of filter: Defining parameters in a Python toolbox—Help | ArcGIS for Desktop
Thank you for the answer. I added a line with data type. Now I do not have any syntax error, but the script still does not work, so I do not know what can still be wrong.
# First parameter param1 = arcpy.Parameter( displayName="Input Raster Dataset", name="in_rasterdataset", datatype=["DERasterDataset","DERasterCatalog"], parameterType="Required", direction="Input") param1.filter.type = "ValueList" param1.filter.list = ['Raster']