AnsweredAssumed Answered

lyr.replaceDataSource

Question asked by CCweedcontrol on Aug 27, 2015
Latest reply on Aug 28, 2015 by CCweedcontrol

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()

Outcomes