LabelClass

Resumen

Provides access to a layer's label class properties.

Debate

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"):
If this statement is True, you will be able to modify all label class properties.

Propiedades

PropiedadExplicaciónTipo de datos
expression
(Lectura y escritura)

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

String
name
(Lectura y escritura)

A layer's individual label class name.

String
SQLQuery
(Lectura y escritura)

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

String
visible
(Lectura y escritura)

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

Boolean

Descripción general del método

MétodoExplicación
getDefinition (cim_version)

Returns a label classes' CIM definition.

setDefinition (definition_object)

Sets a label classes' CIM definition.

Métodos

getDefinition (cim_version)
ParámetroExplicaciónTipo de datos
cim_version

A string that represents the major version of the CIM that will be used.

  • V2The 2.x version of the CIM will be used.
  • V3The 3.x version of the CIM will be used.
String
Valor de retorno
Tipo de datosExplicación
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)
ParámetroExplicaciónTipo de datos
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.

Muestra de código

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
LabelClass example 3

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