Select to view content in your preferred language

Performing if else condition on two raster layers

6810
21
Jump to solution
08-26-2014 10:57 AM
LawrenceSekaluvu
Emerging Contributor

Hi,

I am working on a project and i am writing my scripts in python. However, i am experiencing an obstacle that might make me quit this project for good. Here is is the problem. I want to perform a if else condition on two raster layers that have constant values across all there grid. Initially, i create two raster layers, each with a constant value within each cell. However, when i perform a loop on these two raster layers, one of them at the end will have cells with different values and i need to loop in again to compare it with the other raster with a constant value. I tried using the "Con" tool but things have not worked at all.  Any clues please?

0 Kudos
21 Replies
DarrenWiens2
MVP Alum

Do you mean something like this doesn't work, or something else entirely?

C01 = Con("M0","M0","","VALUE == " + str(fb))

As always, please post your script so we can better see what you're talking about.

0 Kudos
LawrenceSekaluvu
Emerging Contributor

Surely, it works well. The problem was that after performing the previous Con statement, i got an error message. This error message persisted till when I restarted Arcmap again. After then, everything was moving on well.

0 Kudos
LawrenceSekaluvu
Emerging Contributor

Hi @Ian,

I want basically to perform a "if else" condition on each cell within a raster in python window environment. At first, i thought that i could get two raster layers (one with a constant value for all cells and the other with varying values within each cell) and insert the "if else" statement right away within the loop. This seems not to work out all. Lastly, I have some other three parameters that need to be calculated for each cell depending on whether the "if else" condition returns true or false.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Please post your script

0 Kudos
LawrenceSekaluvu
Emerging Contributor

here is my script

""

>>> import arcpy

... from arcpy.sa import *

...

... #calculating slope

... S = Slope("DEM","PERCENT_RISE","")

... H = S/100

...

... #initialize parameters

... T = 10

... Temp = Times("DEM",0)

... M0 = Plus(Temp,0.2)

... fb = 0.345

... D = 2

... Cellsize = 2

... d1 = 0.21

... d2 = 0.01

... d3 = 0.31

... d4 = 0.4

... d5 = 0.395

... Kr = 2.0

... Kh = 0.004

... for t in range (0, T):

...     t += 1

...    

...     #first conditional statement

...     C01 = Con("M0","M0","","VALUE >= fb")

...     C1 = Times(C01, 0)

...     Kv = (d1*(d2/d3)) + Kh

...     Qv = Kv * H * Cellsize * D

...     K1 = Plus(C1,Kv) 

...     Qh1 = Plus(C1,Qv)

...    

...     #second conditional statement

...     C02 = Con("M0","M0","","VALUE < fb")

...     C2 = Times(C02, 0)

...     Ep = Exp((-13*(d4/d5)))

...     Kf = Kr * Ep

...     Qf = Kf * H * Cellsize * D

...     K2 = Plus(C2,Kf) 

...     Qh2 = Plus(C2,Qf)

...    

...     K = K1 + K2

...     QT = Qh1 + Qh2

...     Mt = K + QT

...     M0 = Mt

...     print K, QT, Mt

     ""

0 Kudos
DarrenWiens2
MVP Alum

Your statement:

C01 = Con("M0","M0","","VALUE >= fb")

should be:

C01 = Con("M0","M0","","VALUE >= " + str(fb))

and similarly for the second Con statement.

curtvprice
MVP Alum

Why aren't you using real Python map algebra? For example, you're doing this:

Temp = Times("DEM",0)

M0 = Plus(Temp,0.2)

Python Map algebra takes advantage of the Raster object and overtyping of operators to write straightforward expressions like this:

M0 = Raster("DEM") * 0 + 0.2

Which if course, begs the question, why are you zeroing out every cell of your DEM before you add 0.2 to it?

Map Algebra is a lot easier to debug than using the tools directly (Plus, Times, etc)

Here's another example:

C01 = Con("M0","M0","","VALUE >= " + str(fb))

Using map algebra on the raster object m0 created above:

C01 = Con(m0 >= fb, m0)

curtvprice
MVP Alum

As Dan said, Con is the tool you want. A Python if/else statement will not process raster cell values cell by cell like you want to.

0 Kudos
IanMurray
Honored Contributor

Actually, you could convert a raster to an ascii, and compare cell values between the two ascii files, assuming they have the same extent.  You could even generate a new ascii based on if the values are the same, and convert it back to a raster by adding the header file from the original ascii to the output ascii(I actually have a project I'm working on that is based on this).

But yea, a conditional statement would be easier.

LawrenceSekaluvu
Emerging Contributor

@Ian,

Thanks, let me try out that and see whether it will work out quite well.

0 Kudos