Diff (Spatial Analyst)

Available with Spatial Analyst license.

Available with Image Analyst license.

Summary

Determines which values from the first input are logically different from the values of the second input on a cell-by-cell basis.

If the values on the two inputs are different, the value on the first input is output. If the values on the two inputs are the same, the output is 0.

Illustration

Diff illustration
OutRas = Diff(InRas1, InRas2)

Usage

  • Two inputs are necessary for this logical evaluation to take place.

  • The order of inputs is relevant for this tool.

  • If both inputs are integer, the output will be an integer raster; otherwise, it will be a floating-point raster.

  • If both inputs are single-band rasters or one of the inputs is a constant, the output will be a single-band raster.

  • If both inputs are multiband rasters, the tool will perform the operation on each band from one input, and the output will be a multiband raster. The number of bands in each multiband input must be the same.

  • If one of the inputs is a multiband raster and the other input is a constant, the tool will perform the operation using the constant value for each band in the multiband input, and the output will be a multiband raster.

  • If both inputs are multidimensional raster data with the same number of variables, the tool will perform the operation for all slices with the same dimension value. The output will be a multidimensional raster in CRF format. The variables in the inputs must have at least one common dimension and one common dimensional value for this tool to process; otherwise, an error will occur.

    If both inputs have one variable but different names, uncheck the Match Multidimensional Variable geoprocessing environment (set arcpy.env.matchMultidimensionalVariable = False in Python) before running the tool.

    If one of the inputs is a multidimensional raster and the other input is a constant, the tool will perform the operation for all slices for all variables using the constant value, and the output will be a multidimensional raster.

  • See Analysis environments and Spatial Analyst for additional details on the geoprocessing environments that apply to this tool.

Parameters

LabelExplanationData Type
Input raster or constant value 1

The input to which the second input will be compared.

A number can be used as an input for this parameter, provided a raster is specified for the other parameter. To specify a number for both inputs, the cell size and extent must first be set in the environment.

Raster Layer; Constant
Input raster or constant value 2

The input to which the first input will be compared.

A number can be used as an input for this parameter, provided a raster is specified for the other parameter. To specify a number for both inputs, the cell size and extent must first be set in the environment.

Raster Layer; Constant

Return Value

LabelExplanationData Type
Output raster

The output raster.

The values in the output will be either 0 if the two input values are the same, or the first input value if they are not.

Raster

Diff(in_raster_or_constant1, in_raster_or_constant2)
NameExplanationData Type
in_raster_or_constant1

The input to which the second input will be compared.

A number can be used as an input for this parameter, provided a raster is specified for the other parameter. To specify a number for both inputs, the cell size and extent must first be set in the environment.

Raster Layer; Constant
in_raster_or_constant2

The input to which the first input will be compared.

A number can be used as an input for this parameter, provided a raster is specified for the other parameter. To specify a number for both inputs, the cell size and extent must first be set in the environment.

Raster Layer; Constant

Return Value

NameExplanationData Type
out_raster

The output raster.

The values in the output will be either 0 if the two input values are the same, or the first input value if they are not.

Raster

Code sample

Diff example 1 (Python window)

This example performs a difference operation on two Grid rasters and outputs the result as an IMG raster.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outDiff = Diff("degs", "negs")
outDiff.save("C:/sapyexamples/output/outdiff.img")
Diff example 2 (stand-alone script)

This example performs a difference operation on two Grid rasters.

# Name: Diff_Ex_02.py
# Description: Determines which values from the first input are
#              logically different from the values of the second input
#              on a cell-by-cell basis within the Analysis window
# Requirements: Spatial Analyst Extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "C:/sapyexamples/data"

# Set local variables
inRaster1 = "degs"
inRaster2 = "negs"

# Execute Diff
outDiff = Diff(inRaster1, inRaster2)

# Save the output 
outDiff.save("C:/sapyexamples/output/outdiff")

Licensing information

  • Basic: Requires Spatial Analyst or Image Analyst
  • Standard: Requires Spatial Analyst or Image Analyst
  • Advanced: Requires Spatial Analyst or Image Analyst

Related topics