Get the layer ID within a map with Python

1132
4
Jump to solution
07-11-2022 10:39 AM
Labels (2)
FrancisMoisan1
New Contributor II

Hi!, I need to get the layer id within a map in ArcGIS Pro.  It need to be done with Python, but can't find the way to do it.  Any idea ? Impossible ? 

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
HannesZiegler
Esri Contributor

Ah I see, yes you can get that:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyrs = m.listLayers()
for lyr in lyrs:
    print(lyr.getDefinition("V3").serviceLayerID)

Have a look at CIM access, it's quite useful, there is much more you can do with it besides this

View solution in original post

4 Replies
HannesZiegler
Esri Contributor

Take a look at arcpy.mp, I think this gets you what you want:

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyrs = m.listLayers()
[(i, lyr.name) for i, lyr in enumerate(lyrs)]

returns

[(0, 'TestLine'), (1, 'TestPoint'), (2, 'TestPoint_Raster'), (3, 'TestPoly'), (4, 'TestPoint'), (5, 'World Topographic Map'), (6, 'World Hillshade')]

 

0 Kudos
FrancisMoisan1
New Contributor II

Thanks for the info, but that "id" is just the order within the project.  I look to get the ID that is attached to the layer within the properties.

image_id_couche_layer_prop.png

0 Kudos
HannesZiegler
Esri Contributor

Ah I see, yes you can get that:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyrs = m.listLayers()
for lyr in lyrs:
    print(lyr.getDefinition("V3").serviceLayerID)

Have a look at CIM access, it's quite useful, there is much more you can do with it besides this

FrancisMoisan1
New Contributor II

That was it!  thanks!

0 Kudos