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?

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Feb 13, 2020 at 17:36

1 Answer 1

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()
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered May 15, 2020 at 22:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.