Map Algebra syntax for exponentiation (raising to a power)

4567
7
02-24-2014 05:18 PM
AlainSte-Marie
New Contributor

I use an hydro tool developped by the Mineasota Gov.

Description: This script creates a raster whose cells measure the length of time
in seconds, that it takes water to flow across it and then accumulates
the time from the cell to the outlet of the watershed. water velocity
is calculated as a function of hydraulic radius, Manning's N and slope
Using a DEM as a source, flow direction, flow accumulation and slope (percent)
is calculated.

This tool creates a lot of raster by on various imputs and formulas.
I would like to change a formula:, this is the original:
exp = '(' + str(hydRad1) + ' * (' + flowAcc + ' * ' + cellSqMilesStr + ')) + ' + str(hydRad2) + ''

What I would like is : hydRad1 * ((' + flowAcc + ' * ' + cellSqMilesStr + ') ^ hydRad)

Thank you!

0 Kudos
7 Replies
FilipKrál
Occasional Contributor III
Hello,
If  I understand you correctly, you want to use power operation in your formula.

In Python, the hat operator (^) means binary XOR, whereas double times (**) is used to indicate power.
The ** operator is recognized by Spatial Analyst too, so your desired formula would be:

exp2 = str(hydRad1) + ' * ((' + str(flowAcc) + ' * ' + atr(cellSqMilesStr) + ') ** ' + str(hydRad) + ')'


This should work if your operands are scalar values or saved raster datasets, not temporary arcpy.Raster objects.
Make sure your last operand is correct (is it hydRad or hydRad2 ?).
If licensing of the script allows, you should be able to simply change the line in the script and run the tool again.

I hope this helps.
Filip.
0 Kudos
JohnDye
Occasional Contributor III
I prefer using the math module.

import math

# In the use of the 'math.pow' method, the value x is raised to the power of y
math.pow(x, y)

0 Kudos
curtvprice
MVP Esteemed Contributor

math.pow() will not work with rasters, only with numbers.

However, the Power tool can take value or raster inputs.

This used to be the pow() function in GRID and Arc 9.x map algebra. I miss that.

and, Python string substitution is your friend. compare these two:

hydRad1 + '* ((' + flowAcc + ' * ' + cellSqMilesStr + ') ^ ' + hydRad + ')'

"{} * (({} * {}) ** {}".format(hydRad1, flowAcc, cellSqMilesStr, hydRad)

0 Kudos
curtvprice
MVP Esteemed Contributor

And, I should add in Python you should not use the map algebra tool. Best to do Python map algebra directly:

from arcpy.sa import *
outras = Raster(hydRad1) * ((Raster(flowAcc) * float(cellSqMilesStr) ** Raster(hydRad)
# or
outras = Raster(hydRad1) * Power((Raster(flowAcc) * float(cellSqMilesStr), Raster(hydRad))
outras.save("results.tif")

FilipKrál
Occasional Contributor III

Good point, Curtis.

String substitution aka string formatting can make the whole code much more readable. Especially considering all the options the format method offers.

F.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Filip.... some other links as well Basic fancy formats .... cover simple and other data structures

0 Kudos
RickMoore1
New Contributor

Alain,

I am interested in the hydro tool that you referenced in your question from 2014.  Were you ever able to make it work with the changes to the equation concerning the power.  I would be interested in seeing the new code that was created for this tool as we use this tool in Minnesota.

Thanks,

Rick

0 Kudos