I am trying to combine three numeric fields together for a feature's label. I only want to label the feature if it is over 0. The labeled numbers need to be separated a dash ("-"). This is what I attempted to use as the label expression in ArcMap 10.2:
def FindLabel ( [a1], [a2], [a3] ):
if ([a3]) == 0 and int([a2]) == 0 and ([a1]) == 0:
label = " "
elif ([a3]) == 0 and int([a2]) == 0
label = [a1]
elif ([a3]) == 0
label = [a1] + "-" + [a2]
else:
label = [a1] + "-" + [a2] + "-" + [a3]
return label
-
What errors or issues did you have?papadoo– papadoo2014年08月18日 18:30:50 +00:00Commented Aug 18, 2014 at 18:30
-
I get this when I try to verify the expression or hit OK: "Error 0 on line 0. SyntaxError: invalid syntax(<string>, line 4)."juturna– juturna2014年08月18日 18:36:54 +00:00Commented Aug 18, 2014 at 18:36
-
What are the field types for a1, a2, a3, and are there Null values for any?recurvata– recurvata2014年08月18日 18:45:17 +00:00Commented Aug 18, 2014 at 18:45
-
All three field types are String and there are some null values.juturna– juturna2014年08月18日 18:53:06 +00:00Commented Aug 18, 2014 at 18:53
-
You're missing the colon on the end of the elif expressions, that would be why it's not working. For example change elif ([a3]) == 0 and int([a2]) == 0 to elif ([a3]) == 0 and int([a2]) == 0: for correct syntax. If you have null values the expression is [A3] == None for null.Michael Stimson– Michael Stimson2014年08月18日 21:37:56 +00:00Commented Aug 18, 2014 at 21:37
2 Answers 2
This function doesn't need to be too complex. Convert the items you want into a list, then join the result.
def FindLabel([A1], [A2], [A3]):
args = [A1, A2, A3]
items = [str(x) for x in args if bool(x) and int(x) > 0]
if any(items):
return '-'.join(items)
else:
return ' '
If this were proper Python, you would normally use *args
to accept any number of arguments into a list.
-
That's not how an ArcGis label expression works MikeT, have a read of this resources.arcgis.com/en/help/main/10.1/index.html#//… that functionallity will work if you supply that as another sub and call it from the find_label sub - that name is special, but if you name it join_label and call it from find_label it works great!Michael Stimson– Michael Stimson2014年08月18日 22:12:37 +00:00Commented Aug 18, 2014 at 22:12
-
@MichaelMiles-Stimson OK, I see how it works.Mike T– Mike T2014年08月18日 22:45:18 +00:00Commented Aug 18, 2014 at 22:45
-
Awesome! Great expression for its simplicity and diversity: '_'.join, ' '.join etc.. works just as well. I like the use of any(items), that's not one I've seen before, usually I do if len(items) != 0 but that can mean a list of empty items. Thanks for that, I'll be using it in the future.Michael Stimson– Michael Stimson2014年08月18日 22:51:23 +00:00Commented Aug 18, 2014 at 22:51
-
+1 for a nice pythonic approach that isn't limited to only 3 fields with set names.Paul– Paul2014年08月19日 00:41:27 +00:00Commented Aug 19, 2014 at 0:41
There are missing semi-colons and problems with the parentheses. then you should make sure that you always have a string output.
EDIT : sorry, I've given a solution for the field calculator. This solution is updted for the labelling engin.
def FindLabel ( [A1], [A2], [A3] ):
a1=[A1]
a2=[A2]
a3=[A3]
if ( (int(a1) == 0) and (int(a2) == 0) and (int(a3) == 0)):
label = " "
elif ( (int(a2) == 0) and (int(a3) == 0):
label = str(a1)
elif (int(a3) == 0):
label = a1 + "-" + a2
else:
label = a1 + "-" + a2 + "-" + a3
return label
note that the following test are equivalent
int(a1) == 0
and
a1='0'
for testing if a field is null, you can use
if not a1:
-
Thanks for the response. I'm (very) new to Python. My fields are all Strings. I tried the code you wrote, as well as the following (since my fields are Strings). Neither worked and I get the same error. ` def FindLabel ( a1, a2, a3 ): if ( (int(a3) == 0) and (int(a2) == 0) and (int(a1) == 0)): label = " " elif ( (int(a3) == 0) and (int(a2) == 0): label = str(a1) elif (int(a3) == 0): label = str(a1) + "-" + a2 else: label = str(a1) + "-" + a2 + "-" + str(a3) return label `juturna– juturna2014年08月18日 18:57:08 +00:00Commented Aug 18, 2014 at 18:57
-
+1 for the edited solution for labelling. If this doesn't work I'd guess that something is wrong with the dataGISHuman– GISHuman2014年08月18日 19:34:51 +00:00Commented Aug 18, 2014 at 19:34
-
your first elif statement is checking a2 twice, I'm assuming you meant to check a2 and a3.papadoo– papadoo2014年08月18日 19:46:33 +00:00Commented Aug 18, 2014 at 19:46
-
@papadoo you are right, it's a typo. Corrected by now.radouxju– radouxju2014年08月19日 05:19:15 +00:00Commented Aug 19, 2014 at 5:19
Explore related questions
See similar questions with these tags.