Select to view content in your preferred language

Reclassify GUI/form not active in Python script

3070
13
02-12-2012 11:04 PM
RPW
by
Occasional Contributor
Hi

I have trouble running a Python script, which I added to the ArcToolbox through "Add" -> "Script". The idea is that the user can define the reclassification rules, so the reclassification rules are passed as a parameter.

The problem is that when I open the script, the Reclassify form/GUI is empty and not active (see attached screenshot), i.e. it's not possible to add or change the classification (when clicking any of the buttons, such as Classify, Unique, Add Entry, and Load, nothing happens).

Here the code snippet:

# Get Parameters
infc      = arcpy.GetParameterAsText(0)
recl_rule = arcpy.GetParameterAsText(1)
outfc     = arcpy.GetParameterAsText(2)

# Local Parameters
recl_value = "VALUE"
recl_data  = "DATA"

# Reclassify
outRecl = arcpy.sa.Reclassify(infc, recl_value, recl_rule, outfc, recl_data)
outRecl.save("outfc")


The reclassify input ( GetParameterAsText(1) ) is defined as Data Type "Remap" in the script properties (tab "Parameters").

Version is ArcGIS 10.0 SP3.

Does anyone have an idea why this does not work?

Thanks a lot in advance,
Ralph
Tags (2)
0 Kudos
13 Replies
WarrenMedernach
Frequent Contributor
Carl,
What 'two parameter dependencies' do you mean?
I'm on 10.1 and have added the following to the ToolValidator > initializeParameters section:
self.params[1].parameterDependencies = [0]

I'm getting the exact same results as others: Once the raster is selected, the classify/remap area is not refreshed, and the buttons remain greyed out.

Warren M
0 Kudos
CarlBeyerhelm
Frequent Contributor
In this case, param[0] is the input raster, param[1] is the input raster's RECLASS field (the field has a dependency on the input raster), and param[2] is the REMAP control itself (has a dependency on both the input raster and on the reclass field).

Having said that, I'm still at 10.0, so have not had an opportunity to validate that this (or something like it) works in 10.1.

** For some reason, the indents aren't showing up in the ToolValidator code below...

def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
self.params[1].value = "VALUE"
self.params[1].enabled = False
self.params[1].parameterDependencies = [0]
self.params[2].parameterDependencies = [0, 1]
return
0 Kudos
Umersharif
New Contributor
I have the same problem while  i run the PythonTool (.pyt). I am facing two problems here. First the Remap values is not
populated automatically when add the raster even specifying the "Value" field which contains the values to be reclassified.
Secondly When i manually fill the values and press the reclassify button it won't work and ArcGIS Crashes.

The python script is:
 
  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"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
        displayName="InputRaster",
        name="in_features",
        datatype="Raster Layer",
        parameterType="Required",
        direction="Input")

# Second parameter
        param1 = arcpy.Parameter(
        displayName="RecquiredField",
        name="out_field",
        datatype="Field",
        parameterType="Required",
        direction="Input")
# third parameter
        param2 = arcpy.Parameter(
        displayName="Reclassify",
        name="Reclass",
        datatype="remap",
        parameterType="Required",
        direction="Input")
 
# fourth parameter
        param3 = arcpy.Parameter(
        displayName="OutputRaster",
        name="Output",
        datatype="Raster Layer",
        parameterType="Required",
        direction="Output")

        param1.parameterDependencies = [param0.name] 
        

        param2.parameterDependencies = [param0.name,param1.name]
 
        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."""
        inraster  = parameters[0].valueAsText
        outfield = parameters[1].valueAsText 
        remapclass = parameters[2].valueAsText
        outputraster = parameters[3].valueAsText
        arcpy.gp.Reclassify_sa(inraster, outfield, remapclass, outputraster, "DATA")
        return


Please help if some one know the solution.
0 Kudos
SelinaAgbayani1
New Contributor

Thanks so much to everyone who posted on this, you've all helped me a lot!

It's still a bit buggy (remap values don't populate automatically), but I managed to get something working (10.7.1).

In order to activate the "Classify" button, which is initially grayed out, click on one of the other buttons... "Reverse New Values" or "Precision..." and that should activate the "Classify..." button.  You can then choose the classification method you want and the no. of classes. 

import arcpy, string
import numpy
from arcpy import env
from arcpy.sa import *

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 = [Remap]

class Remap(object):
    def __init__(self):
        self.label = "Remap"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        inputraster = arcpy.Parameter(
            displayName="Input Raster ",
            name="input1",
            datatype="Raster Layer",
            parameterType="Required",
            direction="Input") 

        field = arcpy.Parameter(
            displayName="Input Field ",
            name="input2",
            datatype="Field",
            parameterType="Required",
            direction="Input")
        field.value = "Value"  
        field.enabled = True

        remap = arcpy.Parameter(
            displayName="ReMap ",
            name="remap",
            datatype="GPSARemap",
            parameterType="Required",
            direction="Input")
        remap.parameterDependencies = [inputraster.name,field.name]

        outputraster = arcpy.Parameter(
            displayName="Output Raster ",
            name="ouput1",
            datatype="Raster Layer",
            parameterType="Required",
            direction="Output") 


        params = [inputraster,field,remap,outputraster]
        return params

#####################################################################
### Insert this section to enable the remap buttons ###

    def initializeParameters(self):
        """Refine the properties of a tool's parameters called when the tool is opened."""
        self.params[2].parameterDependencies = [0]
        return
######################################################################
    
    def isLicensed(self):
        return True
    def updateParameters(self, parameters):

#        if parameters[0].value:
#            parameters[2].parameterDependencies = [parameters[0].name,parameters[1].name]

        return 

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        inRaster = parameters[0].valueAsText
        field = parameters[1].valueAsText
        outRaster = parameters[3].valueAsText
        remap2 = parameters[2].valueAsText

        messages.addWarningMessage(str(remap2))
        
        outputraster = Reclassify(inRaster, field, remap2, "NODATA")
        outputraster.save(outRaster)
        return
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos