ArcGIS – Creating Stacked Python Label Expression in ArcGIS Desktop for Enhanced Map Labeling

arcgis-10.3arcgis-desktoplabelingpython-parser

I am using ArcGIS Desktop 10.3.

I have a number of polygons representing exploration licences and a series of attributes including licence name, operator, partners and associated interests (%) within each licence. I am trying to create a stacked label expression that will display in the following order: Licence name, operator and interest, partner(s) name and interest. The way the attribute table is setup is that the percentage interest values are 0 where there is no partner. The dataset is read only and so I can't edit the table to remove the zeros. So far I have the following code:

def FindLabel ([AREABLK] , [Own1] , [Asgnpct1] , [Own2] , [Asgnpct2] , [Own3] , [Asgnpct3] , [Own4] , [Asgnpct4] , [Own5] , [Asgnpct5]):
    if [Own5] is not None:
        return [AREABLK] + "\n" + [Own1] + " (" + [Asgnpct1] + "%" + ")" + "\n" + [Own2] + " (" + [Asgnpct2] + "%" + ")" + "\n" + [Own3] + " (" + [Asgnpct3] + "%" + ")" + "\n" + [Own4] + " (" + [Asgnpct4] + "%" + ")" + "\n" + [Own5] + " (" + [Asgnpct5] + "%" + ")"
    elif ([Own4] is not None and [Own5] is None):
        return [AREABLK] + "\n" + [Own1] + " (" + [Asgnpct1] + "%" + ")" + "\n" + [Own2] + " (" + [Asgnpct2] + "%" + ")" + "\n" + [Own3] + " (" + [Asgnpct3] + "%" + ")" + "\n" + [Own4] + " (" + [Asgnpct4] + "%" + ")"
    elif ([Own3] is not None and [Own4] is None):
        return [AREABLK] + "\n" + [Own1] + " (" + [Asgnpct1] + "%" + ")" + "\n" + [Own2] + " (" + [Asgnpct2] + "%" + ")" + "\n" + [Own3] + " (" + [Asgnpct3] + "%" + ")"
    elif ([Own2] is not None and [Own3] is None):
        return [AREABLK] + "\n" + [Own1] + " (" + [Asgnpct1] + "%" + ")" + "\n" + [Own2] + " (" + [Asgnpct2] + "%" + ")"
    elif  [Own2] is None:
        return [AREABLK] + "\n" + [Own1] + " (" + [Asgnpct1] + "%" + ")"

This gives the following output:

enter image description here

The first few rows work fine but where the partner isn't present it still shows the 0 value with the (%) text added.

I would therefore like to ignore the 0 values where there is no partner in the expression.

Best Answer

The code section at the bottom of this answer is better than using dict. The problem with using dict is that the keys must be unique, and in-some cases (not this particular question) they might not be.


I changed your code somewhat but it produces what you want. It checks for owner to be empty or none, then it checks the same for percent. If they both have values it adds them to the label. You can modify any section of this with an else to replace the empty value with something if needed. Yes intial problem was the way you were checking for the value, but I went down a rabbit hole and wanted to post this.

from collections import OrderedDict
def FindLabel ([AREABLK] , [Own1] , [Asgnpct1] , [Own2] , [Asgnpct2] , [Own3] , [Asgnpct3] , [Own4] , [Asgnpct4] , [Own5] , [Asgnpct5]):
  dict = ([Own1],[Asgnpct1]),([Own2],[Asgnpct2]),([Own3],[Asgnpct3]),([Own4],[Asgnpct4]),([Own5],[Asgnpct5])
  dict = OrderedDict(dict)
  x = [AREABLK]
  for k, v in dict.iteritems():
    if k is not None:
      if k.strip() != "":
       if v is not None:
         if v.strip() !="":
           x = x + "\n" +  k + " (" + v + "%)"
  return x

Updated code to work around using dictionary for key:values and problems associated with it:

def validate(element):
  return element and element.strip()

def FindLabel ([AREABLK] , [Own1] , [Asgnpct1] , [Own2] , [Asgnpct2] , [Own3] , [Asgnpct3] , [Own4] , [Asgnpct4] , [Own5] , [Asgnpct5]):
  fields = [Own1] , [Asgnpct1] , [Own2] , [Asgnpct2] , [Own3] , [Asgnpct3] , [Own4] , [Asgnpct4] , [Own5] , [Asgnpct5]
  area = [AREABLK]
  label = ["{}".format(area)]
  for i in range(len(fields)/2): 
    f1 = fields[i*2]
    f2 = fields[i*2+1]
    if validate(f1) and validate(f2):
      label.append("{} ({}%)".format(f1,f2))
  return '\n'.join(label)
Related Question