Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

If you have produced a long list of rasters (lets say 50) and want to add them together, is there any shorter way to write in python rather than typing all the inputs, like so:

outRas = inRas1 + inRas2.........+ inRas50
share|improve this question
add comment

1 Answer 1

You can make a list of the raster names you wish to add together, then step through the list, adding each one to an output raster.

import arcpy
from arcpy import env

# Set the current workspace
# 
env.workspace = "C:/Data/DEMS"

#Check out ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")


# Get a list of ESRI GRIDs from the workspace and print
#

rasterList = arcpy.ListRasters("*", "GRID")

#Step through the list of raster names
for rasname in rasterList:
    #Cast rasname as raster before adding
    listras = Raster(rasname)
    #Add each raster to an output raster.
    #The previous step of casting as a Raster will invoke
    #the Spatial Analyst Addition function.
    outras += listras

#Save output Raster
outras.save("C:/temp/outras")
share|improve this answer
1  
Another possibility, once you have a list of rasters, is to use CellStatistics with the "SUM" option. –  blah238 Aug 22 '12 at 18:03
    
I got what your suggestion. But, what if I have other rasters in my workspace and don't want to add them? –  tdr Aug 22 '12 at 18:32
2  
Because you have had the foresight to number the raster datasets systematically, simply create the list of raster names with a small loop. Alternatively, filter rasterList to exclude any raster you do not want to process. –  whuber Aug 22 '12 at 18:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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