2

I have a tool that is meant to clip and reproject every layer in an open .mxd, and then create a new feature class and .lyr file with original symbology from the result, not adding either to the .mxd. I would like the new .lyr file to have the same display name as the original, but when I write it this way it removes the original .lyr from the map document, and also fails on importing symbology.

I'm using ArcGIS 10.5.

Is is possible to create a new .lyr with the same name as the open .mxd .lyr, then import symbology, and not have it crash remove the original .mxd .lyr?

My current workaround is to append "_clip" to the arcpy.MakeFeatureLayer_management out_layer parameter as follows:

if not arcpy.Exists(lyr_outname):
    if not os.path.exists(out_lyr_fldr):
        os.makedirs(out_lyr_fldr)
    arcpy.AddMessage("Creating .lyr file: {}\n".format(lyr_outname))
    lyr = arcpy.MakeFeatureLayer_management(fc_outname, i.name + "_clip")
    arcpy.ApplySymbologyFromLayer_management(lyr, i)
    arcpy.SaveToLayerFile_management(lyr, lyr_outname)

This works, but people don't want the "_clip" in the display name.

If I try it using the original name:

if not arcpy.Exists(lyr_outname):
    if not os.path.exists(out_lyr_fldr):
        os.makedirs(out_lyr_fldr)
    arcpy.AddMessage("Creating .lyr file: {}\n".format(lyr_outname))
    lyr = arcpy.MakeFeatureLayer_management(fc_outname, i.name)
    arcpy.ApplySymbologyFromLayer_management(lyr, i)
    arcpy.SaveToLayerFile_management(lyr, lyr_outname)

it first removes the layer its working on from the .mxd, and then crashes on import symbology.

Failed to execute. Parameters are not valid.
ERROR 000968: The symbol layer does not match the input layer
Failed to execute (ApplySymbologyFromLayer).

If there is no direct way to achieve this, I thought maybe I can just search the output folder and change everything after it has been run. I can see the display name using describe nameString:

workspace = r"C:\..."
lyrs = []

walk = arcpy.da.Walk(workspace)

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        lyrs.append(os.path.join(dirpath, filename))


for lyr in lyrs:
    desc = arcpy.Describe(lyr)

    if "_clip" in desc.nameString[-5:]:
        print desc.nameString

But is there a way to change the nameString?

1

Using this answer to Change nameString using arcpy? I was able to solve the problem.

In order to not remove the original layer from the Table of Contents it does need to be created with a different name. In my case I just appended "_clip."

You can then change the nameString in the following way:

desc = arcpy.Describe(lyr_outname)

if "_clip" in desc.nameString[-5:]:
    lyr_in = arcpy.mapping.Layer(lyr_outname)
    lyr_in.name = desc.nameString[:-5]
    lyr_in.save()
| improve this answer | |

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.