Select to view content in your preferred language

How to label features using arcpy?

9706
6
07-09-2017 07:08 AM
FarhanMustafa
Emerging Contributor

I am working on a mapbook and I require to label the features against different fields in different maps. How can I do this using arcpy?

Regards

0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Have you read LabelClass—Help | ArcGIS Desktop ?  If so, are you having a problem with any specific aspect of working with labels through ArcPy?

XanderBakker
Esri Esteemed Contributor

I don't think the LabelClass—Help | ArcGIS Desktop provides access to set the field used for labeling a layer. If that is the case, you could create layerfiles with the appropriate label settings and apply these layerfiles for the different maps using UpdateLayer—Help | ArcGIS Desktop . Or duplicate the layer in your TOC with the different label settings and switch them on and off depending on the map using the visible property of the Layer—Help | ArcGIS Desktop . 

JoshuaBixby
MVP Esteemed Contributor

The LabelClass object does provide access to change the field through the expression property.  When the label class is 'Default', the expression is simply the name of the field to label.  If the user updates the expression to a different field and refresh the active view, the labels get updated.

XanderBakker
Esri Esteemed Contributor

Very true! good catch! +1 for that...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It is the symbology that is tougher to work with, but that is slightly improved with ArcGIS Pro compared to ArcMap.

RandyBurton
MVP Alum

Regarding labels, here's some python snippets that I've been playing around with that may be of interest:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        print "\n\nLayer name: " + lyr.name
        print " Show Labels: " + str(lyr.showLabels)
        print "   Data Set: " + lyr.datasetName
        print "Data Source: " + lyr.dataSource
        for lblClass in lyr.labelClasses:
            print "\n       Class Name:  " + lblClass.className
            print "Show Class Labels:  " + str(lblClass.showClassLabels)
            print "       Expression:  " + lblClass.expression
            print "        SQL Query:  " + lblClass.SQLQuery

'''
Sample output:

Layer name: MyData
 Show Labels: True
   Data Set: FeatureTest
Data Source: C:\...\MyFeature.gdb\FeatureTest
       Class Name:  Default
Show Class Labels:  True
       Expression:  def FindLabel ( [FeatureType] ):
     if [FeatureType] == "Type One":
        return "Type 1"
     else:
         return [FeatureType]
        SQL Query:  FeatureType = '1234'
'''‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("LABELCLASSES"):
        for lblClass in lyr.labelClasses:
            lblClass.SQLQuery = ""
arcpy.RefreshActiveView()

# removes query that was found in first snippet‍‍‍‍‍‍

mxd = arcpy.mapping.MapDocument("CURRENT")
LayerList = ['Program']
for lyr in arcpy.mapping.ListLayers(mxd):
  if lyr.supports("LABELCLASSES"):
    if lyr.name in LayerList:
      lyr.showLabels = True
    else:
      lyr.showLabels = False
        
arcpy.RefreshActiveView()

I recommend checking the links that both bixb0012‌ and xander_bakker‌ have mentioned.