2

How do I get a layer´s label from the TOC?

I can´t find a similar question and ArcGIS Help tells nothing about it. I have searched even in other knowledge bases but with no success.

I need to get a layer´s label (not its description) to be able to work with it further. enter image description here

Getting a layer´s name is easy but getting its label that appears in the TOC not.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
    layer_name = lyr.name
    layer_label = ????     # there is nothing like lyr.label
  • I would investigate the LegendElement class: desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/… – PolyGeo May 22 at 9:22
  • I had found these threads before I asked the question but unfortunately none of them is answering the question. The closest to the topic is one of them about setting UniqueValuesSymbology (arcpy.mapping) but it is still about setting, not about retrieving the labels. – jonlew May 22 at 9:39
  • Well, I see that working with UniqueValuesSymbology is also about retrieving layer´s labels, as explained by @Hornbydd in the accepted answer! – jonlew May 22 at 9:49
3

I don't believe you can access that text because you are using a Single Symbol. If you read the help file on layer objects in the arcpy mapping module a property of a layer is symbologyType which will return OTHER for single symbol and that is not a supported type. If you were using Unique Value, a supported type, then you would be able to access those text with the following code:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
    if lyr.name == "polys":
        # Found layer
        print(lyr.symbologyType)
        sym = lyr.symbology
        for l in sym.classLabels:
            print(l)

I strongly suspect you could access the text of a single symbol if you used ArcObjects but it is clear you are seeking a python solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.