Select to view content in your preferred language

Problem with script tool parameters (parameter comes in as string when I want float)

1744
13
Jump to solution
06-10-2014 08:37 AM
AmelieDavis
Emerging Contributor
Hi!

I have a python script that runs fine stand alone but when I try to make it GUI based by adding it to a toolbox as a script I get errors which I believe are related to how I am setting the parameters.

I have three values that I need to be set as a real number with a decimal but they keep coming in as string.

In ArcGIS toolbox, under properties of the model, and the parameters tab, I set those three parameter types to Double (I have also tried String but that doesn't work either).

then in python, that part of the code looks like this:
# -*- coding: utf-8 -*-  ############################################################################  import sys # Import arcpy module import arcpy #from arcpy import env  # Set this to False once testing is complete arcpy.env.overwriteOutput = True  ############################################################################     # Script arguments  # user defined weights for importance of up to three floral seasons Spring = float(arcpy.GetParameter(3)) arcpy.AddMessage("Floral Weight #1 is equal to: " + str(Spring)) if Spring == '#' or not Spring:     Spring = 0.3 # provide a default value if unspecified check1 = arcpy.Parameter(Spring) arcpy.AddMessage("Data type is: " + str(check1.datatype))  Summer = float(arcpy.GetParameter(4)) if Summer == '#' or not Summer:     Summer = 0.6 # provide a default value if unspecified      Fall = float(arcpy.GetParameter(5)) if Fall == '#' or not Fall:     Fall = 0.1 # provide a default value if unspecified      Tot = Spring + Summer + Fall arcpy.AddMessage("Floral Weights are equal to: " + str(Tot)) if Tot == 1:     arcpy.AddMessage("Sum of floral weights is equal to: " + str(Tot)) else:     arcpy.AddError("The sum of floral weights must equal to one.")     sys.exit() # exit out of model if weights don't add up to 1  ############################################################################


I've tried GetParameter and GetParameterAsText but the value always seems to be coming in as a string.

I know this is probably something very basic but I just can't figure it out.

I'm using ArcGIS 10.2.

Thank you in advance for any assistance.

Sincerely,
Amelie
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AdamCox1
Deactivated User
Hi Amalie, the first thing I'd try out is using GetParameterAsText, that way you are for sure dealing with a string.  Then try casting it as a float for a new variable, and print the object type.  Something like:
Spring = arcpy.GetParameterAsText(3) if Spring == '#' or not Spring:     Spring = 0.3 # provide a default value if unspecified Spring_float = float(Spring) arcpy.AddMessage(type(Spring))

Hopefully that'll work as a good first step.

View solution in original post

0 Kudos
13 Replies
AdamCox1
Deactivated User
Hi Amalie, the first thing I'd try out is using GetParameterAsText, that way you are for sure dealing with a string.  Then try casting it as a float for a new variable, and print the object type.  Something like:
Spring = arcpy.GetParameterAsText(3) if Spring == '#' or not Spring:     Spring = 0.3 # provide a default value if unspecified Spring_float = float(Spring) arcpy.AddMessage(type(Spring))

Hopefully that'll work as a good first step.
0 Kudos
AdamCox1
Deactivated User
For what you're trying to do, you may want to check out the Tool Validator class.  Without too much trouble you'd be able to update the updateParameters function to throw a warning/error in the tool interface before your final script is actually run.
0 Kudos
AmelieDavis
Emerging Contributor
Hi!

That does work - sorry. 

Now I have a float as data type but this doesn't work:

Tot = Spring + Summer + Fall
arcpy.AddMessage("Floral Weights are equal to: " + str(Tot))
if Tot == 1:
    arcpy.AddMessage("Sum of floral weights is equal to: " + str(Tot))
else:
    arcpy.AddError("The sum of floral weights must equal to one.")
    sys.exit() # exit out of model if weights don't add up to 1

Any ideas?
Amelie
0 Kudos
AdamCox1
Deactivated User
Well, to be sure you'll have to be more specific about how it doesn't work (is there an error? what error?).  Though maybe you just need to have 1.0 instead of 1

I was just doing a little testing, and if you have the parameters as Double, you should be able to use GetParameter and the result will be a float object.  Also, it looks like having a parameter set as Double will force it to 0 if nothing valid is entered, which would be useful.
0 Kudos
ClintDow
Deactivated User
The way you are checking the type is incorrect. arcpy.Parameter(x) will create a new Parameter object, not provide information about the parameter itself. Therefore check1.datatype will always default to String, since it is the default data type of a new Parameter object.

Assuming your parameters are set like this in the script -
[ATTACH=CONFIG]34484[/ATTACH]

test = arcpy.GetParameter(0)
arcpy.AddMessage(type(test))


Should return <type 'float'>

You can use these in conditionals like this:
if type(1.0) == float: print "true"



Also, its worth mentioning '#' in a double parameter field will actually return 0, not the '#' character.

Hope that helps.
0 Kudos
AmelieDavis
Emerging Contributor
Sorry, it's not an error it's just not what I would expect.

I've changed "if Tot == 1" to "if Tot == 1.0"

If in the tool GUI I set Spring = 1, Summer = 0, and Fall = 0.
Then I go into the if statement and the rest of the script runs (i get the "Sum of floral weights is equal to: 1.0 message). 
This is what I would expect. 

BUT if in the tool GUI I set Spring = 0.6, Summer = 0.3, and Fall = 0.1,
Then I go into the else statement and the tool stops running even though I've added a "catch message" (arcpy.AddError("The sum of floral weights must equal to one, but it is equal to " + str(Tot))) and it tells me that Tot is equal to 1.0.

What do you think?
Amelie
0 Kudos
AdamCox1
Deactivated User
Try out
if str(Tot) == "1.0"

It seems like it's just a type issue, so the best thing to do is just keep adding a bunch of messages and type-checking.
0 Kudos
AmelieDavis
Emerging Contributor
this is utterly bizarre.

I tried
if Tot == "1.0":
and nothing worked (went into the else loop and sys.exit() ).

So then, I kept the code with
if Tot == 1.0:


and if the values for Spring, Summer, and Fall in the GUI are set to 0.2,0.6,0.2, respectively program runs completely
So does 0.4, 0.6, 0
This works too 0.6, 0, 0.4

but not 0.6, 0.3, 0.1 (it goes into the else loop and does sys.exit() ).

I'm tearing my hair out.
0 Kudos
AdamCox1
Deactivated User
Yes, that makes no sense.  It's a very simple operation, so I bet it has to do with something silly.  I just made three Double parameters, and the following code works perfectly:
import arcpy

a = arcpy.GetParameter(0)
b = arcpy.GetParameter(1)
c = arcpy.GetParameter(2)

total = a + b + c
arcpy.AddMessage(total)
if total == 1.0:
    arcpy.AddMessage("  all good")
else:
    arcpy.AddMessage("  all not good")

I think you should scale it back to something simple like that.  If you set the parameters as required in the tool properties, the user will not be able to run it without entering something, and with Double parameters, if they enter something that is nonsense, like a letter, the parameter will automatically set to 0.  Start from scratch and this should work fine.

Be gentle on the hair.
0 Kudos