0

I've put together a script that for the most part works as intended - to walk through a series of folders, find SDE layer files and update their data source to direct connects if the layers support workspacepath. However I've noticed that in some cases it is not finding specific layer files within folders. Here is the script:

import arcpy
import os
from arcpy import env

env.overwriteOutput = True
TheLayerDir = r"G:\GIS\Layers"
NewDataPath = r"G:\GIS\Data\vector_direct.sde"

for dirname, dirnames, filenames in os.walk(TheLayerDir):

    for filename in filenames:
        newFilename =  os.path.join(dirname, filename)
        desc = arcpy.Describe(os.path.join(dirname, filename))

    if newFilename[-4:] == '.lyr':
            lyrFile = arcpy.mapping.Layer(newFilename)

            for lyr in arcpy.mapping.ListLayers(lyrFile):
                if not lyr.isGroupLayer:

                    if lyr.supports("WORKSPACEPATH") == True:

                        if "sde" in desc.dataElement.catalogPath and lyr.workspacePath != NewDataPath:                         
                            print "------------------------------"
                            print "Layer: "        + lyr.name
                            print "Old Data Source: "   + lyr.workspacePath
                            lyr.replaceDataSource(NewDataPath,"SDE_WORKSPACE")
                            print "New Data Source: "   + lyr.workspacePath
                            lyrFile.save()

                        elif "sde" in desc.dataElement.catalogPath and lyr.workspacePath ==  NewDataPath:
                            print "------------------------------"
                            print "Layer: " + lyr.name + " has already been updated with path, skipping..."

                        else:
                            print "------------------------------"
                            print "Layer: " + lyr.name + " is not a SDE Layer"
                    else:

                        if "sde" in desc.dataElement.catalogPath:
                            print "------------------------------"
                            print "Layer: "        + lyr.name
                            print "Layer does not support workspacePath"                

print "Script Complete! - That's all Folks!" 

If I for example substitude the following code as follows:

 if newFilename[-4:] == '.lyr':

with

if extension.lower() == ".lyr":

then the script will successfully identify all layer files within a folder, however without declaring 'newFilename'.

1

It looks like you're doing a little extra work, and it is telling that it works better when you force lowercase. Maybe some of the layer files end in ".Lyr" or something strange like that.

Ultimately, you are doing two list processes, the first loop with filename and then the second listing with ListLayers.

At any rate, I'd recommend simplifying to the following. You can also use the continue statement to filter things out during an iteration and reduce the amount of indenting:

for dirname, dirnames, filenames in os.walk(TheLayerDir):

    for filename in filenames:
        newFilename =  os.path.join(dirname, filename)

        if not newFilename.lower().endswith(".lyr"):
            continue

        desc = arcpy.Describe(newFilename)
        lyr = arcpy.mapping.Layer(newFilename)

        if lyr.isGroupLayer:
            continue

        if lyr.supports("WORKSPACEPATH") == True:
            if "sde" in desc.dataElement.catalogPath and lyr.workspacePath != NewDataPath:                         
                #etc
            elif "sde" in desc.dataElement.catalogPath and lyr.workspacePath ==  NewDataPath:
                #etc
            else:
                #etc
       else:
           if "sde" in desc.dataElement.catalogPath:
                #etc 
  • Many thanks for the quick reply and helpful solution, I'll ensure to make good use of the continue statement in future, less indenting is always good (especially if you need to print a script). – cofan Mar 23 '15 at 19:35
  • Occasionally it bombs out with "ValueError: Layer: Unexpected error" when attempting to update the data source on an SDE Layer. – cofan Mar 23 '15 at 20:15
  • hmm, that's interesting. I don't have any experience working with SDE layers, but this may be helpful gis.stackexchange.com/questions/41635/… Try removing the ".sde" from the NewDataPath or, maybe better, in the specific block that had trouble use os.path.splitext(NewDataPath)[0]. os.path.splitext turns a path string into a tuple where the first item is everything up to the "." and the second item is the ".xxx" extension (you could also use that function to find the ".lyr" files...) – mr.adam Mar 23 '15 at 20:56
  • Thanks - tried your suggestions with splitext and removal of .sde in the path but still receive same error. I noticed in the link they were omitting '.shp', not sure if that would make a difference (to .sde). I've noticed there has been a few people receiving the same error with "lyr.replaceDataSource" – cofan Mar 23 '15 at 21:30
  • well worth a shot, it must act a little differently for sde paths than shapefiles. It does look like a problematic process overall... The only other suggestion I have would be to make new layers and use the existing ones to apply symbology. A longer, potentially sloppier process, but it may be a workaround you have to use. – mr.adam Mar 23 '15 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.