Change name of layer in TOC with arcpy.mp

1116
3
Jump to solution
10-03-2021 02:36 PM
ParkGIS307
New Contributor

Hello,

I am working on an automated map script that will allow me to import a Shapefile, assign it a pre-defined symbology, zoom to a layer and export a map. I am having one issue when I bring in my input file. I am using the addDataFromPath() function to bring in my data, and it indeed brings it in fine. However, the file shows up in my table of contents and legend with the text "_Layer2", "_Layer3" etc, after the name of the file I bring it. 

I would like to simply rename what is in my legend and TOC to something I want such as "Project Area" or "AOI", etc. 

Does anyone here have the experience with arcpy.mp to get that to work? 

 

Thank you for your time! 

0 Kudos
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

Hi @ParkGIS307 ,

You can use the following code snippet to rename feature layers in TOC using arcpy:

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
for lyr in map.listLayers():
    print(lyr.name)
    # Here this renames a layer that has a keyword "Building" in it to "BuildingFootprint" in TOC
    if "Building" in lyr.name:
        layerName = str(lyr.name)
        lyr.name = lyr.name.replace(layerName, "BuildingFootprint")
        print(f"Layer renamed to: {lyr.name}")

this can be extended to more layers.

I hope that is insightful.

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

"name" in the CIM layer class is read/write

Layer—ArcGIS Pro | Documentation

did you try it?


... sort of retired...
0 Kudos
ParkGIS307
New Contributor

My knowledge on CIM syntax is scant. So I have not tried it yet. I will read up on it more though. 

0 Kudos
MehdiPira1
Esri Contributor

Hi @ParkGIS307 ,

You can use the following code snippet to rename feature layers in TOC using arcpy:

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]
for lyr in map.listLayers():
    print(lyr.name)
    # Here this renames a layer that has a keyword "Building" in it to "BuildingFootprint" in TOC
    if "Building" in lyr.name:
        layerName = str(lyr.name)
        lyr.name = lyr.name.replace(layerName, "BuildingFootprint")
        print(f"Layer renamed to: {lyr.name}")

this can be extended to more layers.

I hope that is insightful.