Some handy ArcGIS label expressions

At Ordnance Survey we have increasingly looked to ESRI’s ArcGIS and Maplex for labelling and label placement.

There are certain more advanced labels that require more than just calling a single label field from within the data. The first type I shall discuss is dual (or multiple) names.

There are multiple code examples on the ESRI help pages, but here are some simple examples of both the code and the final map appearance from the OS VectorMap District raster product so that you can see what the expression actually does!

Railways and rivers depicted linearly can have their labels aligned to the features. But in Wales we might want to also show the Welsh name, and in Scotland the Gaelic.

The dual-named features in our database have the ‘native’ name in a field called NAME1_CART and the alternative (in this case English) name in a field called NAME2_CART.

By navigating to Layer Properties > Labels > Expression… we can activate the Advanced checkbox and enter our Expression either as VBScript or as Python. This post will use VBScript.

Expression 1

We could have used a definition query to pull out all the dual names but we preferred to keep all of the same feature types together and so the first IF statement is looking for all features that only have one name to label. For these it simply labels using the name in the NAME1_CART field.

If [NAME2_CART] = ” ” Then
FindLabel = [NAME1_CART]

The next part of the expression is the ELSE statement, so what to do if the feature has two (i.e. dual) names. What we ask it to return is a label containing the name from the NAME1_CART field followed by a forward slash followed by the name from the NAME2_CART field.

Else
FindLabel = [NAME1_CART] & “/” & [NAME2_CART]
End If

The label will then be placed as one continuous label with whatever Maplex settings we set for the label class, e.g. to follow alongside a linear river.

Ordnance Survey (C) Crown copyright 2013. All rights reserved
Ordnance Survey (C) Crown copyright 2013. All rights reserved

For areas or polygons with more than one name (again in OS VectorMap this is mainly to show English alongside Welsh or Gaelic), for example for lakes or mountains, then the expression is the same except that seeing as the features are not likely to be linear, we have chosen to make the expression a little more complex in order to force the two different names to be shown on two different lines, i.e. to force the effect of ‘double banking’ or label stacking.

I realise there are label stacking options in the Maplex settings but here we want to ensure that the join between the two name fields is where the new line occurs.

Expression 2

So the new ELSE statement within the expression reads:

Else
FindLabel = [NAME1_CART] & “/” & vbnewline & [NAME2_CART]
End If

Once more returning a label containing the name from the NAME1_CART field followed by a forward slash followed by the name from the NAME2_CART field, except that this time we use the visual basic command ‘vbnewline‘ to force the name from the NAME2_CART field to be shown on a new line, the end result of which is like the settlement name shown below.

Ordnance Survey (C) Crown copyright 2013. All rights reserved
Ordnance Survey (C) Crown copyright 2013. All rights reserved

Note that you can still allow Maplex to add further stacking to the label if you wish, as can be seen in the mountain example below.

Ordnance Survey (C) Crown copyright 2013. All rights reserved
Ordnance Survey (C) Crown copyright 2013. All rights reserved

If you would prefer, you can of course just use the simple expression (as in the river example), and allow Maplex to determine whether or not a new line is needed. This can be done by changing the label stacking Maplex options, where we can either use ‘/‘ as a suggested new line character, or just leave Maplex to do as it sees fit with the default ‘ ‘ (space) character option. An example from OS VectorMap of this is the airport labels.

Ordnance Survey (C) Crown copyright 2013. All rights reserved
Ordnance Survey (C) Crown copyright 2013. All rights reserved

Although as you can see, it can lead to some inconsistency between whether the forward slash is placed before or after the line break.
 

The next type of problem I’d like to briefly discuss is with Maplex street label placement and is only present in ArcGIS 10.0 and older. In ArcGIS 10.1, street label placement considers the label name and the label symbology. It groups the segments of street features, if the symbology and label match and the features are connected, and treats them as single, long features.

In older versions of Arc, the symbology is not considered. This creates a problem where a road of the same name or number has two or more sections of different road classification.

So for example, consider Warrington Road below. To the southwest, the section of primary road between the two roundabouts is Warrington Road. If there was significant room, we want to show this primary classified road name in green. To the northeast, we wish to show the standard A classified road name, also Warrington Road, in magenta.

Ordnance Survey (C) Crown copyright 2013. All rights reserved
Ordnance Survey (C) Crown copyright 2013. All rights reserved

If we just set primary road names to be green and A road names to be magenta and we allow Maplex to concatenate road segments of the same name, then the labels of mixed classification roads such as this will either be shown all (i.e. for the entire length of any given road name) in green or all in red on an ad-hoc basis.

One way around this is to trick Maplex into thinking that the two road sections have different names.

Again navigate to the Label Expression window (Layer Properties > Labels > Expression…) and activate the advanced checkbox.

This time we will be adding a prefix to the label and then asking for it to be ignored when writing the label. Sounds strange but it works!

Below is the example for A road names where the name of the road is in a field called NAME.

Expression 3

So first we define a new variable ‘Ardfix‘ and make this equal to A followed by NAME.

Ardfix = “A” & [NAME]

Then we ask the label to be output without the first character using the visual basic ‘Mid‘ function, where the number 2 below is the number character of the label string to start writing the label from.

FindLabel = Mid (Ardfix, 2, 100)

If we do the same for primary roads but use a different prefix to A, e.g. P, then the primary road segments of Warrington Road that touch one another will be grouped as one feature and labelled appropriately, and the standard A road segments of Warrington Road will be grouped and labelled accordingly, giving us the a green label on the green road and magenta label on the magenta road as defined in the label styles and shown in the final image below. You can see the green segment is not labelled, this is only because it is too short to meet our Maplex rules for placing a label.

Ordnance Survey (C) Crown copyright 2013. All rights reserved
Ordnance Survey (C) Crown copyright 2013. All rights reserved

 

This post will be followed up with another on advanced Maplex labelling.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: