LabelClass

Zusammenfassung

Provides access to a layer's label class properties.

Diskussion

The LabelClass object is used for managing labeling properties such as label expressions or SQL queries that are associated with a layer's individual label classes. The listLabelClasses method on the Layer object will return a list of LabelClass objects.

Not all layers support labeling, so it is useful to test this ahead of time using the supports method on the Layer object. For example:

if lyr.supports("SHOWLABELS"):
If this statement is True, you will be able to modify all label class properties.

Eigenschaften

EigenschaftErläuterungDatentyp
expression
(Lesen und schreiben)

A layer's individual label class expression using either a VBScript, JScript, or Python parser associated with the label class.

String
name
(Lesen und schreiben)

A layer's individual label class name.

String
SQLQuery
(Lesen und schreiben)

A layer's individual label class SQLQuery. This is useful for restricting labels to certain features.

String
visible
(Lesen und schreiben)

If True, the label class is visible. If False, it is not visible.

Boolean

Codebeispiel

LabelClass example 1

The following script will print the label class properties for only those layers that have labels turned on and supports the showLabels property.

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]

for lyr in m.listLayers():
    if lyr.supports("SHOWLABELS"):
        if lyr.showLabels:
            print("Layer name: " + lyr.name)
            for lblClass in lyr.listLabelClasses():
                print("\t Class Name: \t" + lblClass.name)
                print("\t Expression: \t" + lblClass.expression)
                print("\t SQL Query:  \t" + lblClass.SQLQuery)
del aprx
LabelClass example 2

The following script finds a layer named Points of Interest and changes the label class properties for a particular label class named Summit so that not all of the summits are labeled. Only those over 2,000 meters in elevation are labeled.

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]

lyr = m.listLayers("Points of Interest")[0]

if lyr.supports("SHOWLABELS"):
  lblClass = lyr.listLabelClasses("Summit")[0]
  lblClass.expression = "Name"
  lblClass.SQLQuery = "Type_ = 'summit' And Elevation > 2000"
  lblClass.visible = True

aprx.save()
del aprx