Select to view content in your preferred language

Bypass Looking for Template File in Mxd Document in Python

2176
3
02-15-2012 09:53 AM
MichaelVolz
Esteemed Contributor
To All Python Users:

I am trying to write several python scripts that would loop through mxds in a user selected directory and either analyze the properties of the mxd or make changes to the mxd.  Unfortunately, with either type of python script, the python script crashes when it gets to an mxd that is referencing a mxt that is not on the machine where the python script is running from.

This is the message you get when you open the mxd in ArcMap.

"Unable to find the following template used by this map document:

C:\Program Files\...\layers.mxt

You can still use this map document but customizations such as macros stored in that template won't be available.  Would you like to browse to the template file to access those customizations?

Yes No"

Other than putting exceptions into the code for these mxds (there could be 100s or 1000s), I was wondering if there was a way for python to be able to bypass this message so the python script can keep running.

Any help or hints regarding this topic are greatly appreciated.  Thank you.
Tags (2)
0 Kudos
3 Replies
deleted-user-h2MwrUDd3vgt
Deactivated User
I know that it's been awhile, but did you ever find a solution to this? I'm trying to update a large collection of maps and my script keeps stalling out and I think that it's because many of the maps are using custom templates. If you have figured out a way to test for this error, it would be a huge help.

Thanks!
0 Kudos
MichaelVolz
Esteemed Contributor
Nathan:

This might not be why your script is stalling out.  My original script was running on an older Windows XP machine where I would run out of memory after processing a large collection of files (200 or more).  I now run the script on a Windows 7 or better a Windows Server 2008 machine and I do not have memory issues anymore (Memory is handled better my the OS I am told).

Take a look at your Task Manager and watch the memory usage of your pythonw.exe process.  If it keeps going up for some time and then plateaus out at a high value with no CPU usage then you have probably stalled out.

Could you also share your code and explain what business need you are trying to accomplish?

Thank you.
0 Kudos
deleted-user-h2MwrUDd3vgt
Deactivated User
It's definitely not a memory issue, python never peaks beyond 20% or so of my total memory. The code doesn't crash, it simply hangs on a particular map for a lot longer than is necessary before moving on to the next map. Once I identified a couple of those maps, I tried opening them and got the same error message about missing a template. I'm sure that the issue is that the maps are older and are using custom templates that are no longer available.

The main point for the code is to inventory a large collection of maps and data that we were given by a client and in order to complete it, I need to try and update as many of the layers as possible in order to get a count of feature classes or shapefiles that are being used across each of the maps.

If I can figure out a way to either identify which of the maps are using a template in order to avoid them, or to apply a timer that skips a particular map if it doesn't progress after a certain amount of time, I can at least inventory the maps that aren't corrupted.

mdRow = 1
mlRow = 1
map_key = 1
connectedLayers = 0
missingLayers = 0
groupLayers = 0
rawLyrList = {}
pathList = {} 
mapList = {}


def WriteMapRow(maps):
    csvRow = []
    for k, v in maps.iteritems():
        s = k + ','
        for val in v:
            s += str(val) + ','        
        csvRow.append(s)
    f = open('S:\Projects\Current\HUD\DATA\Incoming\Maps.csv', 'w')
    f.write('\n'.join(csvRow))
    f.close()    
 
def WriteLayerRow(layers):
    csvRow = []
    for k,v in layers.iteritems():
        s = str(k) + ','
        s += str(v) + ','
        csvRow.append(s)
    f = open('S:\Projects\Current\HUD\DATA\Incoming\Layers.csv', 'w')
    f.write('\n'.join(csvRow))
    f.close()    


#function to split out actual layers from any group layers
def buildLayerList(lyrList):
    rawLyrList = []
    for lyr in lyrList:
        if not lyr.isGroupLayer:
            rawLyrList.append(lyr)
    return rawLyrList
  
for root, dirs,files in os.walk(r'S:\Projects\Current\HUD\DATA\Incoming'):
    for fname in files:
        path = os.path.join(root, fname)
        if path[-3:] == 'mxd':                      
            print "Map: " + path
            try:   
                mxd = arcpy.mapping.MapDocument(path)        
                df = arcpy.mapping.ListDataFrames(mxd)
                rawLyrList = []
                for df in arcpy.mapping.ListDataFrames(mxd):
                    lyrList = arcpy.mapping.ListLayers(mxd, '', df)
                    rawLyrList = buildLayerList(lyrList)
                    for lyr in rawLyrList:
                        if lyr.isBroken:
                            missingLayers += 1
                            if lyr.dataSource not in pathList and lyr.supports('DATASOURCE'):
                                pathList[lyr.dataSource] = (mlRow)                            
                                print 'Datasource Appended: ', lyr.dataSource
                                mlRow += 1
                        else:
                            connectedLayers += 1                                                                   


                mapList[path] = (mxd.filePath.split('\\')[-1], map_key,
                         connectedLayers, missingLayers)


                print 'Connected: ', connectedLayers
                print 'Missing: ', missingLayers
                connectedLayers = 0
                missingLayers = 0
                groupLayers = 0
                mdRow += 1
                map_key += 1            
                del mxd
                print
                
            except:
                print "Unknown Error"
                mapList[path] = (mxd.filePath.split('\\')[-1],
                            map_key, 'Unknown Error')
                map_key += 1
                mdRow += 1
                del mxd


WriteMapRow(mapList)
WriteLayerRow(pathList)


print "Successful"              
0 Kudos