# Con (Spatial Analyst)

## Summary

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

## 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.

• See Analysis environments and Spatial Analyst for additional details on the geoprocessing environments that apply to this tool.

## Parameters

 Label Explanation Data 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 . If in the Edit Clause Mode , you can begin constructing the expression by clicking on the Add Clause Mode button. SQL Expression

### Return Value

 Label Explanation Data Type Output raster The output raster. Raster

`Con(in_conditional_raster, in_true_raster_or_constant, {in_false_raster_or_constant}, {where_clause})`
 Name 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 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")``````

## Licensing information

• Basic: Requires Spatial Analyst or Image Analyst
• Standard: Requires Spatial Analyst or Image Analyst
• Advanced: Requires Spatial Analyst or Image Analyst
{"ft_lang_label":"__label__en","ft_lang_prob":0.5753191,"math_prob":0.95670044,"size":1123,"snap":"2022-27-2022-33","text_gpt3_token_len":242,"char_repetition_ratio":0.14030384,"word_repetition_ratio":0.2631579,"special_character_ratio":0.20658949,"punctuation_ratio":0.09047619,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.994426,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,10,null,10,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-30T04:19:35Z\",\"WARC-Record-ID\":\"<urn:uuid:f99013f7-9370-4947-9d0f-2e301dec74f8>\",\"Content-Length\":\"34306\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:666dc786-af13-430d-826d-5ff80553baeb>\",\"WARC-Concurrent-To\":\"<urn:uuid:a0b538ae-a9a8-485d-b17b-5a57a7670cdf>\",\"WARC-IP-Address\":\"198.102.60.60\",\"WARC-Target-URI\":\"https://pro.arcgis.com/en/pro-app/latest/tool-reference/spatial-analyst/con-.htm\",\"WARC-Payload-Digest\":\"sha1:OQIU7VNLTSLSGIHX6T4HGIJDMUHCFZLK\",\"WARC-Block-Digest\":\"sha1:KOC3F7RRIUYVYSC4YD7LTJ65CSRHEFA7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103661137.41_warc_CC-MAIN-20220630031950-20220630061950-00109.warc.gz\"}"}