Select to view content in your preferred language

Layer.saveACopy() failing for .LYR file

608
2
07-05-2011 04:31 PM
MalcolmParnell2
Deactivated User
From here, the Layer class Provides access to layer properties and methods. It can either reference layers in a map document (.mxd) or layers in a layer (.lyr) file.

I am trying to update an existing layer file (.lyr), replacing the data sources and saving to a new file.

The code below works fine if the source document is an mxd, but fails catastrophically (kills python) if it is a lyr file. (The lyr file was saved from the mxd.)


[INDENT]import arcpy 
sourceType = ".mxd" # Change this to .lyr and it fails.

fullpath = r"D:\WD\InterimGroundwater\Salinity" + sourceType 
sourceLyr = arcpy.mapping.MapDocument(fullpath) 

ID = "10014087" 

newAquiferBoundary = "ID_" + ID + "_AquiferBoundary" 
newLyr = r"D:\WD\InterimGroundwater\Salinity_" + ID + sourceType 

lyrList = arcpy.mapping.ListLayers(sourceLyr) 
for lyr in lyrList: 
if lyr.supports("DATASOURCE"): 
if lyr.name == "AquiferBoundary" : 
print lyr.dataSource 
lyr.replaceDataSource(r"D:\\WD\\InterimGroundwater\\AHGF_GW_v1-0_master.gdb", "FILEGDB_WORKSPACE", newAquiferBoundary) 
sourceLyr.saveACopy(newLyr)[/INDENT]


Am I doing something wrong or is this a bug?

I'm running SP2, Build3200.

Thanks.
Tags (2)
0 Kudos
2 Replies
EricaSmith
Deactivated User
I think your problem is with your source layer declaration. (sourceLyr = arcpy.mapping.MapDocument(fullpath)) In this statement, you need to be declaring a map document (mxd) and so that's why when you change the extension to .lyr, it crashes.

I'd suggest:
mxd = arcpy.mapping.MapDocument(fullpath)
sourceLyr = arcpy.mapping.ListLayers(mxd, "name of layer", "data frame")[0]
and then carry on with declaring the ID and the new layer, etc.
0 Kudos
MalcolmParnell2
Deactivated User
I think your problem is with your source layer declaration. (sourceLyr = arcpy.mapping.MapDocument(fullpath)) In this statement, you need to be declaring a map document (mxd) and so that's why when you change the extension to .lyr, it crashes.

I'd suggest:
mxd = arcpy.mapping.MapDocument(fullpath)
sourceLyr = arcpy.mapping.ListLayers(mxd, "name of layer", "data frame")[0]
and then carry on with declaring the ID and the new layer, etc.


Thanks Erica, but I think you're talking about a layer within an MXD?
I was wanting to create a copy of a .LYR files stored on disk.
0 Kudos