HOW TO
When data servers are upgraded or replaced, maps with layers that reference data on those systems may need to be updated to refer to data on a different system. The following is an example of a script to replace data sources of raster layers in maps residing in many different subfolders.
As an example, suppose the data sources for all color images were located in:
<drive>:<folder tree>/images/color
<drive>:<folder tree>/images
Note:
If this example does not match the file organization in the prevailing environment, the functions illustrated may be helpful in developing a custom workflow.
Code:
import arcpy, os
MapMainFolder = arcpy.GetParameterAsText(0) # topmost folder of maps to be updated
NewDataPath = arcpy.GetParameterAsText(1) # folder to which the raster data sources have been moved
for (root, dirs, files) in os.walk (MapMainFolder):
for fileName in files:
if os.path.splitext (fileName)[1] == ".mxd":
arcpy.AddMessage (fileName)
fullPath = os.path.join (root, fileName)
mxd = arcpy.mapping.MapDocument (fullPath)
rasterLayersUpdated = 0
for layer in arcpy.mapping.ListLayers (mxd): # list all layers in all dataframes
if layer.isRasterLayer:
if layer.supports ("SERVICEPROPERTIES"):
if layers.serviceProperties ['ServiceType'] == "ImageServer":
pass # don't try to re-path an online basemap
dataSrc = layer.dataSource
Color = 0
dataRoot = os.path.dirname (dataSrc)
if dataRoot.find ("Color") > 0:
newPath = os.path.join (NewDataPath, "Color")
else: # B&W
newPath = NewDataPath
layer.replaceDataSource (newPath, "RASTER_WORKSPACE", os.path.basename (dataSrc))
rasterLayersUpdated += 1
if rasterLayersUpdated > 0:
mxd.save()
Get help from ArcGIS experts
Download the Esri Support App