AnsweredAssumed Answered

lyr.replaceDataSource Question

Question asked by cwholmes on Jun 16, 2017
Latest reply on Jun 21, 2017 by cwholmes

Hello, I have the following script:

import arcpy
fileno = arcpy.GetParameterAsText(0)
#Define the path of the mxd
mxd_file = 'o:/plan/subdiv/' + fileno + '/' + fileno + '-ortho_photo.mxd'
arcpy.AddMessage('fileno: ' + fileno + ' mxd_file: ' + mxd_file)
mxd = arcpy.mapping.MapDocument(mxd_file)
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == 'roads':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','roads')
    elif lyr.name == 'railways':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','railways')
    elif lyr.name == 'rivers':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','rivers')
    elif lyr.name == 'lakes':
        lyr.replaceDataSource('X:/GIS/geo_databases/labelling_features.gdb','FILEGDB_WORKSPACE','lakes')
    elif lyr.name == 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon':
        lyr.replaceDataSource('O:/plan/subdiv/' + fileno + '/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')
#Remove layer from ortho_photo.mxd
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if lyr.name == 'site_polygon':
            arcpy.mapping.RemoveLayer(df, lyr)
mxd.save()
del mxd
Result = True
arcpy.SetParameterAsText(1,Result)

At line 17 I get the following error when executing:

 

 

Traceback (most recent call last):

File "X:\GIS\LUAM\DEV\Scripts\OtherTypes\FixDataSourcesOP_Orthophoto.py", line 17, in <module>

lyr.replaceDataSource('O:/plan/subdiv/LOC2018-0001/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')

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 680, in replaceDataSource

return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))

ValueError: Layer: Unexpected error

 

The layer 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon' is part of a feature dataset called 'data', so I don't know if that may have anything to do with it? Although, I did read that if you are using this command with a feature dataset to just reference the geodatabase and it should work fine.

 

Right now I'm at a bit of a loss regarding this "Unexpected error". Any ideas appreciated. Thanks!

Outcomes