Extract by Polygon (Spatial Analyst)

License Level:BasicStandardAdvanced

Summary

Extracts the cells of a raster based on a polygon.

Usage

Syntax

ExtractByPolygon (in_raster, polygon, {extraction_area})
ParameterExplanationData 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:

  • [[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)]

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.

  • INSIDE A keyword specifying that the 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.
  • OUTSIDE A keyword specifying that the cells outside the input polygon should be selected and written to the output raster. All cells inside the polygon will receive NoData values on the output raster.
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)]

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

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Requires Spatial Analyst
ArcGIS for Desktop Standard: Requires Spatial Analyst
ArcGIS for Desktop Advanced: Requires Spatial Analyst
11/8/2012