Updating and fixing data sources with arcpy mapping
Need to update a path in your Arc Map project to reflect the new location? There are several methods for updating data sources using arcpy.mapping, check out this help topic from ESRI.
The example listed below is just one of several methods.
Environment: as opposed to the one for the individual layers.
Here is the help document for fixing data sources: Updating and fixing data sources with arcpy.mapping (arcpy.mapping) The first option would be to use the function as in the example below, to look through your If, on the other hand, you want to keep with your method where you cycle through the layers and only change specific ones, you may consider changing the code block to add an additional filter to check for the specific path you want to change, like below: full Path To New Workspace = r"C:\GIS\New\Data" # for context, not actually used in script full Path To Old Workspace = r"C:\GIS\Old\Data" # for context, not actually used in script mxd = arcpy.mapping. List Layers(mxd, '', df): if lyr.supports("DATASOURCE"): if Source == full Path To Old Workspace: print Source # added to help troubleshoot And Replace Workspace Path(full Path To Old Workspace, full Path To New Workspace) print Source # added to help troubleshoot statement to match the datasource prior to trying to change it, may very well take care of the problem you were having with the rasters, though it may in effect filter them out.
This should be relatively easy to do, after all, as ESRI has some pretty good information in their Resource Center here There were a few small challenges to get a script to process a bunch of map files located in various shared directories.
The example scripts in the Resource Center all involved changing a single file.
During a recent customer upgrade from ESRI 9.3 to 10SP3, we uncovered a last minute "gotcha" regarding over 1,000 map files floating around several share drives.With a little Python magic, we were able to process a whole directory tree at once by using the function.Situation: I am working on creating a Python script that will replace workspace paths for every layer in a map document in order to fix broken layers after a data migration.This seems reasonable at first, but requires the user to perform an additional task which they weren’t expecting to do.The second scenario is similar to the first, but to hire a temporary worker to open all the maps and fix the data source.
The script works except for when run on MXDs that have a raster dataset in them. Specifically the And Replace Workspace Path() method seems to be failing when attempting to perform the operation on the raster layer.