Extract by Mask (Spatial Analyst)
Summary
Extracts the cells of a raster that correspond to the areas defined by a mask.
Illustration
Usage
-
The Extract by Mask tool is similar to setting the Mask environment, except that the input mask is only used on the immediate instance, while a mask set in the environment is applied to all tools until it is changed or disabled.
-
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 mask is raster, the values for non-NoData input cell locations are copied to the output raster. Tools that can create the mask raster include Con, Test, and other tools in the extraction toolset.
-
When a multiband raster is specified for the input raster mask, only the first band will be used in the operation.
-
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 |
in_mask_data |
Input mask data defining areas to extract. This is a raster or feature dataset. When the input mask data is a raster, NoData cells on the mask will be assigned NoData values on the output raster. | Raster Layer | Feature Layer |
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 within a mask defined by an input polygon shapefile feature class.
import arcpy from arcpy import env from arcpy.sa import * env.workspace = "C:/sapyexamples/data" outExtractByMask = ExtractByMask("elevation", "mask.shp") outExtractByMask.save("C:/sapyexamples/output/maskextract")
This example extracts cells from a raster within a mask defined by an input polygon shapefile feature class.
# Name: ExtractByMask_Ex_02.py # Description: Extracts the cells of a raster that correspond with the areas # defined by a mask. # Requirements: Spatial Analyst Extension # Author: ESRI # 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" inMaskData = "mask.shp" # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Execute ExtractByMask outExtractByMask = ExtractByMask(inRaster, inMaskData) # Save the output outExtractByMask.save("C:/sapyexamples/output/extractmask")