[GIS] Python script to summarize layer properties

arcpylayers

I recently found this layer properties script that summarizes layer properties in a single file or a directory and all subdirectories: http://www.esri.com/esri-news/arcuser/winter-2015/create-a-python-tool-that-summarizes-arcmap-layer-properties. It would be enormously useful to me if it worked. It does, in one case: running it on the MXD (not the directory) included with the downloaded sample file exactly as download.

The first problem is the tool doesn’t find layers within groups. Many of my maps have groups or even a group under which I have all my layers. When modifying the layout on a map with lots of data, turning off all the layers speeds time to make changes to the layout; adding a top-level group under which I have all layers means all I have to do it turn off the group to turn off all the layers. Also, I find grouping a good way to organize layers and a great way to develop several maps within a single MXD. To test my hypothesis, I put some layers in the downloaded file within a group, and those layers did not appear in the generated CSV.

The second problem is the tool doesn’t work on directories. The error I get is on this line:

for filename in filenames[2]:

And the message is:

NameError: name ‘filenames’ is not defined

Best Answer

For the second problem, I contacted the author (as suggested by DWynne) who gave the correction:

for filename in dirpath[2]:

That corrected the problem. The author told me he contacted Esri shortly after this made it into print but Esri hasn't changed it on the web. He told me he'd contact them again.

For the first problem of this script not listing layers in a group layer, this ought to fix that. Where the code says:

for layer in arcpy.mapping.ListLayers(mxd):

I added after it (sorry, I know the indenting isn't correct)

if lyr.supports("DATASOURCE"):

I found this on an Esri Help page. I'll try it out when I'm back at work. I hope this helps others who have run across this issue.


I have tested this with the problem of the script not listing layers within a group. I also expanded it to allow the output CSV to be automatically named based on the file or folder entered. Here is my code; I hope someone can make use of it.

#Import modules...
import arcpy, os, fnmatch, csv

#User input variables...
mxddirectory = arcpy.GetParameterAsText(0)
mxd_single = arcpy.GetParameterAsText(1)
outputcsvlocation = arcpy.GetParameterAsText(2)

#Create an empty list of ArcMap documents to process...
mxd_list=[]

#If an output location for the CSV is specified, use it...
if len(outputcsvlocation) > 0:
  outputcsvlocation_2 = outputcsvlocation
#...otherwise if a singe MXD is specified, name the CSV for it, or...
elif len(mxd_single) > 0:
  outputcsvlocation_2 = mxd_single.replace(".mxd",".csv")
#...if a folder location is specified, save a CSV called ListLayers.csv in that folder.
else:
  outputcsvlocation_2 = mxddirectory + "\ListLayers.csv"

#If a user defined a single mxd, add its path to the list...
if len(mxd_single) > 0:
  mxd_list.append(mxd_single)
#Otherwise walk through the input directory, adding paths for each .mxd file found to the list...
else:
  for dirpath in os.walk(mxddirectory):
    for filename in dirpath[2]:
      if fnmatch.fnmatch(filename, "*.mxd"):
        mxd_list.append(os.path.join(dirpath[0], filename))

#Iterate the list of mxd paths and gather property values then write to csv file...
if len(mxd_list) > 0:
  #Create the csv file...
  outputcsv = open(outputcsvlocation_2,"wb")
  writer = csv.writer(outputcsv, dialect = 'excel')
  #Write a header row to the csv file...
  writer.writerow(["mxdpath", "layername", "layerdescription", "layersource"])
  #Iterate through the list of ArcMap Documents...
  for mxdpath in mxd_list:
    mxdname = os.path.split(mxdpath)[1]
    try:
      mxd = arcpy.mapping.MapDocument(mxdpath)
      #Iterate through the ArcMap Document layers...
      for layer in arcpy.mapping.ListLayers(mxd):
        if layer.supports("DATASOURCE"): #List all layers that have data, skipping group layers.
          layerattributes = [mxdpath, layer.longName, layer.description, layer.dataSource]
          #Write the attributes to the csv file...
          writer.writerow(layerattributes)
    except:
      arcpy.AddMessage("EXCEPTION: {0}".format(mxdpath))
    del mxd
  #close the csv file to save it...
  outputcsv.close()
#If no ArcMap Documents are in the list, then notify via an error message...
else:
  arcpy.AddError("No ArcMap Documents found. Please check your input variables.")

Thanks to all, especially the original code author.

Related Question