AnsweredAssumed Answered

replaceDataSource() function

Question asked by jared_pilbeam on Feb 9, 2017
Latest reply on Mar 1, 2017 by jared_pilbeam

Hello,

 

I'm attempting to loop through mxds in a folder replacing any broken layer sources. The script below successfully loops through the entire folder and prints out all the print statements, but it's not replacing the data source. It seems as though the syntax is correct, however?

 

The code below attempts to replace the source data of 'WillCounty_HYDRO_River_Creek'. As you can see it ran through the whole code, but the path in the replaceDataSource() function on line 20 seemingly didn't  work as it was neither printed nor changed in the Layer Properties window (rather, it was changed to S:\Jared...). 

 

I've included a PrtScn of the Layer Properties window of the layer with the broken source. You'll notice on the Source tab the feature class is called 'Hydro_WillCounty_Waterways', but the same layer in the TOC is called 'WillCounty_HYDRO_River_Creek'.  I've tried both to no avail.

 

# set modules
import arcpy
import os
from arcpy import env
arcpy.env.overwriteOutput = True

# set workspace
env.workspace = r"\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_17\New folder"

# iterate workspace and list broken layers
for root, dirs, files in os.walk(env.workspace):
    for f in files:
        if f.endswith(".mxd"):
            mxd = arcpy.mapping.MapDocument(os.path.join(root, f))
            print "current map being checked is " + mxd.filePath
            for brknItem in arcpy.mapping.ListBrokenDataSources(mxd):
                print "current broken layer name is " + brknItem.name
# iterate workspace and update layer to new directory
                if brknItem.name == 'WillCounty_HYDRO_River_Creek':
                    brknItem.replaceDataSource(r'\\gisfile\GISstaff\Jared\WillCoGIS_DataSHP.gdb', 'FILEGDB_WORKSPACE')
                    print "new layer source is " + brknItem.dataSource
                                 
        mxd.save()
del brknItem
del mxd

 

 

 

 

Any help would be appreciated. 

Outcomes