I want to partially change the path for layers in an mxd from a letter drive to a UNC path.
The help page for updating and fixing datasources with arcpy states "...allows you to substitute an entire or partial string for a layer..." but I cannot figure out how to partially replace a path. I have an mxd with one layer that points to W:\Data\City\Planimetric\xyz_planimetric_data_2011.mdb
This doesn't change the path.
import arcpy
mxd = arcpy.mapping.MapDocument(r'C:\GIS\test\AppServerConnections.mxd')
for lyr in arcpy.mapping.ListLayers(mxd):
wp = lyr.workspacePath
if 'W:' in wp:
nwp = r"\\\gisserver2" + wp[2:]
mxd.findAndReplaceWorkspacePaths(wp,nwp)
mxd.saveACopy(r"c:\gis\test\test2.mxd")
del mxd
if I substitute add a print statement here
if 'W:' in wp:
nwp = r"\\gisserver2" + wp[2:]
print nwp
it prints as I would expect: \\gisserver2\Data\City\Planimetric\xyz_planimetric_data_2011.mdb
What am I missing?
try lyr.findAndReplaceWorkspacePath(wp,nwp) instead of mxd, you are changing mxd workspace path, not the layer.
Edit: had Paths, not Path
That throws an error from the mapping module.
Sorry I copy pasted from your post, but for a layer its lyr.findAndReplaceWorkspacePath, not Paths like in the mxd version. Did you change that as well?
Yes. Still getting errors.
Ah okay I got it now, since you are replacing only a partial path, you only need to change the part of the path that needs changing, not the whole path.
Example:
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
if lyr.dataSource == r"C:\Project\Data\Parcels.gdb\MapIndex":
lyr.findAndReplaceWorkspacePath(r"Data", r"Data2")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd
In the example, it finds "Data" and replaces with "Data2", not giving a whole new path.
So you are having it find the part of the path string that needs changing, in your case, from W:\ to \\gisserver2
Try this out
if 'W:' in wp:
wp = "W:"
nwp = r"\\\gisserver2"
lyr.findAndReplaceWorkspacePath(wp,nwp)
I had success earlier today updating the SDE connection files using mxd.replaceWorkspaces. I was trying to do something similar for datasources on a network drive by trying to replace the drive letter with the UNC path. When I try to use lyr in my earlier code (I know I'm using a different method) I get the same or similar ValueError.
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("WORKSPACEPATH"):
wp = lyr.workspacePath
if '@RockGIS2' or 'RockGIS' in wp:
print wp
#mxd.replaceWorkspaces("", "NONE", r"\\gisserver2\GIS_Resources\DirectConnect_ArcCatalog_ConnectionFiles\gisuser@rockgis2.sde","SDE_WORKSPACE")
lyr.findAndReplaceWorkspacePath(wp, r"\\gisserver2\GIS_Resources\DirectConnect_ArcCatalog_ConnectionFiles\gisuser@RockGIS2.sde")
if '@RockRaster' in wp:
Replace Workspaces replaces the entire string path and type, findandReplaceWorkspacepath, finds the first string parameter in your workspace path , and replaces it with the second string, you aren't taking an entire path and replacing it with a new one.
All you are trying to do is replace your lettered drive "W:" or whatever with a network path r"\\gisserver2" or whatever.
Right. I'm trying to partially replace a string in the path. As I said in my original post the help guide states that it is possible to do partial strings. I need some help figuring out how to do that.