Problems to save mxd with removed broken Links

2437
7
02-27-2012 05:27 AM
FelixSvoboda
New Contributor
Hi
I've written a Pyhton script which lists all layers, layer with broken links & datasources of a Project. At the end all datasources & layers with broken links are removed and a copy of the mxd is saved.
The trouble start if I have broken links to our former geoserver. When I run my script it removes those datasources properly. But to open the newly saved project takes ages, eventhough the datasource & layer is removed! So after 5 minutes it opens (if there was only one removed datasource). Now I can save it (ctrl+s) and then it opens with normal speed (<30 second).

Do you have any solutions or tips for this problem?

thanks
felix

def startAnalysis(inputMxd, destLogFile):
 lyrName, lyrSavedName, brokenLayerName=r"Dataframe: Grouplayer\Layer; Datasource","\n\nSaved layer: ","\n\nBroken Links:"
 mxd = arcpy.mapping.MapDocument(inputMxd)
    for df in arcpy.mapping.ListDataFrames(mxd):            # nacheinander alle Dataframes in Projekt verwenden
  for lyr in arcpy.mapping.ListLayers(mxd, "", df):   # nacheinander alle Layer in Dataframe verwenden
   lyrName=lyrName + "\n" + df.name + ";" + lyr.longName
   try:
    lyrName=lyrName + ";" + lyr.dataSource      # speichern des "Dataframes;(Grouplayer)/Layer"
                if blnSaveLayer.get():
     lyr.saveACopy(lyr.name)
     lyrSavedName=lyrSavedName + "\n" + lyr.name
            except:
    pass

            brokenList=arcpy.mapping.ListBrokenDataSources (df)
            for brokenLyr in brokenList:
                arcpy.mapping.RemoveLayer(df, brokenLyr)
                try:
                    brokenLayerName=brokenLayerName + "\n" + df.name + brokenLyr.longName + "; " + brokenLyr.dataSource
                except:
                    print "Error: " +brokenLyr

        ouputMsg= lyrName + lyrSavedName + lyrNotSavedName + brokenLayerName
        f = open(destLogFile, "w")
        f.write(ouputMsg.encode("utf8"))
        f.close()

        mxd.saveACopy(os.path.splitext(inputMxd)[0]+"_WithoutBrokenLinks.mxd")
Tags (2)
0 Kudos
7 Replies
MichaelVolz
Esteemed Contributor
Felix:

I had a similar problem when I performed an SDE conversion from 9.1 to 9.3 using VBA.  In this case, we created another SDE database on a new server with ArcGIS v9.3.  After running this conversion, some layers were still looking for the 9.1 connections which caused the mxd to hang when opening because the 9.1 database and server was now retired.  I was unable to locate the old connections, so I added a dummy domain name to route the connections with the old server name to the new server name.  This solved the problem, but the mxd still contains references to the old server.  The only way to fix this is to recreate the entire mxd (time consuming and we have hundreds of files like this).

It seems that the newly saved mxd might still have references to the old server which causes it to hang.  I guess saving it again outside of python removes these references as you are then able to open the mxd in a normal amount of time.

Do you have any 3rd party software that is used in this mxd?  This could be a cause of your problems.

Also, I would contact tech support and log an incident with them to see if the SaveACopy is supposed to flush out references to old servers.  I would be interested in the results as I will need to perform this operation on thousands of mxds when my organization migrates from Oracle Client 10g to either 11 or 12 depending upon when my organization is ready for this upgrade.
0 Kudos
FelixSvoboda
New Contributor
Thanks for your imput.
I'll do some further test (replace the layers datasource and then save the file) and contact the tech support about the SaveACopy issue. I'll update about the progress.

felix
0 Kudos
JeffBarrette
Esri Regular Contributor
There are a couple of statements I'd question.  I'd like to see if you have the same issue with the following, simplified code:

import arcpy
path = "XXX"
mxd = arcpy.mapping.MapDocument(path)
for df in arcpy.mapping.ListDataFrames(mxd):
  for lyr in arcpy.mapping.ListBrokenDataSources(mxd):
    arcpy.mapping.RemoveLayer(df, lyr)

newPath = "YYY"
mxd.saveACopy(newPath)


Also - what SP are you on?  We did make some changes to saveACopy in a past MXD.  I'll research this a little more.

Jeff
0 Kudos
FelixSvoboda
New Contributor
Thanks for your input Jeff.

I should have posted the more simplified code. But it doesn't solve the problem. I'll post a mxd where I removed one layer with broken data connection. It takes a while to open the empty mxd. If you save the project it opens with normal speed. I think Michael is right and the mxd still has references to the old server.

I tried it with SP 2 (Build 3200) & 3 (Build 3600).

felix
0 Kudos
MichaelVolz
Esteemed Contributor
Felix:

As a workaround you might want to write a script that just saves the file through python before running the BrokenLinks script.  Maybe the Save operation flushes out old SDE connections, but the SaveACopy operation does not.  Are the mxds that you are processing currently saved as v9.3 or earlier?  Or have these mxds been created or saved previously in v10?

I had some issues where I tried to run python scripts on v9.3 mxds where they crashed python in v10 and I was unable to open the mxd in v10.  If I save the file in v9.3 or I saved the mxd back to v9.3 in python, the mxd would then open in v10.  It seems that this Save operation in v9.3 was flushing out bad or corrupt objects for v10.
0 Kudos
PhilipGriffith
New Contributor III

I'm having the same problem in version 10.3.1. Did you ever figure out how to fix this problem?

0 Kudos
PhilipGriffith
New Contributor III

Never mind: found the solution on this post. You can get the code here. Or just work the logic into your script; it's not difficult.

0 Kudos