Summary
References a layer file (.lyr or .lyrx) stored on disk.
Discussion
Layer files authored with ArcGIS Desktop have a .lyr extension and layer files created with ArcGIS Pro have a .lyrx extension. There is a notable difference. Older .lyr files only store a single layer or a single group layer at the root level although a group layer can have multiple layers or group layers within it. Newer .lyrx files can store multiple layers and/or group layers at the root level. Therefore, a reference to a layer file will be a reference to a list of layers.
The ArcGIS Pro application can read legacy .lyr file types, but it can only save to .lyrx file types. If a .lyr file type is being referenced the save method will not work, you must use the saveACopy method instead.
For a more complete discussion on working with layers, refer to the Layer Class help topic.
Syntax
LayerFile (layer_file_path)
Parameter | Explanation | Data Type |
layer_file_path | A string that includes the full system path and file name of an existing layer file. | String |
Code sample
The following script references a layer file and inserts a single layer named Ranger Stations into a map above an existing layer named Points of Interest.
import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
insertLyr = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\LayerFiles\Ranger Stations.lyrx")
m = aprx.listMaps("Yosemite National Park")[0]
refLyr = m.listLayers("Points of Interest")[0]
m.insertLayer(refLyr, insertLyr, "BEFORE")
aprx.saveACopy(r"C:\Projects\YosemiteNP\Yosemite_updated.aprx")