7

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:

  1. Find a way to programmatically click that button or ignore it.
  2. Leave a coffee mug on my Enter key overnight.
  3. 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:

  1. What is causing the Bing Authorisation dialog box to pop up with my initial script, but not the layer deletion one?
  2. Why after running the Bing/Microsoft Virtual Earth layer deletion script the authorization pop up still appears?
  • 1
    Well, this is not for the faint of heart, but...what happens if you just get rid of SetBingKey.exe (typically found in C:\Program Files (x86)\ArcGIS\Desktop10.x\bin)? Maybe you could move it out of that directory and take another crack, moving it back if all hell breaks loose. – wchatx Aug 12 '15 at 19:32
  • Ok. I moved it out of the directory and tried to manually open a map which had Bing layers. The authorization window still popped up. I then tried my script. The authorization window popped up as well. Thanks for trying though :-) – ndthl Aug 16 '15 at 5:10
  • Someone also mentioned trying turning off the Bing/Microsoft Virtual Earth layers to avoid making the authorisation box appear. This did not work. – ndthl Aug 16 '15 at 7:12
1

Try this code:

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.Delete_management(lyr)
        arcpy.mapping.RemoveLayer(df, lyr)
        print "Removed from "+ df.name+" dataframe: " +lyr.name
        mxd.save()
del mxd, lyr, df 
  • Thanks for trying, this did remove the Microsoft* layers but the pop up still appeared when I opened the .MXD afterward. The script also threw the following error: Traceback (most recent call last): File "S:\path.py", line 9, in <module> del mxd, lyr, df NameError: name 'lyr' is not defined – ndthl Aug 23 '15 at 4:39
  • Esri support basically suggested saving it as a copy to remove the problem. This works, but is clumsy because I'll have double the number of .MXDs. – ndthl Sep 1 '15 at 4:23
1

Esri support got in touch with me and said to simply save a copy rather than save the .MXD itself. Further: "...when Bing layers are added to a MXD a global property is created and referenced. Even when those layers are removed the MXD maintains the reference. When saving the MXD as a copy (when Bing layers are not present) the reference is not copied. Which is why we are not observing pop-ups within the copy." They gave no explanation how to remove the global property.

Taking this approach obviously creates many MXD copies. Might want to set your script to overwrite the originals.

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.