Select to view content in your preferred language

Problems with parameters creating a tool with python.

1001
4
11-01-2013 12:13 PM
CarlosGutierrez
New Contributor
First of all thank you for anybody willing to help me.
I am trying to learn how to create a tool that will calculate the multiplication of 4 rasters. The code in python works and gives me an output raster but the tool I am trying to create does not. I am not sure if I am calling the rasters right with GetParameters. Please help I appreciate any comments on the code. Thank you
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 = "Toolbox"
        self.alias = ""

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


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"

    def getParameterInfo(self):
        """Define parameter definitions"""

# First parameter
        param0 = arcpy.Parameter(
        displayName="1",
        name="f1raster",
        datatype="DERasterDataset",
        parameterType="Required",
        direction="Input")

# Second parameter
        param1 = arcpy.Parameter(
        displayName="2",
        name="f2raster",
        datatype="DERasterDataset",
        parameterType="Required",
        direction="Input")

# Third parameter
        param2 = arcpy.Parameter(
        displayName="3",
        name="f3raster",
        datatype="DERasterDataset",
        parameterType="Required",
        direction="Input")

# Fouth parameter
        param3 = arcpy.Parameter(
        displayName="4",
        name="f4raster",
        datatype="DERasterDataset",
        parameterType="Required",
        direction="Input")

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):
        """The source code of the tool."""
f1raster = arcpy.GetParameter(0)
f2raster = arcpy.GetParameter(1)
f3raster = arcpy.GetParameter(2)
f4raster = arcpy.GetParameter(3)
outraster = (f1raster * f2raster * f3raster * f4raster)
outraster.save("C:/REP/REP.tif")
        return
Tags (2)
0 Kudos
4 Replies
DouglasSands
Occasional Contributor II
The first problem I see is how you get parameters:


    def execute(self, parameters, messages):
        """The source code of the tool."""
 f1raster = arcpy.GetParameter(0)
 f2raster = arcpy.GetParameter(1)
 f3raster = arcpy.GetParameter(2)
 f4raster = arcpy.GetParameter(3)



This should be done like the below when you use a python toolbox:

    def execute(self, parameters, messages):
        """The source code of the tool."""
 f1raster = parameters[0].valueAsText
 f2raster = parameters[1].valueAsText
 f3raster = parameters[2].valueAsText
 f4raster = parameters[3].valueAsText


Also, use the code tags, they're pretty cool :)!

- Doug
0 Kudos
CarlosGutierrez
New Contributor
Thank you for your reply. I have changed the code to get value as text and it is not working. What I don't get is that the multiplication of the four rasters works well on a regular python script. I am now getting the error
Type error:can't multiply sequence by non-int of type 'unicode'
and
NameError:global name 'gp' is not defined.

Thanks again for any comments
0 Kudos
DouglasSands
Occasional Contributor II
Ok. Now I'm starting to get a better grasp of your issue here. I don't have too much experience with the raster math, but try this:

    def execute(self, parameters, messages):
        arcpy.CheckOutExtension('Spatial')
        
        """The source code of the tool."""
 f1raster = arcpy.sa.Raster(parameters[0].valueAsText)
 f2raster = arcpy.sa.Raster(parameters[1].valueAsText)
 f3raster = arcpy.sa.Raster(parameters[2].valueAsText)
 f4raster = arcpy.sa.Raster(parameters[3].valueAsText)

 outraster = f1raster * f2raster * f3raster * f4raster

 outraster.save("C:/REP/REP.tif")


Also see here for more info.
0 Kudos
CarlosGutierrez
New Contributor
It works!!! Thanks Doug I appreciate your help with a newbee. I had tried getting the answer elsewhere but had no luck. You are awesome!
0 Kudos