import arcpy import os rootdir = r"C:Your_Root_Folder" for subdir, dirs, files in os.walk(rootdir): for filename in files: fullpath = os.path.join(subdir, filename) print filename if os.path.isfile(fullpath): basename, extension = os.path.splitext(fullpath) if extension.lower() == ".mxd": mxd = arcpy.mapping.MapDocument(fullpath) # Temporarily remove the imagery layer(s) for df in arcpy.mapping.ListDataFrames(mxd): ImageLayerList = arcpy.mapping.ListLayers(mxd, "*USGS EROS*", df) for lyr in ImageLayerList: arcpy.mapping.RemoveLayer(df, lyr); # Search-and-replace to fix up data source paths. mxd.findAndReplaceWorkspacePaths(r"oldpath", r"newpath", False) # Add the imagery layers back in for lyr in ImageLayerList: arcpy.mapping.AddLayer(df, lyr, "BOTTOM") mxd.save() del lyr del df del mxd
I figured out how to work around this. You must temporarily remove the imagery layer from the mxd object. Then apply the findAndReplaceWorkspacePaths function(s). And finally restore the imagery layer you removed. Here is some sample code that temporarily removes any layer with a name that contains "USGS EROS", changes a path, and adds the imagery layer back in. import arcpy import os rootdir = r"C:Your_Root_Folder" for subdir, dirs, files in os.walk(rootdir): for filename in files: fullpath = os.path.join(subdir, filename) print filename if os.path.isfile(fullpath): basename, extension = os.path.splitext(fullpath) if extension.lower() == ".mxd": mxd = arcpy.mapping.MapDocument(fullpath) # Temporarily remove the imagery layer(s) for df in arcpy.mapping.ListDataFrames(mxd): ImageLayerList = arcpy.mapping.ListLayers(mxd, "*USGS EROS*", df) for lyr in ImageLayerList: arcpy.mapping.RemoveLayer(df, lyr); # Search-and-replace to fix up data source paths. mxd.findAndReplaceWorkspacePaths(r"oldpath", r"newpath", False) # Add the imagery layers back in for lyr in ImageLayerList: arcpy.mapping.AddLayer(df, lyr, "BOTTOM") mxd.save() del lyr del df del mxd
for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"):
I just confirmed this on a 10.0 SP3 machine. Image Server layers no longer show up as layers that support the lyr.support("DATASOURCE") property.Before attempting to change a data source, check to see if this property is true. for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): If you are still having a problem, please send me a map package so I can test with your data.
for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): If you are still having a problem, please send me a map package so I can test with your data.
for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE") and lyr.supports("DATASETNAME"):
Sweet! Thank you that works well! Steve
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.