Con (Image 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.

Parameters

LabelExplanationData Type
Input 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
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

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.ia import *
env.workspace = "C:/output/data"
outCon = Con("elevation", "elevation", "", "VALUE > 2000")
outCon.save("C:/output/outcon.img")

# Execute Con using a map algebra expression instead of a where clause
outCon2 = Con(Raster("elevation") > 2000, "elevation")
outCon2.save("C:/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.

# Name: Con_Ex_02.py
# Description: Performs a conditional if/else evaluation 
#              on each pixel of an input raster.
# Requirements: Image Analyst Extension

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

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

# Set local variables
inRaster = Raster("elevation")
inTrueRaster = 1
inFalseConstant = 0
whereClause = "VALUE >= 1500"

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

# Execute Con and save the output
outCon = Con(inRaster, inTrueRaster, inFalseConstant, whereClause)
outCon.save("C:/output/outcon.tif")

# Execute Con using a map algebra expression instead of a where clause, and save the output.
outCon2 = Con(inRaster >= 1500, inTrueRaster, inFalseConstant)
outCon2.save("C:/output/outcon2.tif")

Licensing information

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

Related topics