English

How To: Automate setting null values of rasters to zero

Summary

Instructions provided describe how to assign the NoData cells of rasters to zero values.

Various spatial analysis applications require raster NoData cells to be factored into an analytical operation. Since NoData cells are ignored in any analysis, such requirements can only be met if those cells have a specific value assigned to them.

The following script demonstrates how to assign NoData cells of rasters in a workspace to zero values using map algebra.

Note:
This procedure assumes that the input raster data is in Esri Grid format. See the Related Information section below.

Procedure

#import modules
import arcpy, sys, os
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("spatial")
env.workspace = arcpy.GetParameterAsText(0)

#Create raster directory
ras_dir = "{0}/ras_dir".format(env.workspace)

if not os.path.exists(ras_dir):
	os.mkdir(ras_dir)

rasterList = arcpy.ListRasters("*") #, "GRID")

#Set raster null values to 0
for raster in rasterList:

	arcpy.AddMessage("Reading {0}".format(raster))
	outras = Con(IsNull(raster),0,raster)
	arcpy.AddMessage("Setting null for {0}".format(raster))
	outras.save("{0}/{1}".format(ras_dir, raster))
	arcpy.AddMessage("Saving {0}".format(raster))

Related Information