HOW TO

Automate setting null values of rasters to zero

Last Published: April 25, 2020

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))

Article ID: 000014695

Software:
  • ArcMap

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options