0

I've got this process I'm trying to automate that essentially iterates across a set of rasters, runs a con statement on them to put values from a separate raster wherever the first raster has certain values, save the max value of that new raster in a dictionary and finally save the new raster to disk.

Here's a reprex, then I'll explain some issues:

import arcpy, os
from arcpy import env
from arcpy.sa import *

path = r"C:\GIS\CMP\test"
outName = 'test.gdb'
riskMax = {}

arcpy.env.workspace = path
arcpy.env.addOutputsToMap = 0
print("Creating test.tif")
test = arcpy.CreateRandomRaster_management(path, 'test.tif', 'NORMAL 3.0', '0 0 500 500', 50)

print("Creating file geodatabase")
arcpy.CreateFileGDB_management(path, outName)

gdb = os.path.join(path, outName)
arcpy.env.workspace = gdb

for i in range(10):
    name = 'rand' + str(i)
    print("Creating " + name)
    arcpy.CreateRandomRaster_management(gdb, name, 'NORMAL 3.0', '0 0 500 500', 50)

try:
    for ras in arcpy.ListRasters():
        print('Running Con')
        riskProd = Con(Raster(ras) > 0, test)
        print('Storing max value')
        riskMax[ras] = riskProd.maximum
        outName = os.path.join(gdb, "{0}_{1}".format('out', ras))
        print("Saving " + outName)
        riskProd.save(outName)
except Exception:
    e = sys.exc_info()[1]
    print(e.args[0])
    arcpy.AddError(e.args[0])

So if you run this in the python window, you should get the ERROR 000732 mentioned in the title specifically for the SECOND rand raster. So it makes it through the first iteration of the for loop in the try block, but it fails on the Con() statement the second time through. I've identified two workarounds for this problem, but I want to understand why it is happening.

First, if you set

arcpy.env.addOutputsToMap = 1

this prevents the error. It completes the loop just fine and creates all the out_rand rasters successfully.

The other option, which is weirder to me, is that AFTER the Con() call, if you just save riskProd to the dictionary instead of riskProd.maximum, that works as well. Then you can just call

Raster(riskMax[ras]).maximum 

later to retrieve the max value.

Can someone explain what is causing this error?

Why can I not Con() a raster if it hasn't been displayed?

I want to be able to prevent adding outputs to the map, and it's cleaner (and uses less memory) to save the max value in a dictionary, rather than saving the entire rasters and extracting attributes later.

Note: in other tests, rasters that have been displayed, but are not currently in the table of contents still pass Con() just fine and it will fail on the first raster it encounters that has not been displayed yet. I had mixed results with calculating pyramids for a raster that hasn't been displayed. Sometimes it helped, sometimes it didn't.

  • Are you able to run this in a separate Python window (not within ArcGIS itself) without error? – smiller Aug 30 at 18:38
  • I've never tried to do that, but I'm working on it now. It's having issues getting the license for SA, so I'll report back when I resolve that. – ffollett Aug 30 at 19:39
  • Once I added the code to check out the needed license, it runs fine externally. – ffollett Aug 30 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.