I am having issues with lyr.replaceDataSource, I have an mxd that has layers with symbol that i need to just update the layers after some process are ran, there is a few layers i need to update but for some reason it only replace the first lyr.replaceDataSource but after the first one it removes the layer from the TOC, then it adds it back.
The script runs and updates both layers fine if i remove both arcpy.MultipleRingBuffer_analysis (lines 20&21), but when i add it back it does what i described.
Any help would be gratefully appreciated.
import arcpy, os arcpy.env.overwriteOutput = True mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0] arcpy.env.workspace = os.path.dirname(mxd.filePath) wp = os.path.dirname(mxd.filePath) SP = "SUBJECT_PROPERTY" for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): if lyr.name == "SUBJECT_PROPERTY": lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, "") del mxd, lyr arcpy.MultipleRingBuffer_analysis(SP, "1_2",[1,2],"Miles","ToBufDist","ALL" ) arcpy.MultipleRingBuffer_analysis(SP, "NOTIFBUF1",[600],"Feet","ToBufDist","ALL") mxd = arcpy.mapping.MapDocument("CURRENT") lyr = arcpy.mapping.ListLayers(mxd, "NOTIFBUF1")[0] NF = "NOTIFBUF1" for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): if lyr.name == "NOTIFBUF1": lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", NF, "") #mxd = arcpy.mapping.MapDocument("CURRENT") #lyr = arcpy.mapping.ListLayers(mxd, "NOTIFBUF1")[0] #lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", "NOTIFBUF1", True) arcpy.RefreshActiveView() arcpy.RefreshTOC()
replaceDataSource (workspace_path, workspace_type, {dataset_name}, {validate}) | Replaces a data source for a layer in a map document (.mxd) or layer (.lyr) file. It also provides the ability to switch workspace types (e.g., replaces a file geodatabase data source with an SDE data source). |
When i first open my mxd the layers "Subject_property" & "NOTIFBUFI" data source are not there, it has a red exclamation point. I run the script which creates the "Subject_property" & "NOTIFBUFI" to the mxd folder, then i been trying to use the lyr.replaceDataSource process to set the data source to the newly created "Subject_property" & "NOTIFBUFI". I guess i am confused i should be using something else and why does It works if i remove the two arcpy.MultipleRingBuffer_analysis
If it's only the one layer, it's easiest to click the red exclamation point and navigate to where the data now lives to repair the broken link.
The mxd template has four layers that need the datasource updated. I just put two on the script i posted cuz i was trying to figure out what was going on.
If they are all in the same workspace fixing one by clicking the exclamation mark, will fix all. Typically this happens when you move or delete data.
Here is some more reading material on broken data links.
27425 - Use the 'Set Data Source' tool for map documents in ArcCatalog
I understand how to do it through ArcCatalog and manually, i was trying to do this trough python. The "Subject_property" needs to be updated before the "NOTIFBUF1" because of the processes.
if i ran the script in ArcMap python window i had to use the arcpy.Delete_management to delete the "NOTIFBUF1" right after the MultipleRingBuffer_analysis becaue it would add a new "NOTIFBUF1". if i didn't i would remove the broken layer from mxd. I ran the same script in a tool script and i did not have to use the Delete_management because it did not remove the layer from the mxd and it updated the broken layer just fine. Interesting findings.