Raster calculator failed when using setnull function

7806
14
Jump to solution
06-12-2015 06:03 PM
Zheng_KiYip
New Contributor II

Hi Everyone,

I'm using the expression below to try convert all the values that are negative or equal to zero to NoData

SetNull("cov_fdg_ct" <= 0,"cov_fdg_ct")

But it failed to execute with the error message below:

Executing: RasterCalculator SetNull("cov_fdg_ct" <= 0,"cov_fdg_ct") "E:\GIS Data\Thesis modeling\FloodDepthGrids\cov_fdg_c"
Start Time: Fri Jun 12 17:49:33 2015
SetNull("cov_fdg_ct" <= 0,"cov_fdg_ct")
ERROR 000539: Error running expression: rcexec()
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
  File "<string>", line 5, in rcexec
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\sa\Functions.py", line 320, in SetNull
    where_clause)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\sa\Functions.py", line 309, in Wrapper
    ["SetNull", in_conditional_raster, in_false_raster_or_constant])
RuntimeError: ERROR 000732: Input Raster: Dataset cov_fdg_ct does not exist or is not supported

Failed to execute (RasterCalculator).
Failed at Fri Jun 12 17:49:34 2015 (Elapsed Time: 0.16 seconds)

I've been reading many posts from those who also got the ERROR 000539 and most of them seem to be due to a syntax error. However, I can't seem to figure out what syntax error is in my simple expression. It would be much appreciated if you can provide some advice!

Thank you!

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

​You should include spaces between fields, variable names, operators and values as a matter of course to remove/reduce the change that they may become concatenated.

On another note, you are tempting fate by working with rasters data particularly Esri grids, in a folder path that contains spaces...you may and continue to be 'luck' but when you aren't ... remember these words

Good luck

View solution in original post

14 Replies
DarrenWiens2
MVP Honored Contributor

The first argument is a raster, not a condition. You can use a where clause as the third parameter, though.

SetNull("cov_fdg_ct","cov_fdg_ct","Value <=0")

DanPatterson_Retired
MVP Emeritus

Darren is correct.
Always check the help files for syntax and bookmark the help page...for example SetNull syntax

0 Kudos
curtvprice
MVP Esteemed Contributor

Au contraire, gentlemen.

In the Raster Calculator the rules of arcpy map algebra apply so you can do this.

SetNull("cov_fdg_ct" <= 0,"cov_fdg_ct")

which is converted to the Python below which is then evaluated.

(The <= operator then gets handled by the Python parser to run the LessThanEqual tool, which resolves to the "truth" raster required by the SetNull tool. It's really quite awesome.)

from arcpy.sa import *
out = SetNull(Raster("cov_fdg_ct") <= 0, Raster("cov_fdg_ct"))

My guess is our friend Zheng is having an issue with "cov_fgd_ct" which is for some reason not resolving to a valid raster layer name or raster dataset path.

DanPatterson_Retired
MVP Emeritus

which could only mean that it was typed in manually into the raster calculator otherwise it wouldn't have appeared in the list of rasters .... hmmmm

0 Kudos
DanPatterson_Retired
MVP Emeritus

Interesting....

The Results of a test....  Raster in ArcMap 10.3.1 TOC,

Select ... ArcToolbox, Spatial Analyst Tools, Map Algebra...Raster Calculator

random raster appears as a selectable raster.

Enter the expression shown in the image below WITH a where clause, doesn't give an option for where, but what the hey...I put one in.  The results from the Results window

Messages

Executing: RasterCalculator SetNull("random","random","VALUE>0.5") F:\Test\x2

Start Time: Fri Jun 12 23:07:49 2015

SetNull(Raster(r"random"),Raster(r"random"),"VALUE>0.5")

Succeeded at Fri Jun 12 23:07:52 2015 (Elapsed Time: 3.25 seconds)

Conclusion....surmization...
The raster calculator whips in the Raster around the inputs ( I should have been more inventive) but uses the where clause

RasterCalculator1.png

Guess they should separate out raster calculator help or show some examples

EDIT

More inventive now to show what you can also do

SetNull("random",5,"VALUE>0.5)

Results

Messages
Executing: RasterCalculator SetNull("random",5,"VALUE>0.5") F:\Test\x3
Start Time: Fri Jun 12 23:22:30 2015
SetNull(Raster(r"random"),5,"VALUE>0.5")
Succeeded at Fri Jun 12 23:22:34 2015 (Elapsed Time: 4.12 seconds)

Fun with the raster calculator

curtvprice
MVP Esteemed Contributor

I guess in this particular case it doesn't work exactly like I said, but the result is the same.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I just fine it a little insistent or odd that Raster is whipped around the layer name but NOT the 5 in the Results window output.  Why would a layer whose path is know to ArcMap need the raster

Now wrap your head around this one...reading a file from disk...using a layer in arcmap and a where clause

SetNull("F:/test/x","random","VALUE<0.25")

Messages

Executing: RasterCalculator SetNull("F:/test/x","random","VALUE<0.25") C:\Users\Dan\Documents\ArcGIS\Default.gdb\rastercalc

Start Time: Fri Jun 12 23:36:08 2015

SetNull(Raster(r"F:/test/x"),Raster(r"random"),"VALUE<0.25")

Succeeded at Fri Jun 12 23:36:11 2015 (Elapsed Time: 3.27 seconds)

hmmmm....got to love it

0 Kudos
curtvprice
MVP Esteemed Contributor

> [why] Raster is whipped around the layer name but NOT the 5 in the Results window output.

This is because the SetNull tool can take a straight out integer as an argument, it doesn't need to be a raster layer, raster dataset, or raster object -- no need to wrap in in Raster().

> Why would a layer whose path is know to ArcMap need the raster

I don't believe  the F:/test/x needs to be put inside Raster() to work in arcpy -- my guess is whenever the Raster Calculator tools's parser finds a string in that context, it assumes it must be a raster name and wraps Raster() around it - so if you put in "raster" + 5 it will be changed to Raster("raster") + 5 which is valid. (The plus operator can operate on a raster and an integer, but not a string and an integer.)

0 Kudos
DanPatterson_Retired
MVP Emeritus

Sigh...I know these things...you know these things...but try to explain the subtle differences to someone...

for example... where classes:

(1)  Table Select for an integer raster

        TableSelect x F:\Test\t ""VALUE" > 5"   double quotes around the whole thing, plus the field name

(2)   SetNull for the same raster

         SetNull("x","x","VALUE>5") F:\Test\x2 double quotes around the whole thing NOT the field name

We can explain these things away ... but try explaining these subtleties to introductory students.  I have lots of examples where "things" could be standardized whether they need to be or not