Happy #MapInfoMonday!
In today's article, we will look into label expressions and I'll give you 5 ideas for improving these. If you have specific questions, do reach out via a comment or a direct message.
If you select a layer in the
Layer List, you can access the label settings for this layer via the
Labels tab. From here you can also set the
Label Expression via the
Label Using list. Select the item
Expression... to specify a label other than an existing column. This will open up the
Expression dialog that I will use in this article.
1. Measurements
The first tip may be simple but it is still quite powerful. You can via a label very quickly show the size of your spatial objects, be that lines or polygons.
In my example, I'm using polygons and showing the area of these via the
Area()
function but you can also show the length of a line via the
ObjectLen()
function. Both functions take two parameters: a spatial object and a distance/area unit. If you are using data based on projected coordinate systems, I'd recommend that you the Cartesian versions of the functions.
For my polygons, the area expression looks like this:
Area(Object, "sq m")
. If I had been using lines, the expression would look like this:
ObjectLen(object, "m")
. I could have used any of the other support area (or distance) units supported by MapInfo Pro. By default, MapInfo Pro will use the units from the System Preferences.
In the
Expression dialog shown for the
Label Expression, you can simply select the Area function from the
Functions list and it will work as it by default will reference the Object column from the layer and as I mentioned above, use your preferred distance unit. Click
OK to set the label expression.
The resulting map will look like this.
2. Rounding Values
Sometimes MapInfo Pro can reveal too much information if you just ask it to show for example the area or the length of a spatial object. These values are typically shown with 2-3 decimals. In my example, I want the size of the parcels shown with zero decimals. I want the area rounded the whole meters. The number of decimals often depends on the unit as well as the type of feature your objects represent.
You can round a numeric value with the function
Round()
. This function takes two parameters: the numeric value and a second parameter specifying the value to round to. In my case, the round-to-value is 1 as I want it to be rounded to the nearest whole number. If you want one decimal, you can use 0.1, for two decimals use 0.01, and so on. My expression looks like this with the rounding in place:
Round(Area(Object, "sq m"), 1)
.
On my map, you can now see that the area sizes shown in the labels are all shown as whole numbers.
3. Formatting Values
I will have to clarify something. I'm using Danish Regional Settings on my computer. These settings are used when MapInfo Pro displays numeric values. My Regional Settings control the character used as Decimal Symbol as well as Digit Grouping Symbol. In Denmark, we use a comma (,) as Decimal Symbol and we use a point (.) as the Digit Grouping Symbol. This means that we typically write large decimal values like
123.456,789
whereas the same value in the US would be written
123,456.789
.
MapInfo Pro has a couple of functions that can help you format your values. I often use
FormatNumber$()
to format a numeric value using the Regional Settings. You can also do the same via the more generic
Format$()
function. We will save the
Format$()
function for another article as this function can do a lot more than formatting numeric values.
In my example, I'll add FormatNumber$() to my previous expression:
FormatNumber$(Round(Area(Object, "sq m"), 1))
.
There is actually no change in the way the labels are displayed after adding the FormatNumber$() function to my expression. That's because MapInfo Pro applies the Regional Settings when displaying a numeric value as a label. You see the same happening when a numeric value is displayed in a browser window. The number is shown using the Regional Settings
But in the following steps, we will combine the area with other values and by doing so, the numeric value will be converted to a string. And if we don't explicitly specify formatting here, the numeric value will use the US Regional Settings.
4. Concatenating Values
As I mentioned above, we will now add another string to our label. This string will show the area unit used for the area calculation. In this way, it is easier to read the map as the area will also hold a unit.
We can concatenate strings simply using the + operator:
Str$(Round(Area(Object, "sq m"), 1)) + " m" + Chr$(178)
. In the expression, I add two strings to the calculated rounded area of the polygon. First, I add the string
" m"
. The space inside the quotes adds some space between the area and the m. After this string, I add another using the
Chr$()
function which takes an ASCII code and returns the ASCII character matching the code. The ASCII code 178 refers to the character squared (²).
The resulting label for the polygons now looks like this.
5. Multiple Lines
Now instead of just showing the size of the polygons, I want to also include a name or ID for these. It could be the owner of the parcel or as in my example the APN for the parcel.
To avoid having too long a string, I will show the APN on a separate line in the labels. To do so, I will again use the
Chr$()
function to return a new line character. I will add the APN column at the beginning of the label expression and then add the new line feed between this column and the existing expression. The expression will look like this:
APN+Chr$(10)+FormatNumber$(Round(Area(Object, "sq m"), 1))+" m"+Chr$(178)
.
The ASCII code for "new line" is 10 as you probably can see in the expression.
The resulting map looks like this with the APN shown above the area.
6. Conditional Labels
The map is starting to look as I want it to but it's getting a bit crowded. To leave out some of the labels, I decide to add a condition to my label expression using the
IIf()
function. The
IIF()
function takes three parameters: An expression to evaluate, the value to return if the first expression is TRUE, and the value to return if the first expression is FALSE.
Let's start with a simple example to clarify how this function works:
IIf(Area(Object, "sq m) > 800, "BIG", "small")
. The expressions to be evaluated is this:
Area(Object, "sq m) > 800
. We are comparing the size of the object in square meters to
800
. If the area is larger than
800
, the function will return
BIG
, otherwise it will return
small
.
In my example, we will use a bit more complicated expression. We want to return the expression we created under
5. Multiple Lines if the size of the polygons is larger than 800 square meters. If the polygons are smaller, we will return an empty string. In practice, this will mean that we will only show a label for the larger polygons.
The expression will look like this:
IIf(Area(obj, "sq m") > 800, APN+Chr$(10)+FormatNumber$(Round(Area(Object, "sq m"), 1))+" m"+Chr$(178), "")
.
And you can see that the map does look far less cluttered.
I hope this has given you some ideas for improving your label expressions. There is a lot more that can be done so consider this as an introduction. If you have specific things you want to do through label expression, post these as comments below and we can see if we can help you out.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------