Summary
Provides access to a layer's label class properties.
Discussion
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 existing LabelClass objects. The createLabelClass method on the Layer object returns a reference to a new LabelClass.
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"):
Properties
Property | Explanation | Data Type |
expression (Read and Write) | A layer's individual label class expression using either a VBScript, JScript, or Python parser associated with the label class. | String |
name (Read and Write) | A layer's individual label class name. | String |
SQLQuery (Read and Write) | A layer's individual label class SQLQuery. This is useful for restricting labels to certain features. | String |
visible (Read and Write) | If True, the label class is visible. If False, it is not visible. | Boolean |
Method Overview
Method | Explanation |
getDefinition (cim_version) | Returns a label classes' CIM definition. |
setDefinition (definition_object) | Sets a label classes' CIM definition. |
Methods
getDefinition (cim_version)
Parameter | Explanation | Data Type |
cim_version | A string that represents the major version of the CIM that will be used.
| String |
Data Type | Explanation |
Object | Returns the CIM definition for a LabelClass. |
For more information about working with the CIM and samples, see Python CIM access.
setDefinition (definition_object)
Parameter | Explanation | Data Type |
definition_object | A modified CIM definition object originally retrieved using getDefinition. | Object |
For more information about working with the CIM and samples, see Python CIM access.
Code sample
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
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
The following script creates two new label classes using Python and Arcade language options.
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
l = m.listLayers('GreatLakes')[0]
#Enable layer labels but toggle off all existing label classes
l.showLabels = True
for lc in l.listLabelClasses():
lc.visible = False
#Create new Label classes and toggle those on
lc1 = l.createLabelClass(name = 'Small Lakes - Python',
expression = '[AREA]',
sql_query = 'AREA < 10000',
labelclass_language = 'Python')
lc1.visible = True
lc2 = l.createLabelClass(name = 'Large Lakes - Arcade',
expression = '$feature.AREA',
sql_query = 'AREA > 10000',
labelclass_language = 'Arcade')
lc2.visible = True