0

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).
1
  • 1
    Try saving your flowAccum raster to a path with no spaces and a shorter filename.
    – user2856
    Commented Apr 11, 2016 at 3:26

2 Answers 2

3

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)
0

As with the Con tool, you can accomplish this using Reclassify, which is usually what I do (which doesn't make it the right way).

rast = arcpy.Raster(ef)
rast_fill = arcpy.sa.Fill(rast)
rast_direc = arcpy.sa.FlowDirection(rast_fill)
rast_accum = arcpy.sa.FlowAccumulation(rast_direc)
minr = da_rast.minimum-1
maxr = da_rast.maximum+1
remapRangeValues = arcpy.sa.RemapRange([[minr,threshold-.00001,0],[threshold,maxr,1],["NODATA","NODATA",0]])
outReclass = arcpy.sa.Reclassify(da_rast, "Value", remapRangeValues)

You could probably change the 0 on the low end to NODATA to achieve the SetNull.

Then if you want the threshold areas:

arcpy.RasterToPolygon_conversion(outReclass,somepathtofeatureclass,"NO_SIMPLIFY","VALUE")

See the comment below for stream centerlines.

4
  • Where is flow direction in your answer? Never heard of polygon representation of the streams derived from flow accumulation. Stream to feature is correct tool and it produces line network
    – FelixIP
    Commented Apr 11, 2016 at 20:54
  • Flow direction isn't in this at all. To clarify, I have a flow accumulation raster result. Using a specified threshold value, I want to set all cells that are below the threshold value to null or no data. Those cells that are above the threshold maintain their value (flow accumulation), and thus meet the specified definition of a stream, based strictly on the assumption in the threshold value. After this, I use stream to feature to create a stream network line. Sorry of that wasn't clear to begin with, I see that part of what I wrote got cut off somehow. Commented Apr 12, 2016 at 4:48
  • @ChrisMeder Sorry, I accidentally deleted the flow direction line when I pasted the code. I've edited to include it. Then yes, doing a reclassify won't work for your purposes. FelixIP's con function will work. If a value is false it will return a NoData for those values. outCon = Con(flowAccum, flowAccum,"", "VALUE > %s"%(threshold)) The first python example in this help page seems to achieve what you want help.arcgis.com/En/Arcgisdesktop/10.0/Help/index.html#//…
    – dslamb
    Commented Apr 12, 2016 at 12:52
  • @FelixIP and dslamb - Thank you for the rapid responses. I was able to get things working. Seems like the key was fixing the specification of the where clause. I hadn't recognized the need for the %s usage. Much appreciated! Commented Apr 15, 2016 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.