Select to view content in your preferred language

sum rasters

516
1
06-20-2011 06:13 PM
SarahBurns
Emerging Contributor
I posted this to spatial analyst but thought it might be more appropriate here.
I have reclassified many raster to be only 0's and 1's and I would like to sum the 1's together and output a new raster which is basically a count of those rasters.
I have used the code from a thread in spatial analyst so I am open to different tools to do this processing.
When I run the code I do receive an output but it is wrong and python keeps crashing each time I try to run it so I don't receive an error message.
Can someone please tell me what I am doing wrong.
Thanks
# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
import os

# Set environment settings
arcpy.env.workspace = "D:/sumrasters/rasterimgs"
outputfolder="D:/sumrasters/output_sumRaster"
datapath="D:/reclass/sumrasters/rasterimgs"
arcpy.env.overwriteOutput = 1
arcpy.CheckOutExtension('Spatial')
arcpy.env.scratchWorkspace = r"c:\temp\scratch.gdb"


#create a list of rasters in the workspace
rasters = arcpy.ListRasters("*","IMG")

i = 0
#loop through rasters in list
for raster in rasters:
    print rasters
    #sum rasters together
    if i == 0:
        outSUM = arcpy.Raster(raster)
        i += 1
    else:
        outSUM = outSUM + raster
        i += 0

#save final output to the current workspace
    outSUM.save(os.path.join(outputfolder,"sumRas.img"))

print "end of processing"
Tags (2)
0 Kudos
1 Reply
SarahBurns
Emerging Contributor
In case some one else is doing this I have ended up just using SUM in cell statistics. I convert the category I am interested in to a binary and then sum.
What I really wanted though was to be able to have various classes assigned and to add them depending on what class.
e.g.
If cell = 6 then convert is to a 1
if cell = anything but 6 convert it to 0
then sum rasters to produce an output image with the frequency of that category
and then repeat this for each other category.
if cell =5 then convert to a 1
else convert to 0
and sum
0 Kudos