Extract by Attributes (Spatial Analyst)
Summary
Extracts the cells of a raster based on a logical query.
Illustration
Usage
-
If the Where clause evaluates to true, the original input value is returned for the cell location. If it evaluates to false, the cell location is assigned NoData.
-
Any extra items (other than Value and Count) of the input raster are dropped for the output raster.
-
If an item other than Value of input raster is specified in the Where clause, the original input value is returned for the cell location.
-
When a multiband raster is specified as input, a new multiband raster will be created as output. Each individual band in the input multiband raster will be analyzed accordingly.
The default output format is an Esri Grid stack. Note that the name of an Esri Grid stack cannot start with a number, use spaces, or be more than 9 characters in length.
-
If the input is a layer created from a multiband raster with more than three bands, the extraction operation will only consider the bands that were loaded (symbolized) by the layer. As a result, the output multiband raster can only have three bands, corresponding to those used in the display of the input layer.
-
If the input raster is integer, the output raster will be integer. If the input is floating point, the output will be floating point.
Syntax
Parameter | Explanation | Data Type |
in_raster |
The input raster from which cells will be extracted. | Raster Layer |
where_clause | A logical expression that selects a subset of raster cells. 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
Name | Explanation | Data Type |
out_raster |
The output raster containing the cell values extracted from the input raster. | Raster |
Code Sample
This example extracts cells from a raster based on a logical query, where elevation is greater than 1,000 meters.
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
attExtract = ExtractByAttributes("elevation", "VALUE > 1000")
attExtract.save("c:/sapyexamples/output/attextract")
This example extracts cells from a raster based on a logical query, where elevation is greater than 1,000 meters.
# Name: ExtractByAttributes_Ex_02.py
# Description: Extracts the cells of a raster based on a logical query.
# 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 = "elevation"
inSQLClause = "VALUE > 1000"
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute ExtractByAttributes
attExtract = ExtractByAttributes(inRaster, inSQLClause)
# Save the output
attExtract.save("c:/sapyexamples/output/attextract02")