Python toolbox - compiling two processes

4444
12
Jump to solution
09-15-2015 01:36 AM
KarolinaKorzeniowska
Occasional Contributor

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
0 Kudos
12 Replies
WesMiller
Regular Contributor III

If your example works you can turn scripts into tools and use it in tool box or model builder or you can use tools as scripts just look at the documentation for the tool

DanPatterson_Retired
MVP Emeritus

lines 22-63 are also not properly indented. Assuming they form part of the mymodel class

0 Kudos
KarolinaKorzeniowska
Occasional Contributor

In the original code the lines 22-63 are fine. Now I see that something wrong happen during pasting the code to the forum - I already changed it above.

0 Kudos