1

I am trying to convert several shapefiles (in folder G:\desktop\Project\lyr) into layer files using this python code:

import arcpy,os
from arcpy import env
env.workspace = r"G:\desktop\Project\lyr"
env.overwriteOutput = True
for files in arcpy.ListFeatureClasses():
 filesPath = arcpy.Describe(files).catalogPath
 arcpy.MakeFeatureLayer_management( filesPath,"lyrName")
 arcpy.SaveToLayerFile_management("lyrName",os.path.splitext(filesPath)[0] + ".lyr")

the code work fine, and all the shapefiles converted into layer files with the same name of the shapefiles:

enter image description here

But when I load those layers files into the MXD file, all the layers have the same name, whereas I expected each layer will have a specific name:

enter image description here

I really don't understand what am I doing wrong.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked May 27, 2018 at 13:13

1 Answer 1

4

Your MakeFeatureLayer line names all your layers as "lyrName". The second parameter of that tool needs to be given values that are unique for each layer.

If you don't uniquely name your layers, you have no idea what ArcGIS will do with requests to access a layer by name.

import arcpy,os 
from arcpy import env 
env.workspace = r"G:\desktop\Project\lyr"
env.overwriteOutput = True
for files in arcpy.ListFeatureClasses(): 
 filesPath = arcpy.Describe(files).catalogPath
 lyrName = os.path.splitext(filesPath)[0]
 arcpy.MakeFeatureLayer_management(filesPath,lyrName)
 arcpy.SaveToLayerFile_management(lyrName,lyrName + ".lyr")

There is no correspondence between the source file name, layer name, and layerfile name except as you declare them.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered May 27, 2018 at 13:35
4
  • Thanks Vince, but i get this result: layer1, layer2, layer3- whereas i need the original names in the table of content: frame, line, point3hp- how can i get this result? Commented May 27, 2018 at 13:43
  • You've twice missed the point. The layer description is the name you give it. Commented May 27, 2018 at 14:12
  • i understand that. i just asking if it possible to change the layer name as i need ? Commented May 27, 2018 at 14:16
  • That would be a different question. Commented May 27, 2018 at 14:28

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.