Extract by Polygon (Spatial Analyst)

Summary

Extracts the cells of a raster based on a polygon by specifying the polygon's vertices.

Legacy:

This tool is deprecated and will be removed in a future release.

The Extract by Mask tool provides enhanced functionality or performance.

Illustration

Extract by Points illustration
OutRas = ExtractByPolygon(InRas1,[Point(1.4,0.4),Point(1.4,4.6),Point(3.6,4.6),Point(5.6,2.6),Point(3.6,0.4),Point(1.4,0.4)],"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.

  • To extract based on a polygon in a feature class instead of providing a series of x,y pairs, you can use the Extract By Mask tool.

  • The polygon object may consist of just one part, or many parts as a polygon class. In the later case, all polygon parts must be contiguous, such that they could be outlined by one polygon. To extract based on a polygon feature that contains multiple disconnected parts, use the Extract By Mask tool.

  • The center of the cell is used to determine whether a cell is inside or outside a polygon. If the center is within the arcs of the polygon, the cell is considered fully inside, even if portions of the cell fall outside the polygon.

  • The polygon has a limit of 1,000 vertices. Polygon vertices must be entered in a clockwise order. The first and last vertex should be the same to close the polygon. This is especially important if multiple polygons are to be used. In that case, if the last point for each individual polygon is not identical to its starting vertex, polygons may be closed automatically by directly connecting to the first vertex. However, this may produce a result different from what you may have expected, so take care if employing this technique.

    The arcs of the polygon can cross one another, but convoluted polygons are not recommended.

  • Cell locations that are not selected are assigned a value of NoData.

  • If the input raster is integer, the output raster will be integer. If the input is floating point, the output will be floating point.

  • 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
Polygon

A polygon (or polygons) defined by a series of vertices (x,y point coordinates) that identify the area of the input raster to be extracted. The last coordinate of a polygon part should be the same as the first in order to close a polygon.

When specifying multiple polygons, they must be contiguous. Enter the series of coordinates polygon by polygon. Be sure to close each part by defining the last coordinate the same as the first one.

The points are in the same map units as the input raster.

Point
Extraction area
(Optional)

Identifies whether to extract cells inside or outside the input polygon.

  • InsideThe cells inside the input polygon should be selected and written to the output raster. All cells outside the polygon will receive NoData values on the output raster.
  • OutsideThe cells outside the input polygon should be selected and written to the output raster. All cells inside the polygon will receive NoData.
String

Return Value

LabelExplanationData Type
Output raster

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

Raster

ExtractByPolygon(in_raster, polygon, {extraction_area})
NameExplanationData Type
in_raster

The input raster from which cells will be extracted.

Raster Layer
polygon
[point,...]

A polygon (or polygons) that defines the area of the input raster to be extracted.

Each polygon part is a list of vertices defined by Point classes. The points are specified as x,y coordinate pairs in the same map units as the input raster.

The form of the object for a single polygon is as follows:

  • [point(x1,y1), point(x2,y2), ..., point(xn,yn), point(x1,y1]

Optionally, a group of multiple polygons can be specified by using a Polygon class to define a list of polygon parts. Note that in order to do this, all parts must be contiguous, and thus be encompassed by one single perimeter shape. The form of the object in this case would be in a contiguous list as follows:

  • [point(x1,y1), point(x2,y2), ..., point(xn,yn), point(x1,y1), point(x'1,y'1), point(x'2,y'2), ..., point(x'n,y'n), point(x'1,y'1), ...]

In all cases, the last coordinate for each polygon part should be the same as the first in order to close it.

Point
extraction_area
(Optional)

Identifies whether to extract cells inside or outside the input polygon.

  • INSIDEThe cells inside the input polygon should be selected and written to the output raster. All cells outside the polygon will receive NoData values on the output raster.
  • OUTSIDEThe cells outside the input polygon should be selected and written to the output raster. All cells inside the polygon will receive NoData.
String

Return Value

NameExplanationData Type
out_raster

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

Raster

Code sample

ExtractByPolygon example 1 (Python window)

This example extracts cells from a raster based on the defined polygon coordinates.

import arcpy
from arcpy import env
from arcpy.sa import *
polyPoints = [arcpy.Point(743050, 4321275), arcpy.Point(743100, 4321200), 
             arcpy.Point(743500, 4322000),arcpy.Point(742900, 4321800)]
env.workspace = "C:/sapyexamples/data"
extPolygonOut = ExtractByPolygon("soil", polyPoints, "INSIDE")
extPolygonOut.save("c:/sapyexamples/output/extpoly")
ExtractByPolygon example 2 (stand-alone script)

This example extracts cells from a raster based on the defined polygon coordinates.

# Name: ExtractByPolgyon_Ex_02.py
# Description: Extracts the cells of a raster based on a polygon.
# 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 = "soil"
polyPoints = [arcpy.Point(743050, 4321275), arcpy.Point(743100, 4321200), 
             arcpy.Point(743500, 4322000),arcpy.Point(742900, 4321800)]

# Execute ExtractByPolygon
extPolygonOut = ExtractByPolygon(inRaster, polyPoints, "INSIDE")

# Save the output 
extPolygonOut.save("c:/sapyexamples/output/extpoly02")

Related topics