<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Adding multiple rasters together using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72508#M5954</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a number of raster datasets in a folder that I want to sum/add together.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm having trouble doing this - I've created a raster list but the output doesn't seem to be adding them together.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can someone help please.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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&amp;nbsp; = arcpy.ListRasters("*")

for raster in rasterList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print raster
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute CellStatistics
&amp;nbsp;&amp;nbsp;&amp;nbsp; outCellStatistics = CellStatistics(raster, "SUM", "NODATA")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Save the output 
&amp;nbsp;&amp;nbsp;&amp;nbsp; outCellStatistics.save("C:/data/sumRast.tif")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks Claire&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 18 Jul 2012 13:58:39 GMT</pubDate>
    <dc:creator>ClaireParsons</dc:creator>
    <dc:date>2012-07-18T13:58:39Z</dc:date>
    <item>
      <title>Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72508#M5954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a number of raster datasets in a folder that I want to sum/add together.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm having trouble doing this - I've created a raster list but the output doesn't seem to be adding them together.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can someone help please.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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&amp;nbsp; = arcpy.ListRasters("*")

for raster in rasterList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print raster
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Execute CellStatistics
&amp;nbsp;&amp;nbsp;&amp;nbsp; outCellStatistics = CellStatistics(raster, "SUM", "NODATA")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Save the output 
&amp;nbsp;&amp;nbsp;&amp;nbsp; outCellStatistics.save("C:/data/sumRast.tif")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks Claire&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2012 13:58:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72508#M5954</guid>
      <dc:creator>ClaireParsons</dc:creator>
      <dc:date>2012-07-18T13:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72509#M5955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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&amp;nbsp; = arcpy.ListRasters("*")

print rasterList
# Execute CellStatistics
outCellStatistics = CellStatistics(rasterList, "SUM", "NODATA")

# Save the output 
outCellStatistics.save("C:/data/sumRast.tif")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:48:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72509#M5955</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-10T22:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72510#M5956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Phil&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Help would be much appreciated as I have to get this piece of work delivered tomorrow.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Claire&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2012 20:27:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72510#M5956</guid>
      <dc:creator>ClaireParsons</dc:creator>
      <dc:date>2012-07-18T20:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72511#M5957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2012 21:59:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72511#M5957</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2012-07-18T21:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72512#M5958</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You need to set the output extent = max of the input rasters&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Steve&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2012 22:34:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72512#M5958</guid>
      <dc:creator>SteveLynch</dc:creator>
      <dc:date>2012-07-18T22:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72513#M5959</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Andy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Sep 2012 15:34:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72513#M5959</guid>
      <dc:creator>AndrewOrr</dc:creator>
      <dc:date>2012-09-25T15:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72514#M5960</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;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. &lt;BR /&gt;&lt;BR /&gt;-Andy&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can, it just isn't as clean:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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&amp;nbsp; = arcpy.ListRasters("*")
print rasterList

sumEmpty = 'Yes'

for rasName in RasterList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; rasObject = arcpy.Raster(rasName)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if sumEmpty == 'Yes':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumRas = rasObject
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumEmpty = 'No'
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumRas = sumRas + rasObject

# Save the output 
sumRas.save("C:/data/sumRas.tif")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:48:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72514#M5960</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-10T22:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: Adding multiple rasters together using python</title>
      <link>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72515#M5961</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
yell_rasterList = arcpy.ListRasters("*")

for raster in yell_rasterList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sumRasters = Raster("constantRas") + Raster(raster)
sumRasters.save("SumRaster")

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Either way, I'm glad the cellStatistics method worked out for me. Very happy now. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;-Andy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:48:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-multiple-rasters-together-using-python/m-p/72515#M5961</guid>
      <dc:creator>AndrewOrr</dc:creator>
      <dc:date>2021-12-10T22:48:56Z</dc:date>
    </item>
  </channel>
</rss>

