2

What I am trying to do is create a list of all the data sources in a directory filled with mxds. So I have this code pasted below. For some reason when listing layers it can take up to 50 minutes even though the mxd only has around 10 or 15 layers. Is this a common occurrence with the listlayers method?

with open('data.csv', 'wb') as csvfile:
fieldnames = ['File_Path', 'File_Size(MB)', 'Last_Edit', 'MXD_Version', 'Author', 'PageSize', \
              'XMAX', 'XMIN', 'YMAX', 'YMIN', 'DATASOURCE'] #all the header fields
writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
writer.writeheader()
for mapDocument in findFiles(r"W:/GIS (no purge)", '*.mxd'):
    print mapDocument
    st = os.stat(mapDocument)
    print "Using ArcPY Mods..."
    mxd = arcpy.mapping.MapDocument(mapDocument) #using arcpy to gather more information
    print "Reading Map Document..."
    writer.writerow({'File_Path' : str(mapDocument),\
                     'File_Size(MB)' : str(((st[ST_SIZE])/1024.0)/1024.0), \
                     'Last_Edit' : str(time.ctime(st[ST_MTIME])), \
                     'MXD_Version' : getMXDVersion(mapDocument)})

    print "Searching Layers and Writing..."
    for layer in arcpy.mapping.ListLayers(mxd):
        print "boop"
        if layer.supports("DATASOURCE"):
            writer.writerow({'DATASOURCE' : layer.dataSource})
        else:
            break
    del mxd

So my issue with the above code is when I am running it It will take forever to get to the 'print "boop"'but once it gets there it runs perfectly fine. It seems to get hung up at the arcpy.mapping.ListLayers(mxd) for a very long time.

Sometimes its great, only takes a few seconds, other times 8 minutes. That is manageable, but when I leave my code running all night and some of them take 50 minutes or it just crashes, that is the problem (it crashed due to a memory error). These mxds do not have that many layers in them either.

There is usually just one data frame that has at the most 20 layers in them. I can open these mxds myself and it only takes a few minutes at the most when i do.

From further testing it doesn't seem to be related to the amount of layers either. There were a few mxds that had around 30 layers that were completed in a few seconds and then another few that took 4 hours (it had 44 layers) and there are some that had 30 layers and took 50 minutes. If I could just pinpoint why it takes so long it would be great.

1

First, check your script against a directory with one mxd and one layer, to make sure that the problem isn't something in the part of your script that you didn't post. Next, you need to modify your script to test for more "layer.supports" properties besides "dataSource". I use the version below:

        for lyr in lyrList:
            dType = "UNK"
            dName = "UNK"
            dPath = "UNK"
            if lyr.supports("dataSource"):
                if lyr.supports("workspacePath"):
                    dPath = lyr.workspacePath
                if lyr.supports("datasetName"):
                    dName = lyr.datasetName
                try:
                    dType = arcpy.Describe(lyr.dataSource).datasettype
                except:
                    dType = "Possible broken link"
            else:
                if lyr.isGroupLayer:
                    dType = "Group Layer"
                if lyr.supports("SERVICEPROPERTIES"):
                    arcpy.AddMessage('Found map survice')
                    dType = lyr.serviceProperties["ServiceType"]
  • I have tried it on a mxd and a few mxds. It seems that on some mxds it just takes forever to use anything with arcpy.mapping. Even in other parts of the script. I am going to post a larger section of the script. I will add this part into my script and get back to you. – Sharm Jun 11 '15 at 19:35
  • could you explain why you think the supports() method needs to be used more? If dataSource is the only property that the OP is interested in, I see no need to test for other properties. Also, @Sharm, the 'break' command will quit the for loop as soon as a layer does not support dataSource (like a group layer). Perhaps that's what you want, but if you just want to skip to the next layer in the iteration, I'd suggest just removing the 'else' block altogether. – mr.adam Jun 11 '15 at 19:49
  • Yeah I just realized that. I took out the entire else statement. And I have not been able to test it but doesn't service type just shoot back the the service it is getting the layer from? – Sharm Jun 11 '15 at 20:09

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.