0

I found a code example for what I'm looking to accomplish at Creating conditional stacked labeling in ArcGIS Pro?

I want to display filter quantity and sizes for a school district per building in a stacked format, but not display a line if there is a null value in the field. I could use another value besides null if necessary. These are currently all text fields.

def FindLabel ([UNITID],  [SIZE1],  [QTY1],  [SIZE2],  [QTY2],  [SIZE3],  [QTY3],  [SIZE4],  [QTY4]):
if [QTY3] != "" and [SIZE4] == 'Null':
    return [UNITID] + "\n" + [QTY1] + " (" + [SIZE1] + ")" + "\n" + [QTY2] + " (" + [SIZE2] + ")" + "\n" + [QTY3] + " (" + [SIZE3] + ")"
elif [QTY2] != "" and [SIZE3] == 'Null':
    return [UNITID] + "\n" + [QTY1] + " (" + [SIZE1] + ")" + "\n" + [QTY2] + " (" + [SIZE2] + ")"
elif [QTY1] != "" and [SIZE2] == 'Null':
    return [UNITID] + "\n" + [QTY1] + " (" + [SIZE1] + ")"
else:
    return [UNITID] + "\n" + [QTY1] + " (" + [SIZE1] + ")" + "\n" + [QTY2] + " (" + [SIZE2] + ")" + "\n" + [QTY3] + " (" + [SIZE3] + ")" + "\n" + [QTY4] + " (" + [SIZE4] + ")"

Error Report

1

You probably have null values in one or more fields (not to be confused with a the literal string value of "Null") which are getting passed through as None, a special Python type which can't be concatenated to strings in that way.

You're only currently checking for empty strings, but a more generic way to check for either would be to do simply if [field], since using a string in a boolean condition will only evaluate to False when it's empty.

bool("") == False and bool(None) == False

0

I was able to use a few examples elsewhere, and came up with this working solution. All I want to do now is bold and underline the UNITID.

I'm posting the code here for anyone else who wants an easy solution.

    def FindLabel ( [UNITID], [QTY1], [SIZE1], [QTY2], [SIZE2], [QTY3], [SIZE3], [QTY4], [SIZE4] ):  
  if [UNITID] and [QTY1] and [SIZE1] and [QTY2] and [SIZE2] and [QTY3] and [SIZE3] and [QTY4] and [SIZE4]:  
      return [UNITID] + '\r' + [QTY1] + "x " + [SIZE1] + '\r' + [QTY2] + "x " + [SIZE2] + '\r' + [QTY3] + "x " + [SIZE3] + '\r' + [QTY4] + "x " + [SIZE4] 
  elif [UNITID] and [QTY1] and [SIZE1] and [QTY2] and [SIZE2] and [QTY3] and [SIZE3]:  
      return [UNITID] + '\r' + [QTY1] + "x " + [SIZE1] + '\r' + [QTY2] + "x " + [SIZE2] + '\r' + [QTY3] + "x " + [SIZE3]
  elif [UNITID] and [QTY1] and [SIZE1] and [QTY2] and [SIZE2]:  
      return [UNITID] + '\r' + [QTY1] + "x " + [SIZE1] + '\r' + [QTY2] + "x " + [SIZE2]
  elif [UNITID] and [QTY1] and [SIZE1]:  
      return [UNITID] + '\r' + [QTY1] + "x " + [SIZE1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.