Extract by Polygon (Spatial Analyst)
Summary
Extracts the cells of a raster based on a polygon.
Usage
-
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 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 must be the same to close the polygon if multiple polygons are to be used. If the last points are not identical, the polygon will be closed automatically. 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.
-
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 |
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. Optionally a Polygon class can be used to define a list of polygon parts. The points are specified as x,y coordinate pairs. The form of the object is:
Note that the last coordinate should be the same as the first in order to close the polygon. | Point |
extraction_area (Optional) |
Identifies whether to extract cells inside or outside the input polygon.
| String |
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 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")
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)]
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute ExtractByPolygon
extPolygonOut = ExtractByPolygon(inRaster, polyPoints, "INSIDE")
# Save the output
extPolygonOut.save("c:/sapyexamples/output/extpoly02")