Synthèse
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"):
Propriétés
Propriété | Explication | Type de données |
expression (Lecture et écriture) | A layer's individual label class expression using either a VBScript, JScript, or Python parser associated with the label class. | String |
name (Lecture et écriture) | A layer's individual label class name. | String |
SQLQuery (Lecture et écriture) | A layer's individual label class SQLQuery. This is useful for restricting labels to certain features. | String |
visible (Lecture et écriture) | If True, the label class is visible. If False, it is not visible. | Boolean |
Vue d’ensemble des méthodes
Méthode | Explication |
getDefinition (cim_version) | Returns a label classes' CIM definition. |
setDefinition (definition_object) | Sets a label classes' CIM definition. |
Méthodes
getDefinition (cim_version)
Paramètre | Explication | Type de données |
cim_version | A string that represents the major version of the CIM that will be used.
| String |
Type de données | Explication |
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)
Paramètre | Explication | Type de données |
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.
Exemple de code
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
Vous avez un commentaire à formuler concernant cette rubrique ?