0

I have a folder of .lyr files and I want to change the source on all of them from one file geodatabase to a different file geodatabase. Feature dataset and feature class names will remain the same in both geodatabases. There are a few layerfiles that are grouped layers and there may be times when the FC doesn't exist when the script is run but will be there in the future which is why I have validated False. I'm a beginner at using Python but this is what I've managed to throw together based on my research.

Using the Python window in ArcMap I've managed to change data source on the .lyr files that are not group layers with the code below, but I cannot get it to work in a .pyt toolbox where I hoped to set up parameters for the Layer File folder and the new geodatabase. The script runs in the .pyt tool box but I get no messages and the layerfiles are not updated.

I also cannot figure out what I need to do to change the data source on the group layers.

Not sure if it matters but this is on ArcMap 10.5.1 run in a Citrix environment.

For the grouped layers: I've tried using glyr = arcpy.mapping.Layer(lyr) with error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "e:\arcgis\desktop10.5\arcpy\arcpy\arcobjects\mixins.py", line 428, in __init__
    raise TypeError(repr(type(lyrfile)))
TypeError: <class 'arcpy._mapping.Layer'>

and glyr = arcpy.mapping.ListLayers(lyr) with error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 9, in <module>
AttributeError: 'list' object has no attribute 'name'

Python code that works in the ArcMap Python window:

import os
for dirname, dirnames, filenames in os.walk(r"T:\Project\TestLayerFiles"):
    for filename in filenames:
        newFileName = os.path.join(dirname, filename)
        if not newFileName.lower().endswith(".lyr"):
            continue
        lyr = arcpy.mapping.Layer(newFileName)
        if lyr.isGroupLayer:
            continue
        if lyr.supports("WORKSPACEPATH") == True:
            print("DS :" + lyr.dataSource)
                lyr.findAndReplaceWorkspacePath(r"T:\North\ProjectA\projectA.gdb", r"T:\North\ProjectB\projectB.gdb",{False})

  • Welcome to GIS SE! We're a little different from other sites. We're a Q&A site, not a discussion forum. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. Please check out our short tour for more about how the site works. Thanks. – PolyGeo Aug 30 at 22:54
  • At the moment you have presented a code snippet that works in the Python window so that is useful as background. What you have not shown us is a code snippet that leads to one or other of your errors. You say you are trying to write a Python Toolbox tool, but you seem to be stuck on a much simpler code snippet than that. I think you should focus on getting this working in a standalone Python script first, and then absorbing that into a Python Toolbox tool later. – PolyGeo Aug 30 at 22:57
  • Reviewing these earlier Q&As may also be useful: gis.stackexchange.com/questions/tagged/… – PolyGeo Aug 30 at 23:01

Your Answer

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

Browse other questions tagged or ask your own question.