We have a new database connection and all our MXDs have to be updated. I ran a script to update the layers on a bunch of MXDs. Then I opened a map, right clicked a layer, properties, then checked under the source tab to see if the changes took place. But, the layer was still referencing the old connection. Is there a snippet I could include in this script, or something else, that verifies that the layers have indeed been updated?
There is a print statement in the script, but that just prints the name of the MXD. It doesn't really give concrete proof.
import arcpy
import os
def find_and_replace(MXD_workspace, oldPath, newPath):
arcpy.env.workspace = MXD_workspace
path = arcpy.env.workspace
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith(".mxd"):
mxd_name = name
fullpath = os.path.join(root,name)
print mxd_name
print fullpath
mxd = arcpy.mapping.MapDocument(fullpath)
for df in arcpy.mapping.ListDataFrames(mxd):
for flayer in arcpy.mapping.ListLayers(mxd, "*", df):
if flayer.isFeatureLayer or flayer.isRasterLayer:
try:
mxd.findAndReplaceWorkspacePaths(oldPath, newPath, False)
print "Repaired the path for " + name
except:
print(mxd_name + " cannot replace paths")
mxd.save()
del mxd
print "complete..."
if __name__ == '__main__':
find_and_replace(r"\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_18\CountyBoard",
r"Database Connections\BonnScott.sde",
r"Database Connections\gisEddy.gissql.sde") #(path2MXDs, path2oldGDB, path2newGDB)
Solved! Go to Solution.
Thanks for the help.
@Dan I put mxd.save() after both try and except, but no difference.
@michael I attempted saveACopy, but still no change in the data source.
From what I read on Stack Exchange the findAndReplaceWorkspacePaths() function doesn't always work. I couldn't get it to work in three different scripts:
ArcGIS 10.2: ArcPy - how to replace workspace paths? - Geographic Information Systems Stack Exchange
arcmap - ArcPy Changing Spatial Database Connection - Geographic Information Systems Stack Exchange
Finally, I used this script, which has replaceWorkspaces(). And it worked.
import os, arcpy
folderPath = r"\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_18\Test Folder"
for fileName in [x for x in os.listdir(folderPath) if os.path.splitext(x)[1] == ".mxd"]:
fullPath = os.path.join(folderPath, fileName)
if os.path.isfile(fullPath):
mxd = arcpy.mapping.MapDocument(fullPath)
print "MXD: " + fileName
arcpy.env.workspace = fullPath
mxd.replaceWorkspaces("", "NONE", r"Database Connections\pathtoSDE.sde","SDE_WORKSPACE")
mxd.save()
del mxd
else:
print "error! {0} failed to be replaced".format(fullPath)
print "+++successfully changed data sources+++"
didn't the other print statements produce anything? (ie line 21, 23
Dan,
Line 21 never printed. But, that's OK because every layer points to the same "BonnScott.sde" connection. I think this would have printed if the connection had a name different from BonnScott.
Yes, line 23 did print. Sorry, I should have mentioned that the script ran fine with no errors.
I am having a hard time figuring out whether the mxd.save is in the right spot … I would be tempted to error on the side of overkill to save it in the try except block to see if one works at least
I would do a SaveAs in python. Then delete the original mxd and rename the SaveAs mxd to the original filename.
Thanks for the help.
@Dan I put mxd.save() after both try and except, but no difference.
@michael I attempted saveACopy, but still no change in the data source.
From what I read on Stack Exchange the findAndReplaceWorkspacePaths() function doesn't always work. I couldn't get it to work in three different scripts:
ArcGIS 10.2: ArcPy - how to replace workspace paths? - Geographic Information Systems Stack Exchange
arcmap - ArcPy Changing Spatial Database Connection - Geographic Information Systems Stack Exchange
Finally, I used this script, which has replaceWorkspaces(). And it worked.
import os, arcpy
folderPath = r"\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_18\Test Folder"
for fileName in [x for x in os.listdir(folderPath) if os.path.splitext(x)[1] == ".mxd"]:
fullPath = os.path.join(folderPath, fileName)
if os.path.isfile(fullPath):
mxd = arcpy.mapping.MapDocument(fullPath)
print "MXD: " + fileName
arcpy.env.workspace = fullPath
mxd.replaceWorkspaces("", "NONE", r"Database Connections\pathtoSDE.sde","SDE_WORKSPACE")
mxd.save()
del mxd
else:
print "error! {0} failed to be replaced".format(fullPath)
print "+++successfully changed data sources+++"