Con (Spatial Analyst)

License Level:BasicStandardAdvanced

Summary

Performs a conditional if/else evaluation on each of the input cells of an input raster.

Learn more about performing conditional evaluation with Con

Illustration

Con illustration
OutRas = Con(InRas1, 40, 30, "Value >= 2")

Usage

Syntax

Con (in_conditional_raster, in_true_raster_or_constant, {in_false_raster_or_constant}, {where_clause})
ParameterExplanationData Type
in_conditional_raster

Input raster representing the true or false result of the desired condition.

It can be of integer or floating point type.

Raster Layer
in_true_raster_or_constant

The input whose values will be used as the output cell values if the condition is true.

It can be an integer or a floating point raster, or a constant value.

Raster Layer | Constant
in_false_raster_or_constant
(Optional)

The input whose values will be used as the output cell values if the condition is false.

It can be an integer or a floating point raster, or a constant value.

Raster Layer | Constant
where_clause
(Optional)

A logical expression that determines which of the input cells are to be true or false.

The expression follows the general form of an SQL expression.

Consult the documentation for more information on the SQL reference for query expressions used in ArcGIS and specifying a query in Python.

SQL Expression

Return Value

NameExplanationData Type
out_raster

The output raster.

Raster

Code Sample

Con example 1 (Python window)

In this example the original value will be retained in the output where the input conditional raster is greater than a value of 2000, and a value of NoData where it is not.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outCon = Con("elevation", "elevation", "", "VALUE > 2000")
outCon.save("C:/sapyexamples/output/outcon.img")

# Execute Con using a map algebra expression instead of a where clause
outCon2 = Con(Raster("elevation") > 2000, "elevation")
outCon2.save("C:/sapyexamples/output/outcon2")
Con example 2 (Python window)

In this example the original value will be retained in the output except for Nodata, which will be replaced with the value of 0.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outCon = Con(IsNull("elevation"),0, "elevation")
outCon.save("C:/sapyexamples/output/outcon")
Con example 3 (Python window)

In this example two different rasters are used to create the conditional raster.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
inRaster1 = Raster("landuse")
inRaster2 = Raster("landuse2")
outCon = Con(((inRaster1 == 1) & (inRaster2 == 5)), inRaster1 + inRaster2, 99)
outCon.save("C:/sapyexamples/output/outcon")
Con example 4 (Python window)

In this example multiple Con tools are used inside a Con.

import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension = "Spatial"
env.workspace = "C:/sapyexamples/data"
inRas1 = Raster("inRaster")
outCon = Con(inRas1 < 45,1, Con((inRas1 >= 45) & (inRas1 < 47),2, Con((inRas1 >= 47) & (inRas1 < 49),3, Con(inRas1 >= 49,4))))
outCon.save("C:/sapyexamples/output/outcon")
Con example 5 (stand-alone script)

In this example, where the value of the input conditional raster is greater than or equal to 1500, the output value will be 1, and where it is less than 1500, the output value will be 0.

# Name: Con_Ex_02.py
# Description: Performs a conditional if/else evaluation 
#              on each cell of an input raster.
# 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
inRaster = Raster("elevation")
inTrueRaster = 1
inFalseConstant = 0
whereClause = "VALUE >= 1500"

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

# Execute Con
outCon = Con(inRaster, inTrueRaster, inFalseConstant, whereClause)

# Execute Con using a map algebra expression instead of a where clause
outCon2 = Con(inRaster >= 1500, inTrueRaster, inFalseConstant)

# Save the outputs 
outCon.save("C:/sapyexamples/output/outcon")
outCon2.save("C:/sapyexamples/output/outcon2")

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Requires Spatial Analyst
ArcGIS for Desktop Standard: Requires Spatial Analyst
ArcGIS for Desktop Advanced: Requires Spatial Analyst
4/10/2014