1

I have a script that lists all layers in an MXD and I have a script that lists all broken layers in an MXD (http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/listbrokendatasources.htm). I want to list all layers, their name, source, description, and whether or not their source is broken. Can anyone help me with this? I can't quite figure out how to integrate the two...

import arcpy, os, fnmatch, csv 

#Create an empty list of ArcMap documents to process...
mxd_list=["A.mxd", "B.mxd", "C.mxd"]

for mxd in mxd_list:
    mxd = arcpy.mapping.MapDocument(mxd)
    mapPath = mxd.filePath  
    fileName = os.path.basename(mapPath)
    layers = arcpy.mapping.ListLayers(mxd)  
    filepath = "C:/Users/USERNAME/Desktop/"+ fileName[:-4]+".csv"  
    writer = csv.writer(file(filepath, 'wb'))  
    sourcelist = []  

    for layer in layers:  
        if layer.supports("dataSource"):  
            layerattributes = [layer.longName, layer.dataSource]
            #Write the attributes to the csv file...
            writer.writerow(layerattributes)  
    writer.writerow(sourcelist)  
    del writer 
7
  • The source you linked to will list the broken sources for all layers in all maps in a folder. To clarify, are you trying to report even if the source isn't broken?
    – smiller
    Jun 27 '18 at 20:17
  • @smiller yes - I want a list of all layers, all sources, and whether or not the source is broken on each layer. The link is for a function that only takes in MXDs as an argument. I want a function to call on each layer individually, but haven't found it.
    – Stella
    Jun 27 '18 at 20:28
  • This might help: gis.stackexchange.com/questions/150734/…
    – squirrel72
    Jun 27 '18 at 20:42
  • Or maybe this page will help. gis.stackexchange.com/questions/30630/…
    – squirrel72
    Jun 27 '18 at 20:46
  • 1
    The Layer class has an isBroken property. You should be careful with it, since it does not reflect if the source has been broken since the last time the MXD was written. An arcpy.Exists () will also be necessary.
    – Vince
    Jun 28 '18 at 12:09
1

I figured out an easy fix: I create an array of the broken layers, then check to see if the layers in the MXD are in that array. I instruct my output to write a "yes" or "No" to determine if the layer is broken.

#Import necessary libraries
import arcpy, os, fnmatch, csv 

#Create an empty list of ArcMap documents to process. 
mxd_list=[r"\\...\XYZ.mxd","//computer/d$/ABC.mxd"]

#Initiate a for loop to run through each MXD in the list
for mxd in mxd_list:
    #Define the MXD
    mxd = arcpy.mapping.MapDocument(mxd)

    #Get the file path from the MXD
    mapPath = mxd.filePath  

    #Get the name of the MXD from the full path
    fileName = os.path.basename(mapPath)

    #Get a list of all layers in the given MXD
    layers = arcpy.mapping.ListLayers(mxd)  

    #Define the output file path. Here the file is being saved to the desktop with the same name as the MXD. Change the file path to an accessible location.
    filepath = "C:/Users/USERNAME/Desktop/"+ fileName[:-4]+".csv"  

    #Define the file to be written as a CSV.
    writer = csv.writer(file(filepath, 'wb'))  

    #Identify all broken layers in the MXD
    brknList = arcpy.mapping.ListBrokenDataSources(mxd)

    #Initiate a for loop to run through each layer within the MXD
    for layer in layers:  
        #Check to see if the layer supports the attribute "dataSource". Not all layers support this stored metadata value.

        if layer.supports("dataSource"): 
        #Determine if the layer is broken or not by comparing it to the list of broken layers 
            if layer in brknList:
                broken = "Yes"
            else:
                broken = "No"

            #Compile a list of layer attributes. This list can be modified based on the defined properties of the layer class in arcpy (http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/layer-class.htm).
            layerattributes = [layer.longName, layer.dataSource, broken]

            #Write the attributes to the csv file.
            writer.writerow(layerattributes)  

    #Delete variables that will be recycled in the loop
    del writer 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.