Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Change layer source

$
0
0
I am working on a script that will walk a directory and change the source for a layer. This way when we move data I can just set this script to go update everyone's maps. In my own folder I can get it to run down to printing the layer source, I just can't get the replaceDataSource function to work. Any help? This is in 10.1

Also will I be able to replace a source to something that is loaded from a group or basemap layer?

Code:

#import site-packages, modules and set enviroment
import arcpy, os

#initialize variables
extList = ["mxd"]
mappaths = []

targetDir = r"Z:\Working\MXDs"
oldPath = r"Z:\Working\MXDs\Export_Output.shp"
newPath = r"Z:\Working\Export_Output.shp"


#Loop through each MXD in the folder and writes path to a list
for root, dirs, files in os.walk(targetDir):
    for filename in files:
        splitFilename = filename.split(".")
        if splitFilename[-1] in extList:
            fullpath = os.path.join(root, filename)
            mappaths.append(fullpath)

#prints list
for maps in mappaths:
    print maps
    mxd = arcpy.mapping.MapDocument(maps)
    for layer in arcpy.mapping.ListLayers(mxd):
        if layer.supports("DATASOURCE"):
            if layer.dataSource == oldPath:
                print "Layer: " + layer.name + "\n  Source: " + layer.dataSource
##                layer.replaceDataSource(newPath, "NONE")

print "End Script"

Results when running the code:
>>>
Z:\Working\MXDs\BrkLyr_Map_2.mxd
Layer: Export_Output
Source: \Working\MXDs\Export_Output.shp
Traceback (most recent call last):
File "Z:\Utilities\Python\Wes PYSCRIPTS\Tests\FindBrokenLayers_2for.py", line 41, in <module>
layer.replaceDataSource(newPath, "NONE")
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource
return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error

Viewing all articles
Browse latest Browse all 2485

Trending Articles