I made this happy little arcpy script to fix some data source problems:
print "Hi, I will fix find and replace file path sections on your behalf. \nGive me a moment to just count the number of MXDs I'll be looking at for you today..." import os import arcpy mxdfiles = [os.path.join(d, x) for d, dirs, files in os.walk(r"PATH") for x in files if x.endswith(".mxd")] print "\nOk, I'll be working through "+str(len(mxdfiles))+" MXDs. \nStarting this process now..." for item in mxdfiles: print "\nWorking on: "+item mxd = arcpy.mapping.MapDocument(item) mxd.findAndReplaceWorkspacePaths(r"PATH OLD1", r"PATH NEW") mxd.findAndReplaceWorkspacePaths(r"PATH OLD2", r"PATH NEW") mxd.findAndReplaceWorkspacePaths(r"PATH OLD3", r"PATH NEW") mxd.findAndReplaceWorkspacePaths(r"PATH OLD4", r"PATH NEW") mxd.findAndReplaceWorkspacePaths(r"PATH OLD5", r"PATH NEW2") mxd.save() del mxd print "Completed "+str(mxdfiles.index(item)+1)+" maps so far." print "\nProcess complete!"
Unfortunately, though, when it comes across a MXD with a Bing imagery layer, up pops a Bing Authorization box which you have to click 'OK' on (hitting Enter works, too). This is because my company no longer has a license to use Bing/Microsoft Virtual Earth, so this little box comes up every time a MXD is opened manually, or, it seems, when one is invoked by my script. This means I have to either:
- Find a way to programmatically click that button or ignore it.
- Leave a coffee mug on my Enter key overnight.
- Remove the Bing (or Microsoft Virtual Earth*) layers programmatically.
I Googled to no avail regarding option 1. Before going for option 2, I tried option 3. As a test, I used the following script (as this one didn't work) to try removing Microsoft Virtual Earth* layers (their group is Microsoft Virtual Earth, inside that there are three layers with the same name plus Hybrid, Aerial or Roads suffixed):
import arcpy mxd = arcpy.mapping.MapDocument(r"PATH") for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, "*Microsoft*", df): arcpy.mapping.RemoveLayer(df, lyr) print "Removed from "+ df.name+" dataframe: " +lyr.name mxd.save() del mxd, lyr, df
It ran without errors and removed the layers. The Bing Authorization dialog box pop up did not appear whilst the script ran. Only thing is, the pop up does appear when I open the map I just modified with that script - even though the layers are gone! Does anyone know why removing these layers with this script doesn't stop the pop up? I have tested removing these layers manually, saving, closing then reopening the MXD. The pop up does not appear then. So there is something done in the manual process which is not done in my script.
Curiously, when I open the offending MXD, run the above layer removal script using the Python window, then close and reopen the MXD the Bing Authorization pop up does not appear. Also, if I delete all other layers and tables from the MXD so it only has Microsoft Virtual Earth layers in it, then remove those remaining layers with the script, then open the MXD manually, the Bing Authorization pop up still appears :-\
Before I try to incorporate my script to remove these layers into the one I'm working on to walk through all MXDs and fix their paths, I need to know:
- What is causing the Bing Authorisation dialog box to pop up with my initial script, but not the layer deletion one?
- Why after running the Bing/Microsoft Virtual Earth layer deletion script the authorization pop up still appears?