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:
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:
I really don't understand what am I doing wrong.
1 Answer 1
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.
-
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?newGIS– newGIS2018年05月27日 13:43:33 +00:00Commented May 27, 2018 at 13:43
-
You've twice missed the point. The layer description is the name you give it.Vince– Vince2018年05月27日 14:12:49 +00:00Commented May 27, 2018 at 14:12
-
i understand that. i just asking if it possible to change the layer name as i need ?newGIS– newGIS2018年05月27日 14:16:58 +00:00Commented May 27, 2018 at 14:16
-
That would be a different question.Vince– Vince2018年05月27日 14:28:40 +00:00Commented May 27, 2018 at 14:28