Con (Spatial Analyst)

Available with Spatial Analyst license.

Available with Image Analyst license.

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

  • If either the true raster or the optional false raster is floating point, the output raster will be floating point. If both the true expression and the optional false raster are integer, the output raster will be integer.

  • If the Input conditional raster (in_conditional_raster in Python) is a single-band raster and either the Input true raster or constant value (in_true_raster_or_constant In Python) raster or the optional Input false raster or constant value (in_false_raster_or_constant In Python) raster is a constant, the output will be a single-band raster.

  • If all inputs are multiband rasters, the output will be a multiband raster. The output raster will also be multiband if either the true input or the optional false input is a constant. The number of bands in each multiband input must be the same.

  • The tool will perform the operation on each band from the conditional raster using the corresponding band from the other inputs. If the conditional input is a multiband raster and the true or false raster input is a constant, the tool will perform the operation using the constant value for each band in the multiband input.

  • If the evaluation of the expression is nonzero, it is treated as true.

  • If no Input false raster or constant value is specified, NoData will be assigned to those cells that do not result in true from the expression.

  • If NoData does not satisfy the expression, it does not receive the value of the input false raster; it remains NoData.

  • The Expression uses an SQL query. See the following topics for details on creating queries:

  • In order to use a {where_clause} in Python, it should be enclosed in quotes. For example, "Value > 5000".

    You can consult the help for more information on specifying a query in Python.

  • In Python, you can avoid using a {where_clause} that specifies the Value field by using a Map Algebra expression as the in_conditional_raster instead.

    For example, the following expression:

    • Con("elev", 0, 1, "value > 1000")

    can be rewritten as follows:

    • Con(Raster("elev") > 1000, 0, 1)

    For more information, see the code samples listed below or review Build complex statements in Map Algebra.

  • The maximum length of the logical expression is 4,096 characters.

  • If at least one of the 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 any two inputs are multidimensional rasters and share one variable but with different names, uncheck the Match Multidimensional Variable geoprocessing environment (set arcpy.env.matchMultidimensionalVariable = False in Python) before running the tool.

    If the Input conditional raster value is a multidimensional raster and the Input true raster or constant value and Input false raster or constant value parameters are set to constant values, the tool will perform the operation for all slices for all variables using the constant values, and the output will be a multidimensional raster.

Parameters

LabelExplanationData Type
Input conditional raster

The input raster representing the true or false result of the desired condition.

It can be of integer or floating point type.

Raster Layer
Input true raster or constant value

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
Input false raster or constant value
(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
Expression
(Optional)

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

The Where clause follows the general form of an SQL expression. It can be entered directly, for example, VALUE > 100, if you click the Edit SQL mode button SQL Query. If in the Edit Clause Mode Edit Clause, you can begin constructing the expression by clicking on the Add Clause Mode button.

SQL Expression

Return Value

LabelExplanationData Type
Output raster

The output raster.

Raster

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

The 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. An example of a where_clause is "VALUE > 100".

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 when the input conditional raster value is greater than2000; the value will be NoData when 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 NoData, which will be replaced with 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 *
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, when the value of the input conditional raster is greater than or equal to 1500, the output value will be 1; when 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"

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