Raster calculator power function error in python

716
2
07-27-2023 09:46 AM
LukeFloch
New Contributor III

I have a lengthy raster calculator expression I'm running in a python script tool that keep throwing the following error:

RuntimeError: Failed to apply Raster Function: 'RasterCalculator' (The parameter is incorrect.
Bind failed in function 'Raster Calculator Function' [Raster Calculator Function].)

I narrowed it down to the Power() function. The calculation in the script works fine if I remove it

I simplified everything down to troubleshoot:

If I run the expression Power("x", 2) in the raster calculator everything works as advertised

If I run the script y = RasterCalculator([x], ["x"], "Power(x, 2)") I get the above error. (If I change Power() to another function like Sin(x) the script will run without error)

Is this a bug or am I just missing something simple?

0 Kudos
2 Replies
jeff9870
New Contributor

I actually figured out the problem, which was an incorrect use of the ^ operator. Turns out that ^ is an xOR boolean operator, and not an exponent, and I should be using the Exp () function in its place. I'm glad someone caught it for me, otherwise I would've wasted days of frustration https://1921681254.mx/. Lol

 

0 Kudos
DuncanHornby
MVP Notable Contributor

Unless I've missed some point, seems like overkill the way you are doing it, why not simply call the power tool directly, see python code sample here.

 

0 Kudos