lyr.replaceDataSource Question

732
4
06-16-2017 01:08 PM
ChrisHolmes
Occasional Contributor III

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!

0 Kudos
4 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

I'm not near a machine where I can test, but a couple thoughts.

first, you may want to set a variable and print out  'O:/plan/subdiv/' + fileno + '/data_for_item.gdb'

im also wondering if the path is too long?  Can you test by renaming to a short name and test?

i have an addin /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...‌  that might help you.  

ChrisHolmes
Occasional Contributor III

Hi Rebecca, when setting the path to a variable it does print out correctly. Also, the long path does not have issues in other scripts.

I`m wondering if this could have to do with the fact that I have a different mxd open when this script is run which references the `ortho photo mxd`. Both mxds reference the same data_for_item.gdb, wondering if it could be some sort of locking issue.

I'm thinking maybe I'll move this script and have it run when the user has the ortho photo mxd open instead. Couple extra mouse clicks for the user, but probably cleaner in the end.

Thanks

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

can you print out your getparameterastext(0) from line 1 and 17, to check to see if it is path compliant

ChrisHolmes
Occasional Contributor III

Hi Dan, They both print the same and are correct. Thanks for the input.

Chris

0 Kudos