What is a good way to skip a blank field when creating label expressions? I have a bunch of points that I need labeled with data from multiple fields but some contain blanks. I am working in ArcPro if that makes any difference.
what labelling method are you using? standard or maplex? What types of the fields are involved? labelling language?
Perhaps some examples from your case.
In essence, you can create a labelling function that will use the inputs from fields and translate any nulls to an empty string prior to concatenation
http://pro.arcgis.com/en/pro-app/help/mapping/text/specify-text-for-labels.htm
Can you share an example of a label including blank fields and what you want to have as a result? There are multiple ways to achieve this and an example will make it easier to suggest a good way to do this.
I am using Maplex. The fields are text fields.
The Popup2 field has blanks in that I want to omit when I generate the labels. The red arrows are the blanks I am trying to remove.
Right now I am using a basic expression to create them.
[Name2] + '\n' + [PopUp2] + '\n' + [DMSLat] + '\n' + [DMSLong]
Thanks for taking the time to help.
A demo... a, b, c, d represent field names
they need to be replace with !YourFieldName! in ! marks
Python parser
could be used in a field calculation and I presume in some labeling thing (don't do much mapping with labels, so I will defer to those that do)
a, b, c, d = ['a', '', 'c', 'd']
def prn(a, b, c, d):
"""demo replace the letters with !YourFieldName!
"""
if b not in ("", " ", None):
frmt = "{}\n{}\n{}\n{}".format(a, b, c, d)
else:
frmt = "{}\n{}\n{}".format(a, c, d)
return frmt
prn(a, b, c, d)
'a\nc\nd'
print(prn(a, b, c, d))
a
c
d
... and since you can use Arcade too for the labeling, find an example below:
var Name2 = "FR-20";
var PopUp2 = null;
var DMSLat = "37°32'5.460\"N";
var DMSLong = "113°4'53.641\"W";
// var Name2 = $feature.Name2;
// var PopUp2 = $feature.PopUp2;
// var DMSLat = $feature.DMSLat;
// var DMSLong = $feature.DMSLong;
var array = [Name2, PopUp2, DMSLat, DMSLong];
var result = "";
for (var i = 0; i < Count(array); i++ ){
if (IsEmpty(array[i]) == false) {
if (result == "") {
result = array[i];
} else {
result = result + "\n" + array[i];
}
}
}
return result;
On lines 1 to 4 I'm setting some values hard coded. Lines 6 to 9 show how these values can be read from the fields.
This will yield the label without the empty line:
I will try this one. I have been wanting to learn Arcade a little more.
For more information on Arcade, have a look here: ArcGIS Arcade | ArcGIS for Developers
This blog by Kelly Gerrow is a very good read: https://community.esri.com/community/gis/web-gis/arcgisonline/blog/2017/07/18/conditional-field-disp...
forgot this missive....
*args would require some field names of indeterminate length
ie prn(!A!, !b!, !cc!, !d!) as the expression
a, b, c, d = ['a', '', 'c', 'd']
def prn(*args):
"""demo replace the letters with !YourFieldName!
"""
vals = [i for i in args if i not in ("", " ", None)]
frmt = ("{}\n"*len(vals)).format(*vals)
return frmt
print(prn(a, b, c, d)) # in the order given
a
c
d
print(prn(a)) # a singleton
a
print(prn(a, d, c, b)) # switch them up
a
d
c
But if you want to change the separator, you can use this variant
# --- requires python 3.6 or above
def prn(*args, sep="..."):
"""demo replace the letters with !YourFieldName!
"""
vals = [f"{i}"+sep for i in args if i not in ("", " ", None)]
frmt = ("{}"*len(vals)).format(*vals)
return frmt
prn(a, c, d)
'a...c...d...'
# ---- change sep="\n" for newline instead