Skip To Content

Extract by Mask

Available with Spatial Analyst license.

Summary

Extracts the cells of a raster that correspond to the areas defined by a mask.

Illustration

Extract by Mask illustration
OutRas = ExtractByMask(InRas1, InMsk1)

Usage

  • Additional attributes from the input raster, if any, will be carried over as-is to the output raster attribute table. Depending on the nature of the property being recorded, some of the attribute values may need to be recalculated.

  • When a multiband raster is specified as the Input Raster (in_raster in Python), all bands will be used.

    To process a selection of bands from a multiband raster, first create a new raster dataset composed of those particular bands with the Composite Bands tool, and use the result as the Input Raster (in_raster in Python).

    The default output format is a geodatabase raster. If an Esri Grid stack is specified as the output format, note that the name cannot start with a number, use spaces, or be more than nine characters in length.

  • When a multiband raster is specified for the input raster mask, only the first band will be used in the operation.

  • When the Input raster (in_raster in Python) and a raster Input raster mask or feature mask data data (in_mask_data in Python) are of the same cell size, and the cells are aligned, they will be used directly in the tool. They will not be resampled internally during tool execution.

    If the cell size is different, the output cell size will be the maximum of the inputs, and the Input raster (in_raster in Python) will be used as the snap raster internally. If the cell size is same, but the cells are not aligned, the Input raster will be used as the snap raster internally. Either of these cases will trigger an internal resampling before the extraction operation is performed.

    More information is available in the Cell Size and Snap Raster environment topics.

  • If the mask input is a feature, it will be converted to a raster internally, using the cell size and cell alignment from the Input raster.

  • If Mask is specified in the environment setting while executing the Extract by Mask tool, the output raster would have cell values only for the area that lies within the intersection of the environment mask and the input mask data.

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

Syntax

ExtractByMask (in_raster, in_mask_data)
ParameterExplanationData Type
in_raster

The input raster from which cells will be extracted.

Raster Layer
in_mask_data

Input mask data defining areas to extract.

It can be 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.

When the input mask is feature data, cells in the input raster whose center falls within the perimeter of the feature will be included in the output, while cells whose center falls outside it will receive NoData.

Raster Layer; Feature Layer

Return Value

NameExplanationData Type
out_raster

The output raster containing the cell values extracted from the input raster.

Raster

Code sample

ExtractByMask example 1 (Python window)

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")
ExtractByMask example 2 (stand-alone script)

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

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

# Execute ExtractByMask
outExtractByMask = ExtractByMask(inRaster, inMaskData)

# Save the output 
outExtractByMask.save("C:/sapyexamples/output/extractmask")

Licensing information

  • ArcGIS Desktop Basic: Requires Spatial Analyst
  • ArcGIS Desktop Standard: Requires Spatial Analyst
  • ArcGIS Desktop Advanced: Requires Spatial Analyst

Related topics