Replacing "_" with " " for classLabels using Python

4974
5
Jump to solution
01-07-2015 02:57 PM
DerekNelson
New Contributor II

Head banging against a wall...

 

I have figured how to do this for layer name. The Help Docs say it is possible to change classLabel but don't show any examples and I can't find anyone who has successfully done this through Python. I am sure the answer lies in arcpy.mapping and UniqueValuesSymbology but cannot figure it. I have 10-15 different layers, all using unique_values for symbology, consisting of about 15-20 different classes in each. Each class value and therefore label uses an "_" instead of a space as is common practice for GIS data. For legend and TOC purposes, I am trying to figure a script that can quickly manipulate and change the classLabels in Layer Properties.

 

Any ideas would be wonderful...thanks!

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

For inserting scripts, i.e., syntax highlighting, you need to use the advanced editor that is accessed from a link in the upper-right corner of the reply window/frame.

Try the following:

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]:
        labels = lyr.symbology.classBreakLabels
        for i, label in enumerate(labels):
            labels = label.replace("_", " ")
        lyr.symbology.classBreakLabels = labels
    elif lyr.symbologyType in ["UNIQUE_VALUES"]:
        labels = lyr.symbology.classLabels
        for i, label in enumerate(labels):
            labels = label.replace("_", " ")
        lyr.symbology.classLabels = labels

  • Since the class labels are properties and return a list, you need to "get" the lists, which is what I am doing in lines 03 and 08.
  • Since the labels are stored in a list, you need to iterate over the list and do the text replace/substitution on each element of the list.  Lines 04-05 and 09-10.
    • There are several ways to handle this part.  One could create a new list, using a for loop or list comprehension  I chose to edit the existing list in place, using a for loop and an enumerate object.
  • After the label updates are done, you need to "set" the properties to the new or updated lists, which is what I am doing in lines 06 and 11.

View solution in original post

0 Kudos
5 Replies
JoshuaBixby
MVP Esteemed Contributor

The following code from the interactive Python window in ArcMap should help get you going:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]:
        print lyr.symbology.classBreakLabels
    elif lyr.symbologyType in ["UNIQUE_VALUES"]:
        print lyr.symbology.classLabels

The classBreakLabels and classLabels properties are read-writeable and store lists.  After you get the list, update the contents, and set the properties again; you will need to run arcpy.RefreshActiveView() to see the changes.

0 Kudos
DerekNelson
New Contributor II

Joshua,

So it did grab and create the list from the available classLabels. How do I then go about replacing the underscores with spaces though. You say "update the contents, and set the properties again", what does that mean? I am an admitted Python amateur and quite frankly still lost. This is where I'm at (couldn't figure out how to add script like you did to the discussion):

Capture.JPG

Again...thanks for any advice!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

For inserting scripts, i.e., syntax highlighting, you need to use the advanced editor that is accessed from a link in the upper-right corner of the reply window/frame.

Try the following:

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.symbologyType in ["GRADUATED_COLORS", "GRADUATED_SYMBOLS"]:
        labels = lyr.symbology.classBreakLabels
        for i, label in enumerate(labels):
            labels = label.replace("_", " ")
        lyr.symbology.classBreakLabels = labels
    elif lyr.symbologyType in ["UNIQUE_VALUES"]:
        labels = lyr.symbology.classLabels
        for i, label in enumerate(labels):
            labels = label.replace("_", " ")
        lyr.symbology.classLabels = labels

  • Since the class labels are properties and return a list, you need to "get" the lists, which is what I am doing in lines 03 and 08.
  • Since the labels are stored in a list, you need to iterate over the list and do the text replace/substitution on each element of the list.  Lines 04-05 and 09-10.
    • There are several ways to handle this part.  One could create a new list, using a for loop or list comprehension  I chose to edit the existing list in place, using a for loop and an enumerate object.
  • After the label updates are done, you need to "set" the properties to the new or updated lists, which is what I am doing in lines 06 and 11.
0 Kudos
DerekNelson
New Contributor II

Joshua,

You are the MAN! Had to substitute some single quotes for the doubles you had in the replace areas, added it to my script for renaming the layers with a "arcpy.RefreshTOC()" and presto! Thanks so much for the help!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Your welcome.  Keeping plugging away at learning Python, I know I have really enjoyed learning and applying it, there is so much you can do with it.  Cheers....

0 Kudos