Available with Spatial Analyst license.
Summary
Performs a conditional if/else evaluation on each of the input cells of an input raster.
Illustration
Usage
If either the true raster or optional false raster is floating point, the output raster will be floating point. If both the true expression and optional false raster are integer, the output raster will be integer.
If the evaluation of the expression is nonzero, it is treated as True.
If no input false raster or constant 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 more details on creating queries in the Query Builder:
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} which specifies the Value field by instead using a Map Algebra expression as the in_conditional_raster.
For example, the following expression:
- Con("elev", 0, 1, "value > 1000")
can be rewritten as:
- Con(Raster("elev") > 1000, 0, 1)
For more information, see the code samples listed below, or review Building complex statements in Map Algebra.
The maximum length of the logical expression is 4,096 characters.
See Analysis environments and Spatial Analyst for additional details on the geoprocessing environments that apply to this tool.
Syntax
Con (in_conditional_raster, in_true_raster_or_constant, {in_false_raster_or_constant}, {where_clause})
Parameter | Explanation | Data 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
Name | Explanation | Data 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
Licensing information
- ArcGIS Desktop Basic: Requires Spatial Analyst
- ArcGIS Desktop Standard: Requires Spatial Analyst
- ArcGIS Desktop Advanced: Requires Spatial Analyst