why my IsNull function is not working???

1220
3
11-16-2011 11:05 PM
martinmaretta
Occasional Contributor
hi i have this piece of code
import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True

# Get input parameters
inDEM = arcpy.GetParameterAsText(0)
inStream = arcpy.GetParameterAsText(1)
inMinSlope = float(arcpy.GetParameterAsText(2))
outSlope = arcpy.GetParameterAsText(3)


pDEM = Raster(inDEM)


arcpy.env.snapRaster =  inDEM
arcpy.env.extent = inDEM
arcpy.env.mask = inStream # mask for Stream only
arcpy.env.cellSize = inDEM

pSlopeStr = Slope(pDEM,"PERCENT_RISE")
arcpy.env.mask = inDEM # mask for whole DEM
##
pSlopeDEM = Slope(pDEM,"PERCENT_RISE")
pIsNull = IsNull(pSlopeStr)   #****ERROR
print(str(pIsNull.format))
##pSlope = Con(pIsNull==1,pSlopeDEM,pSlopeStr)
##pSlopeOK = Con(pSlope < inMinSlope,inMinSlope,pSlope)
pIsNull.save('C:\\aaaa4')
pSlopeStr.save('C:\\aaaa5')

##del pSlopeStr
##del pSlopeDEM
##del pSlopeOK


i use IsNull function whit my temporary raster, but result is NOTHING (NULL). When I use nontemporary raster everything is OK.. do anobody know where is a problem??? thanks a lot
Tags (2)
0 Kudos
3 Replies
NathanStrout
New Contributor II
I'm having the same issue. Anytime I use the IsNull function, I get a Raster type result back (so no error is thrown) but if I try to save that or use it in any way, I get an ERROR 999998: Unexpected Error.
I'm having to do a bunch of workarounds to deal with this issue so any help would be greatly appreciated!

[using ArcGIS 10 SP3]
0 Kudos
NathanStrout
New Contributor II
This post made me go back to my code and think about this issue again and I think I've come up with a simple solution that works. I've gotten IsNull to work in the past so I think the problem is actually caused because I'm casting the output path of a PolygonToRaster function to a raster by simply doing polyRaster = Raster(outputPath) - which should work and generally does for casting path strings to Raster objects.
If I instead make a raster layer using MakeRasterLayer_management and use that layer name as the input to my subsequent operations (Con in this case), it works without issue.
So my code went from this:
arcpy.PolygonToRaster_conversion(inPoly,valueField,outputPath,"CELL_CENTER","#",arcpy.env.cellSize)
polyRaster = Raster(outputPath)
output = Con(IsNull(polyRaster),0,polyRaster) ##Returns 999998: Unexepected Error


to this:

arcpy.PolygonToRaster_conversion(inPoly,valueField,outputPath,"CELL_CENTER","#",arcpy.env.cellSize)
arcpy.MakeRasterLayer_management(outputPath,"polyRaster")
output = Con(IsNull("polyRaster"),0,"polyRaster")


So the solution works for me but there must be a bug in the type casting or something...
0 Kudos
MichalisAvraam
New Contributor III
This post made me go back to my code and think about this issue again and I think I've come up with a simple solution that works. I've gotten IsNull to work in the past so I think the problem is actually caused because I'm casting the output path of a PolygonToRaster function to a raster by simply doing polyRaster = Raster(outputPath) - which should work and generally does for casting path strings to Raster objects.
If I instead make a raster layer using MakeRasterLayer_management and use that layer name as the input to my subsequent operations (Con in this case), it works without issue.
So my code went from this:
arcpy.PolygonToRaster_conversion(inPoly,valueField,outputPath,"CELL_CENTER","#",arcpy.env.cellSize)
polyRaster = Raster(outputPath)
output = Con(IsNull(polyRaster),0,polyRaster) ##Returns 999998: Unexepected Error


to this:

arcpy.PolygonToRaster_conversion(inPoly,valueField,outputPath,"CELL_CENTER","#",arcpy.env.cellSize)
arcpy.MakeRasterLayer_management(outputPath,"polyRaster")
output = Con(IsNull("polyRaster"),0,"polyRaster")


So the solution works for me but there must be a bug in the type casting or something...


I tried your solution too, to no avail. I get weird errors:
RuntimeError: ERROR 000732: Input Raster: Dataset d:\Users\MICHAL~1.THI\AppData\Local\Temp\x923a0602_2133_4e68_bd44_b285094f1537y0.afr does not exist or is not supported


The referenced input raster was generated using MakeRasterLayer by the way. I am not sure what the problem is. What I realized is that Geoprocessing of raster data in Python is abysmal. My work should be straight forward but I encounter a lot of cryptic messages (Errors 999998 and 999999). My workflow is simple really, but if you have any suggestions...

I receive daily locations of monitors that where put in place somewhere.
I know the monitor has a radius of 5km.
For each monitor then, I use the wonderful ExtractByCircle on a constant raster of value 1 to get a circular buffer around the monitor (this returns an instance of Raster class) (NOTE: This does not work with vector as DISSOLVE refuses to work with more than 2 overlapping polygons, and ESRI tech support just said find a workaround).
This is then meant to be compared with the historical coverage (i.e. did we add pi*5^2 area or was there an overlap?). A simple conditional statement achieves this (if the result of extractbycircle is null, get data from the old coverage, else grab data from our extractbycircle), along with a comparison of the new and old coverage.

Now, this process works just fine when performed by hand in ArcMap. Run the extract, run the Con, do simple math to figure out change, record it. In Python, this seems like a lost cause. If you use the Con() command, the returned value is of type Raster, but does not allow you to save it or find the maximum or minimum values (Error 999998), which should be what the Raster class provides (trying to save it to verify it is valid does not work).

Any suggestions?
0 Kudos