[GIS] SetNull() failing with input raster in ArcPy

arcmaparcpyconditionalerror-010416spatial-analyst

In ArcGIS 10.3, I am trying to use SetNull to identify streams in a raster based on an existing flow accumulation raster. The process involves setting all raster cells with flow accumulation value of less than a specified threshold value to null, and those exceeding the threshold value are set to 1, indicating a stream is present. After successfully performing the operation of interest using the Raster Calculator tool with the following conditional statement SetNull("flowAccumulationRaster"<threshold,1), I have been trying to implement this in a Python script. I have determined through lots of searching that the Raster Calculator approach doesn't translate directly in Python, so I've been searching out other approaches.

This seems to be a straight forward problem, so I assume my syntax is incorrect somewhere. Again, I've been able to successfully perform this operation manually with the Raster Calculator, which indicates to me that the inputs are valid. Here is the code I've most recently tried:

import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")

threshold=2152000
inConRaster=flowAccum  # this is an existing raster in a GDB 
inFalseConst=0
whereClause="Value > threshold"

outSetNull=SetNull(inConRaster,inFalseConst,whereClause)
outSetNull.save(rasterCalcOut)

# I've also tried the following in place of SetNull with no success
# outSetNull=Con(flowAccum,1,"flowAccum > threshold")

I've tried numerous variations on the above two "SetNull" and "Con" commands, and continue to receive error messages related to the raster being either non-existent, not supported, or other related things.

Here is an error snippet:

Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
   outSetNull=SetNull(inConRaster,inFalseConst,whereClause)
  File "C:\Program Files    (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 331, in SetNull
where_clause)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\sa\Functions.py", line 326, in Wrapper
where_clause)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\geoprocessing\_base.py", line 504, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 010416: Error in setting raster table filter for C:\Users\Chris\Documents\Graduate School\UW Extension\GIS\Project\Data\MCWC_Process_Development_v02.gdb\YoungCreek_YaquinaRiver_07_FlowAccum_Int.
ERROR 010213: Error in reading raster Band_1.
ERROR 010325: Run-time error - ***.
ERROR 010067: Error in executing grid expression.
Failed to execute (SetNull).

Best Answer

Tool cannot understand condition. Try

whereClause= '"Value > %s"' %threshold

BTW it is not clear what you are trying to achieve. I guess Con can do it easier. Please expand your question

Flowacc= arcpy.Raster(path)

Con(Flowacc>threshold, 1)
Related Question