3

I'm using ArcGIS for Desktop 10.3.0.4322

I have a script tool which requires a parameter (folder path to a gdB, via GetParameterAsText) so that a later process can be saved there. The thing I want to do is make an Optional Boolean parameter, that when checked, automatically fills in the folder path gdB parameter. The Boolean text asks the user, "Use default workspace?", to which if they check the box (boolean == True), then the folder path parameter will be automatically defaulted (saving the user a small amount of time). I have already created the Boolean parameter in the script tool, I am however unaware of how to "fill in" the required parameter based on the user's Boolean.

Code Snippet:

# Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)
# Ask user if they want to use default workspace (boolean)
useDefault = arcpy.GetParameterAsText(1)
# If user wants default workspace, then give it to the required parameter (0)
if useDefault == True:
 arcpy.env.workspace = path
else:
 # Set workspace environment based upon user's choice
 arcpy.env.workspace = userWorkspace
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jul 9, 2015 at 22:05
0

5 Answers 5

7

Try adding a print useDefault immediately after you set it to see what is returned.

I have a suspicion (without testing) that it may be returning 'True'(or 'False') rather than 'true' (or 'false').

Also, arcpy.GetParameterAsText() always returns a string so you need to test for the String of 'True'(or 'False').

If you use arcpy.GetParameter() then you will be able to test for the Booleans True (or False).

answered Jul 9, 2015 at 22:37
0
6

Extending @PolyGeo's answer, running this snippet from the command line and from a toolbox with a single parameter set as "boolean, optional" will give slightly different returns.

import arcpy
useDefault = arcpy.GetParameterAsText(0)
# report to Arcgis message queue
arcpy.AddMessage(useDefault)
arcpy.AddMessage(type(useDefault))
# report to shell console
print useDefault
print type(useDefault)

When run from a toolbox the message is:

true
<type 'unicode'>

and from a command line, python test.py True:

True
<type 'str'>

When we change to using arcpy.GetParameter(0) the message queue result is:

1
<type 'bool'>

but the command line report remains unchanged:

True
<type 'str'>

So the useDefault parameter is not a native python boolean. You (sadly) can't use standard python idioms like if useDefault: ..., but must test against strings:

usedDefault = GetParameterAsText(0)
if not useDefault.lower() == 'true':
 #... do "option box not checked" stuff
if useDefault.lower() == 'true':
 #... do "option box IS checked" stuff

Strictly speaking the .lower() suffix is not needed if the script will (a) always be run from a toolbox, and (b) be defined as a boolean parameter (checkbox). It's added here for clarity and flexibility.

answered Jul 14, 2015 at 17:10
0
2

This can be accomplished via the use of ArcGIS's Script Tool Validation: http://resources.arcgis.com/en/help/main/10.1/index.html#//00150000002m000000

def updateParameters(self): 
 """Modify the values and properties of parameters before internal 
 validation is performed. This method is called whenever a parameter 
 has been changed.""" 
 if self.params[1].value == True: 
 self.params[0].value = arcpy.env.workspace 
 return

GUI Pic GUI Pic #2

answered Jul 13, 2015 at 21:43
0

ArcPy is a bit weird with the GetParameterAsText() for the script options. For whatever reason I couldn't test mine as a straight Boolean True/False, as you noted in your question. You can, however, try:

if str(useDefault) == 'true':
 arcpy.env.workspace = path

That should work just as you intended.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Sep 29, 2017 at 20:05
0

Try omitting the AsText part. Use:

useDefault = arcpy.GetParameter(1)

It won't change the boolean to a text string. It will get the actual Boolean parameter.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
answered Jun 17, 2021 at 20:38
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.