5

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

2 Answers 2

9

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")
5
  • 2
    Another possibility, once you have a list of rasters, is to use CellStatistics with the "SUM" option.
    – blah238
    Commented Aug 22, 2012 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
    Commented Aug 22, 2012 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
    Commented Aug 22, 2012 at 18:38
  • 1
    I am getting error like outras is not defined! or python hangs!
    – Learner
    Commented Sep 17, 2016 at 6:53
  • @SIslam Without seeing your code, it is not possible to figure out what the problem is. The comments of this question is probably not the best place to try to figure out what the problem is. I think your best bet would be to post the problem you are having as a new question on GIS.se, along with what you are trying to do, a code sample, etc. Then, people will be able to answer your question directly with all the pertinent information available. Commented Sep 17, 2016 at 6:56
1

Another option may just using the python built-in sum function as below

Step-1:Get list of the paths of the rasters

import arcpy,os
raster_pths = []
for root, dirs, files in os.walk(raster_input_folder):
    for single_file in files:
        if single_file.endswith('.tif'):
            input_raster_pth = os.path.join(root,single_file)
            inpt_raster = os.path.join(root, single_file)
            raster_pths.append(inpt_raster)

Step-2:Now create a list of arcpy raster using those collected rasters path-it needs memory

needed_rasters_virtual = [arcpy.Raster(i) for i in needed_rasters]

Step-3:Now do the sum

summed_raster = sum(needed_rasters_virtual)

Though the above method needs a lot of memory in case of large raster but it comes handy when you need to do some complex steps at easy go like-

Say you have a value list estimates = [ 0.292088680, -0.000046162, -0.000014440, -0.000023292, -0.000027125, -0.000000819, 0.000032517, -0.000021072, 0.000004663, 0.000047970, 0.000111651, 0.000008808, -0.000014821] and another raster list,say rsts of same length. Now you want to multiply each element of the rsts by respective element of the estimates list according to their position order and after all you want to get the sum of all of these rasters.I this case this way may comes handy as below-

for the above process:

rsts = [arcpy.Raster(i) for i in needed_rasters]
summed_raster = sum([i*j for i,j in zip(needed_rasters_virtual,estimates)])

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.