Changing layer datasources

382
4
Jump to solution
11-08-2017 11:48 PM
GinoMellino
Occasional Contributor

Hi All, 

I have an MXD which contains a large number of layers that originally all linked to featureclasses within a single file geodatabase. I have since needed to move some of the data into another file geodatabase. My thinking was that after I moved the data I could use arcpy to iterate the layers, pick up any with broken sources and change their data source to target the new geodatabase. I have the code below which I adapted from HERE to suit my needs but it throws the error below below in red. I'm sure it is something simple but can anyone tell me what I am doing wrong here? The name printed by line 7 (i.e. State Electorates) is that or a featureclass so i don't see why it wouldn't work?

Many thanks in advance. 

Gino

import arcpy
mxd = arcpy.mapping.MapDocument(r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\Copy_of_Internal_Geodatabase_ArcGIS10.3(1).mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        if lyr.isBroken:
            if "DYNAMIC_DATA.gdb" in lyr.dataSource:
                print lyr.name
                lyr.findAndReplaceWorkspacePath(lyr.workspacePath, r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\STAND_ALONE_DATA.gdb")
mxd.saveACopy(r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\Updated_Internal_Geodatabase_ArcGIS10.3.mxd")
del mxd

>>>
State Electorates
Traceback (most recent call last):
File "E:\_GINO's\Planning Geodatabase\Geodatabase Management\Script\updateDS.py", line 8, in <module>
lyr.findAndReplaceWorkspacePath(lyr.workspacePath, r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\STAND_ALONE_DATA.gdb")
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\_mapping.py", line 696, in findAndReplaceWorkspacePath
return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error
>>>

0 Kudos
1 Solution

Accepted Solutions
GinoMellino
Occasional Contributor

Thank you for the reply. I removed all white space/special characters from the paths but it didn't fix the issue. I did manage to find the solution after a re read of the web page in my original post - I had to update line 8 to set the Validate property to False (see code below).  

Cheers, 

Gino

lyr.findAndReplaceWorkspacePath(lyr.workspacePath, r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\STAND_ALONE_DATA.gdb",False)

View solution in original post

4 Replies
JayantaPoddar
MVP Esteemed Contributor

Please avoid use of any special character (except underscore) in pathname/filename.



Think Location
GinoMellino
Occasional Contributor

Thank you for the reply. I removed all white space/special characters from the paths but it didn't fix the issue. I did manage to find the solution after a re read of the web page in my original post - I had to update line 8 to set the Validate property to False (see code below).  

Cheers, 

Gino

lyr.findAndReplaceWorkspacePath(lyr.workspacePath, r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\STAND_ALONE_DATA.gdb",False)
JayantaPoddar
MVP Esteemed Contributor

Thanks for sharing the solution.



Think Location
0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Although you have found your solution, I posted an addin a while back that may help in the future

/blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...