Map Typography – A Python Label Expression
Recently, I had to label parcels with APN & owner information. The parcel feature class has the APNs and the ownership table is joined based on the APNs. I’ve been commiting to using python in labeling expressions whenever possible and this was a good chance to use an advanced expression.
One problem was that the owner in the join table was all caps, which I planned to convert to title case in the expression. I also planned to add formatting tags. A lot of the owners include an & in the owner name. I used python to convert the & to & in order to display correctly with the use of formatting tags.
Based on the data in the table, I want the APN labeled, in a bold typeface and black. I want the Assessee on a new line, smaller, grey and in italics.
In the Label Expression window, set the parser to Python and check the Advanced checkbox.
Here’s the code:
1 2 3 4 5 6 7 8 9 | def FindLabel ([Parcels_MontereyCounty2013.APN],[Parcel_data.Assessee]): apn = [Parcels_MontereyCounty2013.APN] own = [Parcel_data.Assessee] if own == None: L = apn +"\n"+"<fnt size='6'><ita><clr black='70'>"+"Unknown"+"</clr></ita></fnt>" elif own != None: ownT=own.title() L = apn +"\n"+"<fnt size='6'><ita><clr black='70'>"+ownT.replace("&","&")+"</clr></ita></fnt>" return L |
Here’s the resulting label:
The first line defines the fields being used in the label.
1 | def FindLabel ([Parcels_MontereyCounty2013.APN],[Parcel_data.Assessee]): |
The next two lines define the variables apn & own to the corresponding fields in the joined parcel dataset.
1 2 | apn = [Parcels_MontereyCounty2013.APN] own = [Parcel_data.Assessee] |
The if statement catches any <null> assessee’s and defines the label (variable L) as the APN number, then a new line (“\n”) then the word “Unknown” in fnt size 6 at 70% black. Without this, any <null> assessee’s would not label at all.
1 2 | if own == None: L = apn +"\n"+"<fnt size='6'><ita><clr black='70'>"+"Unknown"+"</clr></ita></fnt>" |
The elif statement defines the label (L) for the non-null assessees. The next line creates a new variable, a conversion of the assessee string to title case. Then L is defined as the APN number, then a new line, then the title case assessee with any & replaced with & along with formatting tags to return a 6pt, italics, 70% grey assessee.
1 2 3 | elif own != None: ownT=own.title() L = apn +"\n"+"<fnt size='6'><ita><clr black='70'>"+ownT.replace("&","&")+"</clr></ita></fnt>" |
Finally the last line returns either version of L, the full label for each parcel.
1 | return L |
Fancy labels with python. Labels formatted nicely without creating any new data.
5 Comments
sue hawkins
September 7, 2017Howdy! I came across this nifty entry while looking for a solution to my cartographic question: how to automatically make a light (or any colored) “halo” around text, labels or annotation. As far as I can tell, this doesn’t seem to be possible using ArcMap! Boo…
Do you know differently?
I got my cartographic experience using Maptitude, old versions of ArcView and Illustrator.
Thanks for not laughing at me,
sue hawkins
Michael
February 20, 2018Hi Sue – I believe you’re looking for the: Text Properties > Change Symbol… > Edit Symbol > Mask > Style > Halo pick size then hit Symbol again to change color. It’s super annoying and an insane amount of clicks, but that’s ArcMap!
sue hawkins
February 21, 2018Good morning, Michael! Thanks for getting back to me. I did find out what I was looking for in the interim, but *Boy!* you are right about the insane number of clicks!
I appreciate the help!
—sueh
craig
January 24, 2019What I don’t see is how you defined the APN text to be BOLD. I only see (ita) (/ita) in the expression. Or was your label symbology set for bold and black already and you simply used the expression to change everything you DIDN’T want to be bold and black?
Flox
August 28, 2019Okay.. Thank You. It’s very helpful to me.