Is there something wrong with lyr.replaceDataSource ?

611
12
Jump to solution
03-12-2018 01:47 PM
AnnaBuchheit
New Contributor III

I have been racking my brain trying to figure out why I can't get this simple code to execute properly.  I am trying to replace Datasources for numerous layers in numerous mxd's.  I really don't want to do this manually!  Essentially, a new file gdb has been created in a new folder with renamed feature classes.  Feature class names were changed to simplify other geoprocessing scripts. Now I need to re-link the datasources to the new datasets.

 I have a working code:  it walks through a directory, it lists mxd's by file name, it finds layers and prints their names and finally saves the mxd.  there are no bugs; however, when I open the mxd, the datasource has not changed.  The script runs through the lyr.replaceDataSource but does not actually replace the datasource.  I have used the validate {FALSE} parameter and still no luck!  I am not a whiz when it comes to python, but I thought this was a pretty straight forward script. 

Does anyone see what I am doing wrong?  What am I overlooking?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

With version 10.5, I was having some luck using something like:

lyr.replaceDataSource(r"L:\Data\CFX_2018_180214\gdb\CFX_2018.gdb","FILEGDB_WORKSPACE","BB_Points", True)

# omitting 'IFS_Working' from the path
# L:\Data\CFX_2018_180214\gdb\CFX_2018.gdb\IFS_Working\BB_Points‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

It seems strange that the feature class name would be omitted from the path or layer name.  This could cause problem if other feature classes  in the file geodatabase contain an identical feature name.  Could it be a bug?

Replacing the data source did not change the layer name, so that had to be set.  Also, the old symbology was retained, so that may also need to be corrected.

Also, I was working inside ArcMap's Python window and would use arcpy.RefreshActiveView to update the map.

View solution in original post

12 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

do you have a  

mxd.save()

anywhere in your script?   

Edit:  never mind...just saw it.    But I thik it needs to be indented.  Looks like you would only be saving the last mxd changed.  But haven't looked closely at your code (short on time).

I have an addin if you are interested in using, or just reviewing the code.

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

AnnaBuchheit
New Contributor III

Thank you for the reply, 

I have tested the code using only one mxd and still nothing changes.  I'm stumped!

0 Kudos
MichaelVolz
Esteemed Contributor

I think you might need a mxd.SaveACopy and then use file manipulation to rename the newly created mxd to the original mxd name.

0 Kudos
AnnaBuchheit
New Contributor III

thanks, I'll give it a try

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Anna

From the help....http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/updatingandfixingdatasources.htm

This seems to be used... worth a whirl

mxd.saveACopy(r"C:\Project\Project2.mxd")

del mxd

In the future, actual pop the code in using syntax highlighting instead of an image, it might help if people want to actually

try it without retyping

Code Formatting... the Basics ++

AnnaBuchheit
New Contributor III

Thanks for the tip Re: inserting code... this is my first post

0 Kudos
RandyBurton
MVP Regular Contributor

Wondering about line 12 in your script:

for lyr in arcpy.mapping.List(mxd):

Should it be (ListLayers):

for lyr in arcpy.mapping.ListLayers(mxd):
MichaelVolz
Esteemed Contributor

If you have multiple dataframes in your mxd, you will need to loop through them as well, as I believe the current code will only resource layers in the active dataframe.  Not sure this applies to you, but something to consider.

AnnaBuchheit
New Contributor III

thanks,

no I only have one dataframe, but I do have multiple Feature Datasets

...I have added to my thread below...

0 Kudos