TransWikia.com

Writing Python toolbox that gives two options for one parameter?

Geographic Information Systems Asked on July 30, 2021

I’m working on a Python toolbox that buffers features using slope distance based on an input DEM rather than horizontal distance. The tool works great with buffer widths derived from a field in the input feature.

I’d like to add the option to buffer by one given width like the ArcGIS proximity tool, Buffer, resulting in a radio button choice:

enter image description here

2 Answers

I got to where I wanted to go using the updateParameters and updateMessages functions:

def updateParameters(self, parameters):
    if parameters[0].altered:
        if parameters[0].value:
            parameters[1].enabled = False
        else:
            parameters[1].enabled = True
    if parameters[1].altered:                
        if parameters[1].value:
            parameters[0].enabled = False
        else:
            parameters[0].enabled = True
return

def updateMessages(self, parameters):
    if not parameters[1].value and not parameters[2].value:
        parameters[1].setErrorMessage(
            '''Either parameter 1 OR parameter 2 must be supplied, but not both.''')
        parameters[2].setErrorMessage(
            '''Either parameter 1 OR parameter 2 must be supplied, but not both.''')
    elif parameters[1].value and parameters[2].value:
        parameters[1].setErrorMessage(
            '''Either parameter 1 OR parameter 2 must be supplied, but not both.''')
        parameters[2].setErrorMessage(
            '''Either parameter 1 OR parameter 2 must be supplied, but not both.''')

return

When the tool loads, the parameters appear as though required, with the green dot:

enter image description here

If both parameters are populated, then it becomes a red error X with the error message:

enter image description here

The updateParameters function disables the other parameter once one parameter is populated.

Correct answer by Ian Yau on July 30, 2021

I use a checkbox and then a conditional statement on the existence of the moveTapToBP variable to achieve a similar logic split.

param2 = arcpy.Parameter(
    displayName = "Move un-snapped taps to their building points",
    name = "moveTapToBP",
    datatype = "GPBoolean",
    parameterType = "Required",
    direction = "Input")
param2.value = False

Answered by maverickGIS on July 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP