Hello everyone, I got a string variable as an input parameter, where you can choose between yes or no. Now I would like to use this input in the "Raster calculator", so the string should be converted in a double (values 1 and 2). My programming skills are not that powerful so Python was not option for me. So I tried it via "Calculate Value" in the Model Builder but my block Code did not work at all (perhaps because of my programming skills...). Hopefully you got a solution for my problem. Sorry for my bad english btw...
Greetings from Germany
Kevin
Solved! Go to Solution.
Here's a Calculate Value expression that will do what you want. Rename the Calculate Value output "OptValue"
# expression
f(r"%Option%")
# code block
def f(opt):
if opt == "YES":
return 1.0
else:
return 2.0
# data type: Double
Then, open the Raster Calculator tool and build this expression.
This is the python float function (lower case, not the arcpy.sa tool Float)
"%raster dataset%" * float(%OptValue%)
Here's a Calculate Value expression that will do what you want. Rename the Calculate Value output "OptValue"
# expression
f(r"%Option%")
# code block
def f(opt):
if opt == "YES":
return 1.0
else:
return 2.0
# data type: Double
Then, open the Raster Calculator tool and build this expression.
This is the python float function (lower case, not the arcpy.sa tool Float)
"%raster dataset%" * float(%OptValue%)
UPDATE: the float() is not necessary - it works without it.
Executing (Calculate Value): CalculateValue f("YES") "def f(opt):\n if opt == "YES":\n return 1.0\n else:\n return 2.0" Double
Start Time: Thu Aug 10 13:39:45 2017
Value = 1
Succeeded at Thu Aug 10 13:39:45 2017 (Elapsed Time: 0.06 seconds)
Executing (Raster Calculator): RasterCalculator ""dem_anal_Clip" * 1" D:\Users\cprice\work\inc\zstats_issue\zonalstats_issue.gdb\rastercalc
Start Time: Thu Aug 10 13:39:45 2017
Raster(r"dem_anal_Clip") * 1
Succeeded at Thu Aug 10 13:39:46 2017 (Elapsed Time: 0.68 seconds)
Thanks alot! It is working!
You are welcome! Please do mark my answer correct so others can easily find it.
For the good of the thread, here's a fancier one-line Calculate Value expression (no code block needed) to convert a string to a float value. I am using a Python dictonary data structure to do this.
{"YES":1.0, "NO":2.0}["%Option%"]
UPDATE:
and -- if only a binary choice, we can use Python's ternary if construct
1.0 if "%Option%" == "YES" else 2.0