ClipDataFrameToGeometry (arcpyproduction.mapping)

Summary

Clips the drawing of the data frame to a polygon geometry.

Discussion

If you need the data inside of a data frame to be a shape other than a rectangle, you can use this function to clip your data to a specific geometry. This is particularly useful when grids are created using the Make Grids And Graticules Layer geoprocessing tool or Grids and Graticules Designer because you can clip the data frame to the grid's geometry to prevent outlying features from being included in the data frame. You can also use this function to clip any data frame to a specific geometry.

This function performs the same operation as opening the Data Frame Properties dialog box in ArcMap and changing the Clip Options setting to Clip to shape.

Syntax

ClipDataFrameToGeometry (data_frame, clip_geometry, {exclusion_layers})
ParameterExplanationData Type
data_frame

The DataFrame object to be clipped.

DataFrame
clip_geometry

The Geometry object to which the data frame is clipped. This must be a polygon.

Geometry
exclusion_layers

A list of Layer objects that are excluded from the clipping operation.

Layer

Code Sample

ClipDataFrameToGeometry example 1

This sample clips the data frame based on a selected feature from a layer.

import arcpy
import arcpyproduction

# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")

# Define the map document, data frame, and geometry
mxd = arcpy.mapping.MapDocument('current')
df = mxd.activeDataFrame
g = arcpy.Geometry()
geometryList = arcpy.CopyFeatures_management('U.S. States (Generalized)', g)

# Clip the DataFrame object using the selected geometry from the
# U.S. States (Generalized) layer and save .mxd
arcpyproduction.mapping.ClipDataFrameToGeometry(df, geometryList[0])
mxd.save()

# Check in extension
arcpy.CheckInExtension("Foundation")
ClipDataFrameToGeometry example 2

This sample clips the data frame to a group of features.

import arcpy
import arcpyproduction

# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")

# Define the map document, data frame, geometry, and features to use as the
# clip geometry
mxd = arcpy.mapping.MapDocument('current')
df = mxd.activeDataFrame
g = arcpy.Geometry()
geometryList = arcpy.CopyFeatures_management('U.S. States (Generalized)', g)
geometry = geometryList[0]

# Loop through each geometry and union each feature to the next to create a
# single feature
for geo in geometryList:
    geometry = geometry.union(geo)

# Clip the geometry using the unioned geometry and save .mxd
arcpyproduction.mapping.ClipDataFrameToGeometry(df, geometry)
mxd.save()

# Check in extension
arcpy.CheckInExtension("Foundation")
ClipDataFrameToGeometry example 3

This sample clips the data frame to a new polygon.

import arcpy
import arcpyproduction

# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")

# Define map document, data frame, and polygon geometry coordinates
mxd = arcpy.mapping.MapDocument('current')
df = mxd.activeDataFrame
array = arcpy.Array()
coordList = ['-105.0;39.0','-100.0;39.0','-100.0;35.0','-105.0; 35.0']

# Create polygon geometry
for coordPair in coordList:
    x, y = coordPair.split(";")
    pnt = arcpy.Point(x,y)
    array.add(pnt)
array.add(array.getObject(0))
boundaryPolygon = arcpy.Polygon(array)

# Clip data frame using the defined polygon and save .mxd
arcpyproduction.mapping.ClipDataFrameToGeometry(df, boundaryPolygon)
mxd.save()

# Check in extension
arcpy.CheckInExtension("Foundation")
ClipDataFrameToGeometry example 4

This sample clips the data frame to the geometry of a grid stored in the GRD_ feature class, which defines the extent of the grids and graticules layer, and excludes the other grid features during the clipping operation. There can be several grids stored in the same feature dataset, so the first grid feature will be used as the data frame extent. Once the data frame is clipped, the map is exported to a Production PDF file.

import arcpy
import array
import os
import arcpy.mapping as map
import arcpyproduction.mapping as pmap

# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")

# Set working properties
grid_wksp = r"C:\project\grids\Grids.gdb\NAD_83"
out_lyr = r"C:\project\grids\GRD_NAD_83.lyr"
if os.path.exists(out_lyr):
    os.remove(out_lyr)
mxd = map.MapDocument(r"C:\project\grids\ClipDataFrameTest.mxd")
df = mxd.activeDataFrame
out_pdf = r"C:\project\grids\ClipDataFrameTest.pdf"
settings = r"C:\project\grids\print_settings.xml"

# Get the properties of current grid loaded in map
grid = pmap.ListGrids(grid_wksp,"*USNG_500K_NAD83*")[0]
grid_name = grid.name
grid_type = grid.type
grid_layer = grid.layer

# Create layer object from GRD feature layer and select the required grid feature
arcpy.MakeFeatureLayer_management(grid_wksp+"\\GRD_NAD_83",out_lyr)
arcpy.SelectLayerByAttribute_management(out_lyr,"NEW_SELECTION",'"MapName" = '+"'"+grid_name+"'"+' AND '+'"GridType" = '+"'"+grid_type+"'")

# Store the geometry of grid feature in geometry object
g = arcpy.Geometry()
geomLst = arcpy.CopyFeatures_management(out_lyr, g)

# Create layer exclusion list so the grid layers are not clipped
ex_layers = []
lyrs = map.ListLayers(map.MapDocument(r"C:\project\grids\ClipDataFrameTest.mxd"))
for lyr in lyrs:
    if lyr.name.find("USNG")>0:
        ex_layers.append(lyr)
    elif lyr.name.find("Transportation")>0:
        ex_layers.append(lyr)

# Clip data frame with exclusion layers
pmap.ClipDataFrameToGeometry(df, geomLst[0],ex_layers)

# Export the mxd to PDF
pmap.ExportToProductionPDF(mxd, out_pdf, settings)

# Check in extension
arcpy.CheckInExtension("Foundation")

Related Topics

6/8/2015