arcpy.env.overwriteOutput = 1 arcpy.CheckOutExtension('Spatial') arcpy.env.scratchWorkspace = outPath arcpy.env.workspace = dataPath #create a list of rasters in the workspace rasters = arcpy.ListRasters('','') i = 0 #loop through rasters in list for raster in rasters: print "processing raster: %s" %os.path.join(dataPath,raster) #convert nodata to zero out1 = Con(IsNull(raster), 0, raster) #sum rasters together if i == 0: out2 = arcpy.Raster(out1) i += 1 else: out2 = out2 + out1 i += 1 #save final output out.save(os.path.join(outPath,'sumRas'))
import arcpy import os arcpy.CheckOutExtension('Spatial') arcpy.env.scratchWorkspace = r"c:\temp\scratch.gdb" arcpy.env.workspace = r"c:\temp\work.gdb" #create a list of rasters in the workspace rasters = arcpy.ListRasters("*", "GRID") i = 0 #loop through rasters in list for raster in rasters: print ("processing raster: %s" %os.path.join("datapath",raster)) #sum rasters together if i == 0: outSUM = arcpy.Raster(raster) i += 1 else: outSUM = outSUM + raster i += 1 #save final output to the current workspace outSUM.save('sumRas')
# 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:/DSE_work/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"
Hi Magnus, here is abit of code that could help get you started. I didn't test it but give it a try (see attached file). good luck-Ryanarcpy.env.overwriteOutput = 1 arcpy.CheckOutExtension('Spatial') arcpy.env.scratchWorkspace = outPath arcpy.env.workspace = dataPath #create a list of rasters in the workspace rasters = arcpy.ListRasters('','') i = 0 #loop through rasters in list for raster in rasters: print "processing raster: %s" %os.path.join(dataPath,raster) #convert nodata to zero out1 = Con(IsNull(raster), 0, raster) #sum rasters together if i == 0: out2 = arcpy.Raster(out1) i += 1 else: out2 = out2 + out1 i += 1 #save final output out.save(os.path.join(outPath,'sumRas'))
rasters <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">''</SPAN><SPAN class="punctuation token">)</SPAN> rastersum <SPAN class="operator token">=</SPAN> rasters<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> raster <SPAN class="keyword token">in</SPAN> rasters<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> rastersum<SPAN class="operator token">+=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>raster<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I know this is a bit late but I came across this from google and thought I'd clean up your for loop.
A few comments on your code:
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.