Checking for ANYNODATA in a DEM always results in True

302
3
Jump to solution
01-16-2020 03:06 AM
GISTeam4
Occasional Contributor

I'm working on a script that would fill (NoData) holes in a DEM with a raster calculation. The calculation fills the holes with the average of the surrounding cells and this proces should be repeated to get an accurate result. It works quite well, just the while loop never ends as arcpy.GetRasterProperties_management keeps finding ANYNODATA. Within the DEM itself there is no NoData, but on the outsidesthere is (outside of the extent).

The DEM I'm working with is cut (Extract by Mask) to a polygon which I've also set my extent to.

Could anyone point me into a direction how I can check for NoData and stop the While loop as soon as the DEM is filled?

My script looks like this:

import arcpy

# Environment settings
arcpy.env.overwriteOutput = True
arcpy.env.extent = "D:\..."
arcpy.env.parallelProcessingFactor = "8"
arcpy.env.processorType = "CPU"
DEM = arcpy.Raster("D:\...")

#	Check for NoData in the raster and start filling as long as there is NoData
NoDataCheck = arcpy.GetRasterProperties_management(in_raster=DEM, property_type="ANYNODATA")
NoDataRes = int(NoDataCheck.getOutput(0))
count = 0
print("Voor loop: " + str(NoDataRes))

while NoDataCheck:
	# RasterCalc filling the holes
	expression=' Con(IsNull(DEM), FocalStatistics(DEM, NbrRectangle(5,5, "CELL"), "MEAN"), DEM)'
	arcpy.gp.RasterCalculator_sa(expression, DEM)
	count = count + 1
	NoDataCheck = arcpy.GetRasterProperties_management(in_raster=DEM, property_type="ANYNODATA")
	NoDataRes = int(NoDataCheck.getOutput(0))
0 Kudos
1 Solution

Accepted Solutions
GISTeam4
Occasional Contributor

@Dan: You're right, but it's not the cause of the problem. I copied my code falsely and I already had that part in there (sorry for that). The correct script is in the main post now.

This is what's going on:

After some trial and error I've noticed the raster always result in a squared extend. This means a shape more complex than just 4 outer corners will contain NoData outside of the actual extent.

To make this visual, in the image below my extent is set to the black outlined polygon. The raster is masked to this shape. My script starts filling holes, but it will continue untill all the red (NoData) has been filled. So it always tries to work with a square extent.

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

You also need a nodata check within your while loop to check if all was removed during the loop.  

It will remain True, until it is checked again.

GISTeam4
Occasional Contributor

@Dan: You're right, but it's not the cause of the problem. I copied my code falsely and I already had that part in there (sorry for that). The correct script is in the main post now.

This is what's going on:

After some trial and error I've noticed the raster always result in a squared extend. This means a shape more complex than just 4 outer corners will contain NoData outside of the actual extent.

To make this visual, in the image below my extent is set to the black outlined polygon. The raster is masked to this shape. My script starts filling holes, but it will continue untill all the red (NoData) has been filled. So it always tries to work with a square extent.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

GIS Team‌ glad you posted the correct code now