Select to view content in your preferred language

Adding multiple rasters together using python

4431
7
07-18-2012 06:58 AM
ClaireParsons
New Contributor
I have a number of raster datasets in a folder that I want to sum/add together.
I'm having trouble doing this - I've created a raster list but the output doesn't seem to be adding them together.

Can someone help please.

import arcpy, os
from arcpy import env

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

env.workspace = r"C:\Users\Documents\GIS\Site Selection Example\Output Datasets\Cons_Raster"

rasterList  = arcpy.ListRasters("*")

for raster in rasterList:
    print raster
    # Execute CellStatistics
    outCellStatistics = CellStatistics(raster, "SUM", "NODATA")

    # Save the output 
    outCellStatistics.save("C:/data/sumRast.tif")


thanks Claire
Tags (2)
0 Kudos
7 Replies
PhilMorefield
Occasional Contributor III
You don't need to iterate through each raster in your list. You just need to provide the tool with the list of rasters one time, and it will add them together.

import arcpy, os
from arcpy import env

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

env.workspace = r"C:\Users\Documents\GIS\Site Selection Example\Output Datasets\Cons_Raster"

rasterList  = arcpy.ListRasters("*")

print rasterList
# Execute CellStatistics
outCellStatistics = CellStatistics(rasterList, "SUM", "NODATA")

# Save the output 
outCellStatistics.save("C:/data/sumRast.tif")
0 Kudos
ClaireParsons
New Contributor
Hi Phil

thanks for your reply - this still isn't working - the output is a fraction of the size it should be (See attached) - it even does this when I use the tool in ArcMap but I do get a larger area.

Help would be much appreciated as I have to get this piece of work delivered tomorrow.

thanks

Claire
0 Kudos
PhilMorefield
Occasional Contributor III
I don't think the image you tried to attach is showing up. There are few things I would check. First, make sure all of your rasters have the same extent and projection. If any part of any raster has no data, your output for that cell will have no data. You could also set the processing extent to the union on all inputs, just to be thorough.

I'm assuming you checked the value for 'rasterList' and all of the file names or included and look okay? As a matter of best practice, you shouldn't have spaces or special characters in file names or paths; you currently have some spaces in your folder names.
0 Kudos
SteveLynch
Esri Regular Contributor
You need to set the output extent = max of the input rasters

Steve
0 Kudos
AndrewOrr
Occasional Contributor
I just did the procedure Phil provided and it worked well for me. However, why on earth can you not iterate through a list of rasters and have each one add together? I spent a good 10 hours trying to figure out why this would not work. Seems to me a very stupid limitation since this works for multiple other tools where you can iterate through lists of files.

-Andy
0 Kudos
PhilMorefield
Occasional Contributor III
I just did the procedure Phil provided and it worked well for me. However, why on earth can you not iterate through a list of rasters and have each one add together? I spent a good 10 hours trying to figure out why this would not work. Seems to me a very stupid limitation since this works for multiple other tools where you can iterate through lists of files.

-Andy


You can, it just isn't as clean:

import arcpy, os
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

arcpy.env.workspace = r"C:\Users\Documents\GIS\Site Selection Example\Output Datasets\Cons_Raster"

rasterList  = arcpy.ListRasters("*")
print rasterList

sumEmpty = 'Yes'

for rasName in RasterList:
    rasObject = arcpy.Raster(rasName)
    if sumEmpty == 'Yes':
        sumRas = rasObject
        sumEmpty = 'No'
    else:
        sumRas = sumRas + rasObject

# Save the output 
sumRas.save("C:/data/sumRas.tif")
0 Kudos
AndrewOrr
Occasional Contributor
Thanks Phil! I guess my issue was trying to convert the names into Raster objects within the actual "raster calculator" equation itself. I don't have the exact code I was using, but it was something on the order of:

yell_rasterList = arcpy.ListRasters("*")

for raster in yell_rasterList:
     sumRasters = Raster("constantRas") + Raster(raster)
sumRasters.save("SumRaster")



That's probably not the correct syntax for what I had used, but I think it gets the idea across. I was getting an error saying that I couldn't convert an object to a raster or something like that.

Either way, I'm glad the cellStatistics method worked out for me. Very happy now. 🙂
-Andy
0 Kudos