Sum rasters failing with "Unexpected error"

367
2
Jump to solution
03-27-2018 10:00 AM
ChrissyWhite
New Contributor III

Hi, I am very new to python and am coming across an issue that I haven't been able to work out on my own.

I have 9 rasters that I need to sum. Nothing fancy, just a simple sum. I have this code that I used in another script (found on some blog somewhere) and it worked without any issue whatsoever:

    rasters = arcpy.ListRasters('cf_*')

    i = 0
    for ras in rasters:
        if i == 0:
            sumras = arcpy.Raster(ras)
            i += 1
        else:
            sumras = sumras + ras
            i += 1
    sumras.save(outRasterCFS)

However, in my current script it fails with:

Traceback (most recent call last):
  File "<module1>", line 32, in <module>
  File "<module1>", line 25, in Cut
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 4329, in Plus
    in_raster_or_constant2)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 4326, in Wrapper
    ["Plus", in_raster_or_constant1, in_raster_or_constant2])
RuntimeError: ERROR 999998: Unexpected Error.

When researching how to resolve this, I found another post somewhere that suggested cellstatistics might be better, so:

sumras = CellStatistics(rasters,"SUM","DATA")
sumras.save(outRasterCFS)

But it fails with:

Traceback (most recent call last):
  File "<module1>", line 32, in <module>
  File "<module1>", line 16, in Cut
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 3149, in CellStatistics
    ignore_nodata)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 3145, in Wrapper
    [function] + Utils.flattenLists(in_rasters_or_constants))
RuntimeError: ERROR 999998: Unexpected Error.

Usually, when I run into problems that aren't very clear, I run it manually from ArcMap and can usually figure out what is going wrong in my python, but when I run CellStatistics from ArcMap, it fails as well:

ERROR 010092: Invalid output extent.

I really have no idea where to go from here. I can't find anything online that points me in a helpful direction...

Any thoughts or suggestions? I would greatly appreciate it!

Here is the entire script:

#Set environment
import arcpy
from arcpy import env
from arcpy.sa import *
wrkpath = r'E:\GIS_Data\GEO5419\MyWork'
arcpy.env.workspace = wrkpath
arcpy.env.overwriteOutput = True


def Cut():
    arcpy.CheckOutExtension("Spatial")
    topRaster = 'Site_DEM'
    outRasterCFS = wrkpath+'\CFSum'
    rasters = arcpy.ListRasters('cf_*')

   

    sumras = CellStatistics(rasters,"SUM","DATA")
    sumras.save(outRasterCFS)

##    i = 0
##    for ras in rasters:
##        if i == 0:
##            sumras = arcpy.Raster(ras)
##            i += 1
##        else:
##            sumras = sumras + ras
##            i += 1
##    sumras.save(outRasterCFS)

    arcpy.CheckInExtension("Spatial")

if __name__ == '__main__':
    Cut()

0 Kudos
1 Solution

Accepted Solutions
ChrissyWhite
New Contributor III

I figured it out not long after posting this. I wanted to delete this entire thread, but I cannot find an option to do so.


So, until I do, for anyone else having the same issue, the resolution was to add this to my code:

arcpy.env.extent = "MAXOF"

Now it is working fine.

View solution in original post

2 Replies
ChrissyWhite
New Contributor III

I figured it out not long after posting this. I wanted to delete this entire thread, but I cannot find an option to do so.


So, until I do, for anyone else having the same issue, the resolution was to add this to my code:

arcpy.env.extent = "MAXOF"

Now it is working fine.

XanderBakker
Esri Esteemed Contributor

I'm glad you resolved it by yourself,. Indeed using Cell statistics is the best way to sum multiple rasters.(since it can also handle NoData cells better). 

To give you a reason why your first snippet failed, this is most likely since you are trying to sum a raster object and a string. Probably it would be fixed if you include "arcpy.Raster()" on line 9 as shown below:

rasters = arcpy.ListRasters('cf_*')

i = 0
for ras in rasters:
    if i == 0:
        sumras = arcpy.Raster(ras)
        i += 1
    else:
        sumras = sumras + arcpy.Raster(ras)
        i += 1
sumras.save(outRasterCFS)

I marked your answer as the correct one. It is better to not delete the question, since other users may find the answer you found yourself helpful to solve their problems.