English

How To: Use ArcPy to obtain the extents of features in a map layer

Summary

This article discusses ways to obtain the extents of features in a map layer using Python scripting.

If a feature layer has a coordinate system, the coordinate system is obtained from the layer's data source. If the coordinate system of the map data frame is different from that of the layer, the layer is reprojected dynamically to the coordinate system of the data frame. Calling the ArcPy method getExtent on a map layer returns the layer extents expressed in the units of the data frame's coordinate system.

However, it might be necessary to obtain the layer extents in the units of the layer's coordinate system. In that case, use the ArcPy Describe object to obtain the extent property relative to the layer's data source.

Procedure

Below is an example script to be run from a toolbox, where the input parameter is a map named 'Test1.' The data frame in this map has a geographic coordinate system 'GCS_North_American_1983' with angular units of degrees. There is one layer having a projected coordinate system 'North_America_Equidistant_Conic' with linear units of meters.


Code:
import arcpy, os, sys

mapPath = arcpy.GetParameterAsText (0) # Test1.mxd
mxd = arcpy.mapping.MapDocument (mapPath)
layerList = arcpy.mapping.ListLayers (mxd)

for layer in layerList:
arcpy.AddMessage ("layer name: " + layer.name)
arcpy.AddMessage ("layer extent in c/s of dataFrame: " + str(layer.getExtent()))

desc = arcpy.Describe (layer)
if layer.supports ("DATASOURCE"):
descDS = arcpy.Describe (layer.dataSource)
arcpy.AddMessage ("layer extent in c/s of dataSource: " + str(descDS.extent))

Sample results:

Code:
Running script Script1...
layer name: U.S. States (Generalized)
layer extent in c/s of dataFrame: -180.112396694215 8.88321931557688 180.112396694215 79.3719235333962 NaN NaN NaN NaN
layer extent in c/s of dataSource: -6047698.3142 -1568756.4536 2122393.9964 4372931.5431 NaN NaN NaN NaN
Completed script Script1...