1

I'm just getting started with Python toolboxes in ArcGIS and I'm pretty new to Python in general. I've created a python toolbox which

  1. takes an raster input file
  2. derives some values based on the name of the input file (via a custom function)
  3. suggests several parameter values
  4. processes the files in some way (not relevant to my question)

My issue is, that I cant seem to override the suggested parameter values when I run the tool. The values always switch back to the original values as suggested via the custom function. My guess is that whenever I change the suggested parameter, the function updateParameters() is run which in turn overrides my manual input. Is there a way to solve this dilemma?

I've reduced my toolbox (.pyt-File) to a minimal example (excluding the geoprocessing part) in the following code. In this example, the function suggestName() returns a new filename with the ending ".shp" and a path to the scratch folder. Imagine the function suggestName() to be slightly more complex and returning several values, not just one.

import arcpy


def suggestName(inputname):
    "Extract the Sheetnumber and Year from a given map Name"
    return inputname+".shp"


class Toolbox(object):
    def __init__(self):
        """Toolbox"""
        self.label = "Toolbox"
        self.alias = "Toolbox"

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


class Tool(object):
    def __init__(self):
        """Some Info"""
        self.label = "Tool"
        self.description = "Tool"
        self.canRunInBackground = False


    def getParameterInfo(self):
        """"""


        ## Raster map to extract features from
        param0 = arcpy.Parameter(
            name = "inputRaster",
            displayName = "Inpurt Raster File (TA25)",
            parameterType = "Required",
            direction = "Input",
            datatype = "DERasterDataset"
            )


        ## Output feature class
        param1 = arcpy.Parameter(
            displayName="Output Features",
            name="out_features",
            datatype="GPFeatureLayer",
            parameterType="Requred",
            direction="Output")



        return [param0,param1]



    def updateParameters(self, parameters):
        """"""

        if parameters[0].value:
            parameters[1].enabled = True

            newname = suggestName(arcpy.Describe(parameters[0].valueAsText).basename)


            parameters[1].value = newname
        return


    def execute(self, parameters, messages):
        """"""
        return

Edit:

It seems like I wasn't very clear in clarifying my issue, I'll try to do so now. As you can see in the screenshot below, the tool suggests the name "test.shp" as the output file. But I cannot override this parameter. If I try to do so (e.g. change the name to "test_2.shp") the parameter switches back to the original name ("test.shp") once I click anywhere outside the box.

Screenshot of my tool

  • I am wondering, if I understand your question corectly. If you want multiple in put you can use: params.multiValue = True or you can take a look in arcgis example here: pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/basics/… – Shiuli Pervin Feb 2 '18 at 9:08
  • 1
    It seems I wasn't very clear with what my issue is. I've tried to explain it better by adding an Edit: with a screenshot. Is it clearer now? – Ratnanil Feb 2 '18 at 14:38
1

I don't know whether the answer was too obvious or my question too obscure. In any case, I've found a pragmatic solution to my problem. Add add the option "override default values?" (boolean), which when set to "true" allows manual input of the parameters without the values being updated by updateParameters()

Here's the final solution (see code below). Interesting side note: param2.value must be set in uppercase (True / False) while parameters[2] returns the boolean in lower case (true / false).

import arcpy


def suggestName(inputname):
    "Extract the Sheetnumber and Year from a given map Name"
    return inputname+".shp"


class Toolbox(object):
    def __init__(self):
        """Toolbox"""
        self.label = "Toolbox"
        self.alias = "Toolbox"

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


class Tool(object):
    def __init__(self):
        """Some Info"""
        self.label = "Tool"
        self.description = "Tool"
        self.canRunInBackground = False


    def getParameterInfo(self):
        """"""


        ## Raster map to extract features from
        param0 = arcpy.Parameter(
            name = "inputRaster",
            displayName = "Inpurt Raster File (TA25)",
            parameterType = "Required",
            direction = "Input",
            datatype = "DERasterDataset"
            )


        ## Output feature class
        param1 = arcpy.Parameter(
            displayName="Output Features",
            name="out_features",
            datatype="GPFeatureLayer",
            parameterType="Requred",
            direction="Output")

        param2 = arcpy.Parameter(
            displayName="Override default values?",  
            name="override",  
            datatype="GPBoolean",  
            parameterType="Optional",  
            direction="Input"
            ) 

        param2.value = False



        return [param0,param1,param2]



    def updateParameters(self, parameters):
        """"""

        if parameters[0].value:
            if str(parameters[2].valueAsText) == "false":
                newname = suggestName(arcpy.Describe(parameters[0].valueAsText).basename)

                parameters[1].value = newname
        return


    def execute(self, parameters, messages):
        print(parameters[2].valueAsText)
        """"""
        return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.