Python syntax error in Raster Calculator, "Not" or "!" ?

4142
7
11-30-2015 07:44 AM
johnpolo
New Contributor III

I want to erase part of an existing raster I call ecoregion. I saw these instructions in an old forum post​ that tell how to do what I want:

"

Convert the polygon to a raster and use IsNull/SetNull in the Raster Calculator, as in 

SetNull( Not IsNull( [PolyRaster] ), [MyRaster] )

"

I wrote in Raster Calculator

SetNull( Not IsNull("water3a"), "Ecoregion.tif")

and I get an error message

"Python syntax error: Parsing error

SyntaxError: invalid syntax (line 1)"

I don't know what is causing this error, because I don't know Python. I tried replacing "Not" with "!", but that didn't change the error. If I leave out the "Not", the command runs, but it doesn't give me the results I want, which is no surprise. I was just checking if maybe the problem had something to do with the "Not", which maybe it does. I guess there is a Python command I am supposed to use instead of "Not"?

Tags (4)
0 Kudos
7 Replies
IanMurray
Frequent Contributor

Instead of using the word Not or the != expressionhave you tried using the Boolean Not (~).  I didn't use the old raster calculator, but I think that might be the correct operator to replace the Not operator.

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

SetNull(~ IsNull("water3a"), "Ecoregion.tif")

johnpolo
New Contributor III

Thanks for the reply, Ian, but that didn't work for me.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I believe "Not IsNull(the raster)" should be equivalent to "the raster" (although I'm open to reasons why it's not):

SetNull( "water3a", "Ecoregion.tif")
0 Kudos
johnpolo
New Contributor III

Thanks, Darren, I would agree with your reasoning, but when I tried that in raster calculator, the results were not what I wanted. The results were a few oddly shaped and placed slivers that remained.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Are you sure it isn't that SetNull is changing both nulls and zeroes to null (it will)? Also, have you set the processing extent environment to match Ecoregion.tif?

johnpolo
New Contributor III

I think you're right, Darren. And I realized part of my problem. My raster for water appears to not be aligned properly with the ecoregion raster. (I don't understand that, because they all started from the same grid.) So it's overlapping in weird places, causing the weird pieces. But it's not erasing the one main settlement, which is the point of this. I included an image of the whole ecoregion layer. The red arrows point to a town and the water. The town is the upper left arrow. The next image is the "water3a" layer and the  last image is what remains after running raster calculator.

dr.jpgdr2.jpgdr3.jpg

0 Kudos
IanMurray
Frequent Contributor

I thought about that a bit and not isn't equal to the raster, if the raster has null values in it.  If you have say a landsat scene that has a bunch of null values in it for the scene edge, but that wasn't part of the area you wanted set null in the raster you were applying it to.  You would only want to set null values to the existing raster where the raster you are using has existing values, not anywhere that is just extraneous null values to complete the whole grid.