Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

`

share|improve this question
    
Would you be able to include print statements before lyr.replaceDataSource to show the values of variables Suffix1, Suffix2 and oldLayerLocation when you see the error, please? Also, can you edit your question to show the exact error (including line number), exact output and exact code snippet run for your next test, please? –  PolyGeo Dec 29 '14 at 23:38
    
This has been cross-posted to Stack Overflow: stackoverflow.com/q/27691066/820534 The inadvisability of doing this is discussed at meta.gis.stackexchange.com/questions/3802/… –  PolyGeo Dec 30 '14 at 4:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.