lundi 29 décembre 2014

Error when running Python script to replaceDataSource of multiple layers in multiple mxd's


I looked on all of your sites and did not find a solution to my problem.

Almost all of our data has been copied to an SDE and now most of the layers in all of the mxd's need to be redirected to the new location before the old data is deleted.

The old data is a mixture--shapefiles, gdb, etc. and it is located directly in the oldLayerLocation, or in subfolders of that location.


I only want to change the data source for layers pointing to the oldLayerLocation and its subfolders, as some people may have developed their own data which they are storing elsewhere.


I am open to other ways of getting this done, if this is not the best way.


I tried using mxd.replaceWorkspaces, but the results were not enough (only changed items that were located directly in the oldLayerLocation, but not in subfolders below that), and too aggressive (changed everything that I wanted to, but also changed layers that did not point to oldLayerLocation).


I am running ArcGIS 10.2.2, editing with PythonWin 2.7,


and the error that I am getting is "ValueError: Layer: Unexpected error" referring to the line containing the lyr.replaceDataSource.


`newLayerLocation = r"X:\Path to new data.sde"



oldLayerLocation = r"X:\Path_to_old_non-SDE_data"
MXDlocation = r"Z:\Where_the_MXDs_are_located"
Suffix1 = r".mxd"
Suffix2 = r"_SDE.mxd"

import arcpy, os

for dirpath, dirnames, filenames in arcpy.da.Walk(MXDlocation, True, None, True, None, None):
for filename in filenames:
if filename.lower().endswith(Suffix1):
mxdPath = os.path.join(dirpath, filename)
mxd = arcpy.mapping.MapDocument(mxdPath)
for df in arcpy.mapping.ListDataFrames(mxd):
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
if lyr.supports("DATASOURCE"):
if lyr.dataSource.startswith(oldLayerLocation) == True:
newMXDname = mxdPath.replace(Suffix1, Suffix2)
lyr.replaceDataSource(newLayerLocation, "SDE_WORKSPACE")
mxd.saveACopy(newMXDname)
del mxd


`





Aucun commentaire:

Enregistrer un commentaire