3

I have two rasters for same spatial extent and need to compare both rasters cell by cell and produce one output raster. Each has 10 land cover classes. Based on those 10 classes the comparison should be 100 conditional statements.

Here I wrote two statements and did a test run. It gives me only a output based on the last conditional statement. Which is output raster has cell values only 21.

outCon = Con (((inRaster1 == 11) & (inRaster2 == 11)), inRaster1 + inRaster2, 11)
outCon = Con (((inRaster1 == 21) & (inRaster2 == 11)), inRaster1 + inRaster2, 21)
outCon.save ("G:\\LC_Transitions\\con_test")

Any suggestions on what I did wrong in here?

Also, if I want to run a statement for multiple conditions together (for ex: If (InRaster = 21, 22, 23, 24) and (inRaster2 = 11) - return 11) could anyone suggest the correct syntax for this kind of multiple conditions.

3
  • It looks like combine might help you do all 100 operations in one step.
    – whuber
    Commented Aug 21, 2012 at 16:01
  • Combine will not meet my requirement. Because, I need to assign different values for the output raster. Combine will just add the two rasters together and show both the values.
    – tdr
    Commented Aug 22, 2012 at 15:30
  • And that takes care of the major problem. You finish the task by creating a (simple) lookup table that translates the combined identifiers into the values you want and you join that table to the grid's attribute table. Unless your operations correspond to a fairly simple mathematical formula, there is no simpler or more efficient way to get your job done.
    – whuber
    Commented Aug 22, 2012 at 16:23

2 Answers 2

2

I see two potential issues right away:

  1. Replace "G:\LC_Transitions\con_test" with r"G:\LC_Transitions\con_test"
  2. Name your two objects different names (e.g. outCon1 and outCon2)
2
  • Thanks a lot for your comments. So, you suggest me to give different names for each output and then add them together to make one final output?
    – tdr
    Commented Aug 21, 2012 at 14:34
  • I tried and it works. But, the new problem is it gives me some unrealistic numbers (not the values that it suppose to be. for ex: it should have 11, 21, etc. but the output gives me 54, 74, etc)
    – tdr
    Commented Aug 21, 2012 at 15:30
0

After all your supports and other helps I figured out how to give the conditional statements to multiple rasters.

outCon1 = Con (((inRaster1 == 11) & (inRaster2 == 11)), 11, 0)
outCon2 = Con (((inRaster1 == 21) & (inRaster2 == 11)), 21, 0)
outCon3 = ..................
outCon = outCon1 + outCon2 + outCon3 + ........
outCon.save (r"G:\LC_Transitions\con_test")
2
  • 3
    Sure, this will work. For future interested readers, please note that this is a very inefficient and complicated way to solve the problem. With 100 rasters it involves over 100 lines of code and some 300 raster calculations! In some cases mathematical formulas will be efficient; in the most general case, one application of combine followed by a reclassification will do the work in only two lines of code and two raster operations (although I admit that somewhere, somehow, the 100 desired outputs have to tabulated: this would occur in the reclassification table).
    – whuber
    Commented Aug 22, 2012 at 18:36
  • 1
    I agree with you. I did the "combine" also for this project and use the field_calculator to assign the logical value that I wanted instead of reclassification. However, what I want to try in here is directly produce a raster with "value" field representing the exact code value that I wanted. I'm just a beginner to Python and need lots of support from you guys. I greatly appreciate your comments and looking forward some efficient methods for this process.
    – tdr
    Commented Aug 22, 2012 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.