Labeling the Third Word

This is a continuation of my previous post about using the Field Calculator to calc a field to a word position in another field. My whole purpose of the exercise was to use the new field for labeling. But then I thought why create yet another field when you can use what you already have and do the same thing in a labeling expression? So …

labeling1

I really don’t want the text “Census Tract” in my labels, just the tract numbers. Using Python in a labeling expression should do the trick. First I open the properties of my census tract layer in ArcMap 10.1, click on the Labels tab, and next to my Label Field, click on the Expression button.

labeling2

Note in the Label Expression window that my current expression is just the field name NAMELSAD10. What I want to do is strip out the “Census Tract” text that all the records have and just label with the tract name, which is the thrid word in the field’s value. So using the .split() function, similar to my previous post, I build the following expression using the Python parser (make sure to check the Advanced box to use multiple lines of code in your expression):

labeling3

So what is going on here? When you use multiple lines of Python code, you are required to use a function called FindLabel. This function is used by ArcMap to label your features after the code is run. When you click on the Advanced box in the menu, the fucntion is setup for you in it’s basic form, which is just labeling the feature with the value in the selected field. I added code between the first line and the last line that sets a variable named L to the Python code that takes the value in the NAMELSAD10 field, splits out the words, and extracts the third word. Remember in Python position 0 would be the first word and position 2 would be the third word. Also note that I modified the last line with return, using L instead of the field name. The function will return the value of L, which will be set to the thrid word that I want, which would be the tract number. Press the OK button and you will see the result.

labeling4

Just like before, but without having to create a new field!

Most coders like to code with as few lines as possible. You could do the same thing by removing the second line setting the L variable, and just manipulate the field value on the return line like so:

labeling5

Here you cut out the middle man, or cut out setting the L variable, and just executed the .split() function on the return line. Easy and efficient!

Let’s back up a bit. What if I did want the “Census Tract” text in my label, but I wanted each word stacked? That can be done by doing this:

labeling6

I set three variables, one for each word, then set the final L variable for the labeling. The ‘\n’ represents a new line character, or for us humans a carriage return to start a new line.  Using this Python expression results in the following labels:

labeling7

Now that you know a few of these tricks, you can start doing some fancy labeling of your own data! -mike

3 thoughts on “Labeling the Third Word

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s