GIS Logo GSP 118 (318): GIS Programming

Raster Math

In Review

Introduction

The functions below perform "math" on rasters. In other words, if you muitple two rasters together, each pixel will be the result of multiplying the pixel from the first raster that is in the same location as the resulting pixel with a pixel in the second rasters that is also in the same spatial location.

You can pass in a file path, name of a raster in the workspace (after workspace is set), or a current raster for the input raster in these functions. The output raster can then be saved to a file as:

OutRaster=arcpy.sa.Plus("C:/Temp/FirstRaster.img","C:/Temp/SecondRaster.img")
OutRaster.save("C:/Temp/Result.img")

Raster Math Functions

Name Description
Arithmatic  
arcpy.sa.Plus(InRasterOrConstant,InRasterOrContant) Add the two rasters together
arcpy.sa.Minus(InRasterOrConstant,InRasterOrContant) Subtract the second raster from the first
arcpy.sa.Times(InRasterOrConstant,InRasterOrConstant) Mulitply the first raster by the second
arcpy.sa.Divide(InRasterOrConstant,InRasterOrConstant) Divide the first raster by the second
arcpy.sa.FloatDivide(InRasterOrConstant,InRasterOrContant) Divides the first raster by the second (not sure when this is needed, for floating-point rasters?)

Conditional Operations

These functions return a boolean raster based on a comparison between two other rasters.

Name Description
arcpy.sa.EqualTo(InRasterOrContsant,InRasterOrConstant) Returns a raster with pixels set to 1
arcpy.sa.GreaterThan(InRasterOrContsant,InRasterOrConstant)  
arcpy.sa.GreaterThanEqual(InRasterOrContsant,InRasterOrConstant)  
arcpy.sa.LessThan(InRasterOrContsant,InRasterOrConstant)  
arcpy.sa.LessThanEqual(InRasterOrContsant,InRasterOrConstant)  

Boolean Operations

These operations work with rasters that have pixels set to 0 ("false") or 1 ("true"). These are also known as "bit masks" or "boolean rasters" and can be used to manipulate areas within a raster.

Name Description
arcpy.sa.BooleanAnd(RasterOrConstant,RasterOrConstant)  
arcpy.sa.BooleanNot(InRaster)  
arcpy.sa.BooleanOr(RasterOrConstant,RasterOrConstant) Performs a boolean "OR" on the two rasters
arcpy.sa.Con(ConditiontalRaster,InTrueRaster,InFalseRaster) Performance a conditional operation. The resulting raster will have values from the "true" raster where the pixels in the ConditionalRaster are true and values from the "false" raster where pixels in the Conditional raster are false.