% (Modulo)

Summary

Finds the remainder (modulo) of the first raster when divided by the second raster on a cell-by-cell basis.

Illustration

Modulo illustration
OutRas = Raster("InRas1") % Raster("InRas2")

Discussion

The % (Modulo) operator is an overloaded operator since it exists in both Spatial Analyst and Python. To distinguish an input is a raster as opposed to a scalar variable the input raster is cast using the Raster class: Raster("inRas"). If both input are scalars, the Python % operator will be used working on scalars. If one or both of the input are rasters (identified by casting with the Raster class), the Spatial Analyst % is used and will work on each cell within the rasters.

The order of input is relevant for this operation.

Any value modulated (divided) by zero is assigned NoData on the output; therefore, any location on the second input that is zero or NoData will return NoData for that location on the output.

The modulo operator assumes both its inputs are integers. If any inputs are not integer, those inputs will be converted to integers through truncation. Output values are always integers.

Another way to perform the modulo operator is a %= b which is an alternative way to write a = a % b.

Syntax

in_raster_or_constant1 % in_raster_or_constant2
OperandExplanationData Type
in_raster_or_constant1

The numerator input.

If the first input is a raster and the second is a scalar, an output raster is created with the remainder being calculated for each input raster value after being divided by the scalar value.

Raster Layer | Constant
in_raster_or_constant2

The denominator input.

If the first input is a scalar and the second is a raster, an output raster is created with the remainder being calculated for each input raster value after being divided into the scalar value.

Raster Layer | Constant

Return Value

NameExplanationData Type
out_raster

The output raster object.

The cell values are the remainder of the division of the values in the first input by the second input.

Raster

Code Sample

% (Modulo) example 1 (Python window)

This sample returns the value of the remainder (modulus) of dividing the cells in the first raster by the second.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outMod = Raster("degs") % Raster("negs")
outMod.save("C:/sapyexamples/output/outmod.tif")
% (Modulo) example 2 (stand-alone script)

This sample returns the value of the remainder (modulus) of dividing the cells in the first raster by the second.

# Name: Op_Mod_Ex_02.py
# Description: Finds the remainder of the first raster when divided by
#              the second raster on a cell-by-cell basis
# Requirements: Spatial Analyst Extension
# Author: ESRI

# 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 = Raster("degs")
inRaster2 = Raster("negs")

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute Mod
outMod = inRaster1 % inRaster2

# Save the output 
outMod.save("C:/sapyexamples/output/outmod")

Environments

Related Topics


Published 6/7/2010