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 osimport arcpymxdfiles = [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:
import arcpymxd = arcpy.mapping.MapDocument(r"PATH.mxd")for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, "Microsoft Virtual Earth", df): arcpy.mapping.RemoveLayer(df, lyr)mxd.saveACopy(r"PATH2.mxd")del mxdIt ran without errors, including without the Bing Authorization dialog box popping up. The resulting MXD, however, would not open without crashing. I therefore used this script to retrieve its list of layers:
import arcpymxd = arcpy.mapping.MapDocument(r"PATH2.mxd")df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]print arcpy.mapping.ListLayers(mxd, "", df)del mxd...and found that the Microsoft Virtual Earth* layers had in fact been removed. Btw, the list layers script also did not cause the Bing Authorization box to pop up.
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:
أكثر...
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 osimport arcpymxdfiles = [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.
import arcpymxd = arcpy.mapping.MapDocument(r"PATH.mxd")for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, "Microsoft Virtual Earth", df): arcpy.mapping.RemoveLayer(df, lyr)mxd.saveACopy(r"PATH2.mxd")del mxdIt ran without errors, including without the Bing Authorization dialog box popping up. The resulting MXD, however, would not open without crashing. I therefore used this script to retrieve its list of layers:
import arcpymxd = arcpy.mapping.MapDocument(r"PATH2.mxd")df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]print arcpy.mapping.ListLayers(mxd, "", df)del mxd...and found that the Microsoft Virtual Earth* layers had in fact been removed. Btw, the list layers script also did not cause the Bing Authorization box to pop up.
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 nor listing ones?
- Why after running the Bing/Microsoft Virtual Earth layer deletion script the MXD is now corrupted and causes ArcMap to crash (crash report box comes up)?
أكثر...