HOW TO
When a data source cannot be found for a particular layer in an ArcGIS Desktop project or map document, the layer fails to draw and a red exclamation point appears next to the item in the Table of Contents showing that it is considered broken. The data source can be broken if references have been moved, renamed, deleted, or become inaccessible for reasons such as folder permissions. Python can be used to efficiently identify broken layers.
There are two ways to identify broken layers, either from a map document (.mxd) or a project (.aprx). Use the ListBrokenDataSource() Python function to check for broken layers in map documents in ArcMap, and the isBroken property of the Layer class to list broken layers in an ArcGIS Pro project.
To list broken layers in map documents in ArcMap:
import arcpy, os
path = r"<path to folder>"
for root, dirs, files in os.walk(path): for fileName in files: basename, extension = os.path.splitext(fileName)
if extension == ".mxd": fullPath = os.path.join(root, fileName) mxd = arcpy.mapping.MapDocument(fullPath) arcpy.AddMessage(mxd) brknMXD = arcpy.mapping.ListBrokenDataSources(mxd)
for brknItem in brknMXD: print "\t" + brknItem.name del mxd
The following is a sample of the full script:
import arcpy, os path = r"C:\\Test" for root, dirs, files in os.walk(path): for fileName in files: basename, extension = os.path.splitext(fileName) if extension == ".mxd": fullPath = os.path.join(root, fileName) mxd = arcpy.mapping.MapDocument(fullPath) arcpy.AddMessage(mxd) brknMXD = arcpy.mapping.ListBrokenDataSources(mxd) for brknItem in brknMXD: print "\t" + brknItem.name del mxd
import arcpy, os
path = r"C:\\Test"
for fileName in os.listdir(path): fullPath = os.path.join(path, fileName)
if os.path.isfile(fullPath): basename, extension = os.path.splitext(fullPath) if extension == ".aprx": aprx = arcpy.mp.ArcGISProject(fullPath) print("APRX: " + fileName) for m in aprx.listMaps(): print() for lyr in m.listLayers(): if lyr.isBroken: print("(BROKEN) " + lyr.name) else: print(" " + lyr.name)
The following is a sample of the full script:
import os import arcpy path = r"C:\\Test" for fileName in os.listdir(path): fullPath = os.path.join(path, fileName) if os.path.isfile(fullPath): basename, extension = os.path.splitext(fullPath) if extension == ".aprx": aprx = arcpy.mp.ArcGISProject(fullPath) print("APRX: " + fileName) for m in aprx.listMaps(): print() for lyr in m.listLayers(): if lyr.isBroken: print("(BROKEN) " + lyr.name) else: print(" " + lyr.name) del aprx
Get help from ArcGIS experts
Download the Esri Support App