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. The script works except for when run on MXDs that have a raster dataset in them. I have isolated the problem to the code below. Specifically the .findAndReplaceWorkspacePath() method seems to be failing when attempting to perform the operation on the raster layer.
fullPathToNewWorkspace = r"C:\GIS\New\Data" # for context, not actually used in script fullPathToOldWorkspace = r"C:\GIS\Old\Data" # for context, not actually used in script mxd = arcpy.mapping.MapDocument(r"Path\To\MXD") for df in arcpy.mapping.ListDataFrames(mxd): for lyr in arcpy.mapping.ListLayers(mxd, '', df): if lyr.supports("DATASOURCE"): print lyr.dataSource # added to help troubleshoot lyr.findAndReplaceWorkspacePath(r"Public", r"G-DRIVE") print lyr.dataSource # added to help troubleshoot
The troubleshooting print calls are as such:
> C:\GIS\Old\Data\VectorData1.shp > C:\GIS\New\Data\VectorData1.shp > C:\GIS\Old\Data\VectorData2.shp > C:\GIS\New\Data\VectorData2.shp > C:\GIS\Old\Data\RasterData # ArcGRID format > # No print output for C:\GIS\New\Data\RasterData
So the script is getting caught up on trying to .findAndReplaceWorkspacePath for the raster. Furthermore, when I remove the raster dataset from the mxd and rerun the same script it works fine.
This is the traceback I get from running it with the raster dataset in the MXD:
"C:\Users\Chaz\Desktop\TestDataMig\Scripts\DataMigration_v1.2_RasterTesting.py", line 1, in <module> import arcpy File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\_mapping.py", line 695, in findAndReplaceWorkspacePath return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True))) ValueError: Layer: Unexpected error
I was under the impression (ArcPy documentation) that raster datasets worked fine with the .findAndReplaceWorksapcePath method, yet I am still having these issues. Am I missing something? Do I need to phrase my arguments differently, or do I need to use a different method to replace a raster path?
- Windows 7
- ArcGIS/ArcPy 10.2
- Python 2.7.3