Available with Production Mapping license.
Summary
Configures and applies layer masking settings for a specific data frame.
Discussion
Masking features can be useful when there is text on a map or chart that is obscured by other features or symbology. With grids and graticules layers, masking can be used to hide gridlines when annotation appears inside the grid.


Syntax
MaskLayer (data_frame, operation, masking_layer, masked_layer, {symbol_levels})
Parameter | Explanation | Data Type |
data_frame | The DataFrame object that will have masking applied. | DataFrame |
operation | Indicates whether a masking and masked layer relationship are added or removed from the masking settings:
| String |
masking_layer | The Layer object that will mask the masked layer. The polygons in this layer will be used to hide parts of the masked layer. | Layer |
masked_layer | The Layer object that will be masked by the masking layer. The features in this layer will be hidden by the polygons from the masking layer. | Layer |
symbol_levels [symbol_levels,...] | A list of symbol levels (layers) that will be hidden from the masked layer. | String |
Code sample
MaskLayer example 1
This sample masks annotation layers in a map.
import arcpy
import arcpyproduction
# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")
# Load map and get active from the map
mxd = arcpy.mapping.MapDocument(r'C:\project\masking\MaskingExamples.mxd')
df = mxd.activeDataFrame
# Get list of the data frame layers
layers = arcpy.mapping.ListLayers(df)
# Get sample layers
highways_layer = layers[2]
rivers_layer = layers[3]
# Get masked layer
mask_layer = layers[4]
# Enable masking on the data frame
arcpyproduction.mapping.EnableLayerMasking(df,'true')
# Mask layer annotation layer
arcpyproduction.mapping.MaskLayer(df, 'APPEND', mask_layer, highways_layer) # fully hide highways
arcpyproduction.mapping.MaskLayer(df, 'APPEND', mask_layer, rivers_layer, "1") # partilly hide rives
# Save updated map
mxd.save()
# Check in Production Mapping license
arcpy.CheckInExtension("Foundation")
MaskLayer example 2
This sample enables masking on some layers and disables masking on others.
import arcpy
import arcpyproduction
# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")
# Load map and get active from the map
mxd = arcpy.mapping.MapDocument(r'C:\project\masking\MaskingExamples.mxd')
df = mxd.activeDataFrame
# Get list of the data frame layers
layers = arcpy.mapping.ListLayers(df)
# Get masked layer
mask_layer = layers[4]
# Enable masking on the data frame
arcpyproduction.mapping.EnableLayerMasking(df,'true')
# Find all polyline layers and mask them and unmask all other feature layers
for layer in layers:
if layer.isFeatureLayer:
if 'Polyline' == arcpy.Describe(layer.dataSource).shapeType:
arcpyproduction.mapping.MaskLayer(df, 'APPEND', mask_layer, layer)
elif (mask_layer != layer): # masked and masking layer cannot be the same
arcpyproduction.mapping.MaskLayer(df, 'REMOVE', mask_layer, layer)
# Save updated map
mxd.save()
# Check in Production Mapping license
arcpy.CheckInExtension("Foundation")
MaskLayer example 3
This sample masks annotation features inside the grid. Mask features are used as the masking layer, and the gridline is used for the masked layer.
import arcpy
import arcpyproduction
# for testing
#arcpy.env.overwriteOutput=True
# Check out Production Mapping license
arcpy.CheckOutExtension("Foundation")
# Define variables
mxd = arcpy.mapping.MapDocument(r'C:\project\masking\ClipDataFrameTest.mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]
anno_layer = arcpy.MakeFeatureLayer_management(r'C:\project\masking\Grids.gdb\NAD_83\ANO_NAD_83_500000')
anno_masks = r'C:\project\masking\Grids.gdb\AnnoMasks'
gridline_layer = arcpy.mapping.ListLayers(mxd,"GLN_NAD_83",df)[0]
# Create masks from the grid annotation features and create layer object
masks=arcpy.FeatureOutlineMasks_cartography(anno_layer,r'C:\project\masking\Grids.gdb\AnnoMasks','250000',arcpy.SpatialReference(3719),'10','CONVEX_HULL','ALL_FEATURES')
anno_mask_layer=arcpy.mapping.Layer(masks.getOutput(0))
# Add masks to map and create masking layer
arcpy.mapping.AddLayer(df, anno_mask_layer,'BOTTOM')
masking_layer = arcpy.mapping.ListLayers(mxd,anno_mask_layer.name,df)[0]
# Enable masking on the first data frame
arcpyproduction.mapping.EnableLayerMasking(df,'ENABLE')
# Mask the gridline features with the masking features
arcpyproduction.mapping.MaskLayer(df,'APPEND',masking_layer,gridline_layer)
# Save MXD
mxd.save()
# Check in Production Mapping license
arcpy.CheckInExtension("Foundation")