Extract by Mask (Spatial Analyst)

Summary

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

Illustration

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

Usage

  • Additional attributes from the input raster, if any, will be carried over as-is to the output raster attribute table. Depending on 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) value, all bands will be used.

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

    The default output format is a geodatabase raster. If an Esri Grid stack is specified as the output format, the name of the stack 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) value and the Input raster or feature mask data raster 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 operation.

    If the cell size is different, the output cell size will be the maximum of the inputs, and the Input raster value will be used as the snap raster internally. If the cell size is the same but the cells are not aligned, the Input raster value 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 (snap raster) from the Input raster value by default.

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

  • You can use the Analysis Extent (analysis_extent in Python) parameter to specify the output analysis area explicitly for a stand-alone tool operation or to override the environment setting as part of a workflow. You can specify the extent by typing values, choosing the display extent, selecting a layer, or browsing for an input dataset.

  • The default Analysis Extent value is calculated from the intersection of the Input raster value and the Input raster or feature mask data value.

  • If the analysis extent is not explicitly specified as the parameter value, it will be derived from the analysis environment settings.

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

Parameters

LabelExplanationData Type
Input raster

The input raster from which cells will be extracted.

Raster Layer
Input raster or feature mask data

The input mask data defining the cell locations to extract.

It can be a raster or a 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 specified shape of the feature will be included in the output, while cells whose center falls outside will receive NoData.

Raster Layer; Feature Layer
Extraction Area
(Optional)

Specifies whether cells inside or outside the locations defined by the input mask will be selected and written to the output raster.

  • InsideCells within the input mask will be selected and written to the output raster. All cells outside the mask will receive NoData on the output raster. This is default.
  • OutsideCells outside the input mask will be selected and written to the output raster. All cells covered by the mask will receive NoData.
String
Analysis Extent
(Optional)

The extent that defines the area to be extracted.

By default, the extent is calculated as the intersection of the Input raster value and the Input raster or feature mask data value. Processing will occur out to the x and y limits, and cells outside that extent will be NoData.

The coordinates are specified in the same map units as the input raster if not explicitly set by the analysis environment.

  • Current Display Extent Map View—The extent will be based on the active map or scene. This option is only available when there is an active map.
  • Draw Extent Draw Extent—The extent will be based on a rectangle drawn on the map or scene. This option will create a feature class in the project geodatabase and add a layer to the map. The feature class will have the same coordinate system as the map.
    Note:

    This option is not available in the Environments dialog box. It is only available from a tool parameter with an extent data type or from the Environments tab on a tool dialog box.

    Note:

    When the Enable and disable editing from the Edit tab editing option is checked, you must enable editing on the Edit ribbon tab to draw the extent.

  • Extent of a Layer Layer—The extent will be based on an active map layer. Use the drop-down list to choose an available layer or use the Extent of data in all layers option to get the combined extent of all active map layers, excluding the basemap. This option is only available when there is an active map with layers.

    Each map layer has the following options:

    • All Features Select All—The extent of all features in the layer.
    • Selected Features Area from Selected Features—The extent of the selected features in the layer.
    • Visible Features Extent Indicator—The extent of the visible features in the layer.
      Note:

      The extents from the Selected Features Area from Selected Features and Visible Features Extent Indicator options are only available for feature layers.

  • Browse Browse—The extent will be based on an existing dataset.
  • Clipboard Paste—The extent can be copied to and pasted from the clipboard.
    • Copy Extent Copy—Copies the extent coordinates and coordinate system to the clipboard.
    • Use Paste Extent Paste—Pastes the extent coordinates and, optionally, the coordinate system from the clipboard. If the clipboard values do not include a coordinate system, the extent will use the map’s coordinate system.
    Note:

    The extent coordinates are copied to and pasted from the clipboard using the same formatting and order as the ArcPy Extent object: x-min, y-min, x-max, y-max, and the spatial reference.

  • Reset Extent Reset—The extent will be reset to the default value.
  • Manually entered coordinates—The coordinates must be numeric values and in the active map's coordinate system.
    Caution:

    The map may use different display units than the entered coordinates. The use of a cardinal direction (N, S, E, W) is not supported. Use a negative value sign for south and west coordinates.

Extent

Return Value

LabelExplanationData Type
Output raster

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

Raster

ExtractByMask(in_raster, in_mask_data, {extraction_area}, {analysis_extent})
NameExplanationData Type
in_raster

The input raster from which cells will be extracted.

Raster Layer
in_mask_data

The input mask data defining the cell locations to extract.

It can be a raster or a 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 specified shape of the feature will be included in the output, while cells whose center falls outside will receive NoData.

Raster Layer; Feature Layer
extraction_area
(Optional)

Specifies whether cells inside or outside the locations defined by the input mask will be selected and written to the output raster.

  • INSIDECells within the input mask will be selected and written to the output raster. All cells outside the mask will receive NoData on the output raster. This is default.
  • OUTSIDECells outside the input mask will be selected and written to the output raster. All cells covered by the mask will receive NoData.
String
analysis_extent
(Optional)

The extent that defines the area to be extracted.

If not specified, the default extent is the intersection of the in_raster value and the in_mask_data value.

The coordinates are specified in the same map units as the input raster if not explicitly set by the analysis environment.

  • MAXOF—The maximum extent of all inputs will be used.
  • MINOF—The minimum area common to all inputs will be used.
  • DISPLAY—The extent is equal to the visible display.
  • Layer name—The extent of the specified layer will be used.
  • Extent object—The extent of the specified object will be used.
  • Space delimited string of coordinates—The extent of the specified string will be used. Coordinates are expressed in the order of x-min, y-min, x-max, y-max.
Extent

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", "INSIDE")
outExtractByMask.save("C:/sapyexamples/output/maskextract")
ExtractByMask example 2 (stand-alone script)

This example extracts cells from a raster for all areas outside a mask defined by an input polygon shapefile feature class, keeping the output extent of the input raster.

# Name: ExtractByMask_Ex_02.py
# Description: Extracts the cells of a elevation raster for all areas outside of the mask features.
#     Keeping the output extent of the input elevation 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 = "elevation"
inMaskData = "mask.shp"
extraction_area = "OUTSIDE"
analysis_extent = "elevation"


# Execute ExtractByMask
outExtractByMask = ExtractByMask(inRaster, inMaskData, extraction_area, analysis_extent)

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

Related topics