3

I have done a lot of research and cannot seem to get the following expression right. I have three (3) attribute columns - ID, Diameter, and Material, I would like to label sewer lines with the unique id, diameter, and material. E.g. GS1945 12" VCP. Ultimately the script would leave out any information the is NULL.

def FindLabel ([ID],[Diameter],[Material])
     if IsNull([Diameter]) and IsNull([Material]) then
          return [ID] 
     elif IsNull([Diameter]) then
          return ([ID] + " " + [Material])
     elif IsNull([Material]) then
          return ([ID] + " " + [Diameter] + "''" )
     else:
          return ([ID] + " " + [Diameter] + "''" + [Material])
1
  • As identified by @Bjorn you are mixing scripting languages and have incorrect syntax. His answer pretty much nails it.
    – Hornbydd
    Jun 6, 2017 at 22:54

2 Answers 2

7

Your code appears to be a mix of VBScript and Python. Also, use variables as parameters in a function definition instead of attribute identifiers. IsNull is not a Python command; use a boolean test instead. Making those corrections, and assuming the 3 parameters are strings, your code would look like:

def FindLabel(id, diameter, material):
    if not diameter and not material:
        return id 
    elif not diameter:
        return (id + " " + material)
    elif not material:
        return (id + " " + diameter + "''" )
    else:
        return (id + " " + diameter + "''" + material)

By further exploiting variables, concatenation, and string formatting, the code can be simplified to:

def FindLabel(id, diameter, material):
    label = id
    if diameter:
        label += " {}''".format(diameter)
    if material:
        label += " {}".format(material)
    return label
2
  • This is generally correct. I had to change a few minor things, I assume to work with ArcGIS's Label Expression Python Parser over directly with Python. Do you recommend any reading resources to understand formatting, etc. I will likely answer my own question with the correct format that worked for me, unless there is another appropriate way to handle this.
    – LandArch
    Jun 7, 2017 at 13:19
  • If your ultimate solution is significantly different, you should post it as an answer, and mark it as the correct one.
    – Bjorn
    Jun 15, 2017 at 20:19
1

@Bjorn was generally correct above. I needed to tweak things slightly, I assume to pork with ArcGIS's Python Parser over straight Python. The expression that worked for me is as follows:

def FindLabel ([ID], [Diameter], [Material]):
    if not [Diameter] and not [Material]:
        return [ID] 
    elif not [Diameter]:
        return ([ID] + " " + [Material])
    elif not [Material]:
        return ([ID] + " " + [Diameter] + "''")
    else:
        return ([ID] + " " + [Diameter] + "''" + " " + [Material])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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