I have several buffers to be added together (Cold, Hot, Raining, etc.), but they only need to be added if they exist.

So I currently have create buffer if Cold else no buffer. (I did convert polys to raster.) I want to set up a calculator that runs all of the buffers that exist. But I won't know which exist (large project with lots of weather specifications).

So far I am able to add them fine if everything is there, but if one specified buffer doesn't exist then it errors out.

  • Which tool did you use (raster calculator I presume) and how (what was your expression if it was raster calculator)? Also, what does error message say? – fatih_dur Sep 5 '18 at 14:54
  • What's the error message? – Tom Lee Sep 5 '18 at 15:55

The Con statement (conditional) is similiar to an IF ELSE statement and can be used with the raster calculator. I use it to add rasters together that have null values by converting the nulls to zeros. Con(isNull("raster_layer"), 0, "raster_layer")

Esri documentation: http://desktop.arcgis.com/en/arcmap/latest/tools/spatial-analyst-toolbox/conditional-evaluation-with-con.htm

blog post: https://tomtl.github.io/arcgis-conditional-statements/

  • Thanks. I ended up using the script for now, but I think I want to revisit this in the future. – Colleen Sep 6 '18 at 11:27

ModelBuilder doesn't handle IF-THEN-ELSE statements well at all. For that you're going to need a python script. A simple use of TRY-EXCEPT should handle those pesky errors for you.

Basics of TRY-EXCEPT:
This is the most basic method of error handling in python. What you do is take your code and put it inside a TRY just like an IF statement. you then have an EXCEPT, just like an ELSE statement. The code in the TRY part will run as normal until it encounters an error. When it hits an error, it will trigger the EXCEPT part of the statement. The program will then continue as normal. In the OP's case, this continuation will be with another execution of the loop.

for foo in bar:
    print "Adding buffer " + foo
    code to add buffer
    print "No buffer detected, proceeding to next iteration of loop"

See also "Try and Except in Python" over at PythonForBeginners.com

  • Not OP, but care to explain a little bit how Try- Except works please? Does it work just like IF-ELIF-THEN ? Thanks. – AndrewLebron Sep 5 '18 at 14:52
  • @AndrewLebron, added a basic explanation and a link to a more in depth look at try and except. – P.T. Curran Sep 5 '18 at 16:09

you may need to handle your extents (union of inputs) or NoData values.

When using RasterCalculator to ADD two rasters together the output of cell values


You may have areas that don't overlap so your output will be NODATA and need to fist convert nodata values = 0.

from arcpy.sa import *
outCon = Con(IsNull("buffras"),0, "buffras")



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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