replaceDataSource() function

1339
1
02-09-2017 11:42 AM
JaredPilbeam1
Occasional Contributor

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. 

0 Kudos
1 Reply
JaredPilbeam1
Occasional Contributor

To answer my own question:

I just changed a few things around from line 19 down of the above script. And then it worked.

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 " + f
            for brknItem in arcpy.mapping.ListBrokenDataSources(mxd):
                print "\t broken layer: " + brknItem.name
# iterate workspace and update layer to new directory
                if brknItem.supports('DATASOURCE'):
                    if brknItem.dataSource == r'Z:\Jared\WillCoGIS_DataSHP.gdb\Trans_StreetAnno_Gray_C':
                        brknItem.replaceDataSource(r'\\gisfile\GISstaff\Jared\WillCoGIS_DataSHP.gdb', 'FILEGDB_WORKSPACE', 'Street_Anno_Gray_C')
                        #brknItem = 'Hydro_WillCounty_Waterways'
                        print "\t layer source successfully changed "
                                  
        mxd.save()
del brknItem
del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Now I'm having trouble turning this into a script tool. Usually, I don't have a problem getting the GetParameterAsText function to work. But line 20 (above) has the workspace path, workspace type and the layer. And I only know how to use one variable for the GetParameterAsText function? Is there a way around this?

import arcpy
import os
from arcpy import env
arcpy.env.overwriteOutput = True

# set workspace
Workspace = arcpy.GetParameterAsText(0)
Old_LayerSource = arcpy.GetParameterAsText(1)
New_LayerSource = arcpy.GetParameterAsText(2)

# iterate workspace and list broken layers
for root, dirs, files in os.walk(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 "\t current broken layer name is " + brknItem.name
# iterate workspace and update layer to new directory
                if brknItem.supports('DATASOURCE'):
                    if brknItem.dataSource == Old_LayerSource:
                        brknItem.dataSource = New_LayerSource
                        print "\t new layer source is " + brknItem.dataSource‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here's the script tool menu with the parameters filled out followed by the error (in the error, "Line 30" is line 22 here-- brknItem.dataSource = New_LayerSource):

0 Kudos