The company I work for sells a MapGuide Open Source based GIS solution and I frequently use the LOOKUP and CONCAT functions to create tooltips and hyperlinks. These are very useful functions, but when you start to use them, it isn’t very clear what their possibilities are. This article also explains how to handle NULL values in these functions.
Our MapGuide based application is managed using MapGuide Maestro. This means we can use the built-in info on functions when using the Expression Editor for tooltips and hyperlinks.
CONCAT
The built-in info on the CONCAT function shows Concat([text property], [text property]). When I started using MapGuide Maestro this made me assume that if I wanted to combine three texts I needed to use it as:
CONCAT('Text A', CONCAT('Text B', 'Text C')) If you have to combine even more texts that would quickly result in very complex expressions for a pretty simple task. Luckily the CONCAT function isn’t limited to two texts. So you can use it as:
CONCAT('Text A', 'Text B', 'Text C')Here is an example of how to use it in a tooltip. It combines texts, html tags and fields:
CONCAT('<b>', LSTRNM, '</b><br/>House number: ', HNR)And here is an example of how to use it in a hyperlink:
CONCAT('https://example.com/search?street=', URLENCODE(LSTRNM))Notice how I also used the URLENCODE() function, because without it, a street name with spaces or accents breaks the URL.
LOOKUP
The built-in info on the LOOKUP function shows LOOKUP([expression], [defaultValue], [index], [value]). That didn’t make it clear to me what this function actually does.
LOOKUP compares the value of an expression, usually a field, with a list of values. When it finds a match, it returns the result that belongs to that value. When there is no match, it returns a default value. If you know other programming languages, it works a lot like a switch statement or a CASE in SQL. That might still sound a bit abstract, so I’ll explain it with an example:
LOOKUP(FieldA, 'defaultvalue', 'myvalue', 'anothervalue')In the above example:
- The value of FieldA is compared to the values in the list.
- If FieldA is equal to ‘myvalue’, the result will be ‘anothervalue’.
- If FieldA doesn’t match any of the values in the list, the result will be ‘defaultvalue’.
Use case
So how do I use this function? In the following example the result will be an empty string if the field LSTRNM is equal to ‘ng’, and it will be the content of LSTRNM if it’s something else.
LOOKUP(LSTRNM, LSTRNM, 'ng', '')In the layer where I used this the value ‘ng’ was used if a street name was not available, instead of using a NULL value. So on the map it showed a lot of ‘ng’ values. This function hides those ‘ng’ labels, but still shows the names of all other streets.
LOOKUP with more values
LOOKUP isn’t limited to one value and result. After the expression and the default value, you can add as many pairs as you need. Each pair starts with the value to compare with, followed by the result for that value.
In the following example the field ROADTYPE contains short codes like ‘HW’ and ‘MR’. These codes aren’t very useful to the people looking at the map, so LOOKUP is used to replace them with a readable description in the tooltip:
CONCAT(
LSTRNM,
' (',
LOOKUP(
ROADTYPE,
'unknown road type',
'HW', 'highway',
'MR', 'main road',
'LR', 'local road',
'CP', 'cycle path'),
')')In this example:
- The value of ROADTYPE is compared to ‘HW’, ‘MR’, ‘LR’ and ‘CP’, in that order.
- If a match is found, the result that follows that value is used. So ‘HW’ becomes ‘highway’, ‘MR’ becomes ‘main road’, and so on.
- If ROADTYPE doesn’t match any of the values, the result will be ‘unknown road type’.
- CONCAT then puts the street name in front of it, with the road type between brackets.
So for a street with the name Main Street and road type ‘MR’, the tooltip will show: ‘Main Street (main road)’
I put each value and its result on a separate line. MapGuide doesn’t need this, but it makes it a lot easier to see which result belongs to which value, especially when the list gets longer.
Combining LOOKUPs
You can also combine different LOOKUPs. In the following example there are two fields with street names: LSTRNM is the street name for the left side of the street and RSTRNM that contains the street name of the right side of the street.
LOOKUP(
LSTRNM,
LOOKUP(
RSTRNM,
LOOKUP(
LSTRNM,
CONCAT(LSTRNM, ' - ', RSTRNM),
RSTRNM,
LSTRNM),
'ng',
LSTRNM),
'ng',
LOOKUP(RSTRNM, RSTRNM, 'ng', ''))This expression works from the outside in:
- First it checks if LSTRNM is ‘ng’. If it is, there is no name for the left side, so the result is the name of the right side. The last LOOKUP still makes sure no label is shown if RSTRNM is ‘ng’ as well.
- If LSTRNM contains a street name, it checks if RSTRNM is ‘ng’. If that’s the case, only LSTRNM is shown.
- If both fields contain a street name, the innermost LOOKUP compares them. If they are the same, the name is shown once. If they are different, both names are shown with a dash between them.
So these are all the possible results:
| LSTRNM | RSTRNM | Label |
| Main Street | Main Street | Main Street |
| Main Street | Station Road | Main Street – Station Road |
| Main Street | ng | Main Street |
| ng | Station Road | Station Road |
| ng | ng | (no label) |
Handling NULL values
In my layer, missing street names were stored as ‘ng’. In a lot of datasets a missing value is stored as NULL instead, and the expression above doesn’t handle that. A NULL value is never equal to anything, not even to ‘ng’, so LOOKUP never sees it as a missing street name. You can end up with labels like ‘Main Street – ‘ or with no label at all for a street that does have a name.
The solution is the NullValue function. NullValue(FIELD, ‘ng’) returns the content of FIELD, but if FIELD is NULL it returns ‘ng’ instead. So the expression treats a NULL value exactly like ‘ng’.
LOOKUP(
NullValue(LSTRNM, 'ng'),
LOOKUP(
NullValue(RSTRNM, 'ng'),
LOOKUP(
LSTRNM,
CONCAT(LSTRNM, ' - ', RSTRNM),
RSTRNM,
LSTRNM),
'ng',
LSTRNM),
'ng',
LOOKUP(NullValue(RSTRNM, 'ng'), RSTRNM, 'ng', ''))You don’t have to use NullValue for every field in the expression, only in the places where the field is checked for ‘ng’:
- The outer LOOKUP checks NullValue(LSTRNM, ‘ng’). If the left street name is ‘ng’ or NULL, the last LOOKUP is used. It shows RSTRNM, unless RSTRNM is ‘ng’ or NULL as well. In that case no label is shown.
- The second LOOKUP checks NullValue(RSTRNM, ‘ng’). If the right street name is ‘ng’ or NULL, only LSTRNM is shown.
- The innermost LOOKUP is only reached when both fields contain a street name. So no NullValue is needed there.
This gives the following results:
| LSTRNM | RSTRNM | Label |
| Main Street | Main Street | Main Street |
| Main Street | Station Road | Main Street – Station Road |
| Main Street | ng or NULL | Main Street |
| ng or NULL | Station Road | Station Road |
| ng or NULL | ng or NULL | (no label) |
