Stacked Label Expression in Python

7286
11
Jump to solution
01-16-2015 05:43 PM
BrynDunbar
New Contributor II

I have successfully rendered a stacked label in ArcMap using a java script labeling expression, but I cannot figure out how to do the same thing with Python in the Label Expression box.

 

This is what I used for java script:

 

function FindLabel ( [LOT], [SUBDIVISION] )

{

  return [LOT] +  "\r"  +  [SUBDIVISION];

}

 

How do I do this same thing by using python?

 

I just want to stack the my Lot label on top of my Subdivision label, but using python.

 

Thank you in advance for any thoughts or suggestions.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You could change the expression to:

def FindLabel([LOT] ,[SUBDIVISION]):
  return "{0}\n{1}".format([LOT], [SUBDIVISION])

It suppresses the error. However, this will return None if there is no data:

result0.png

The nice part of using the python expression is that you can enhance the result, replacing the None for a different text

def FindLabel([LOT] , [SUBDIVISION]):
  return "{0}\n{1}".format([LOT] if [LOT] != None else "No LOT",  [SUBDIVISION] if [SUBDIVISION] != None else "No SUBDIVISION")

... will result in:

result.png

... or adding some formatting to the text:

def FindLabel([LOT] , [SUBDIVISION]):
  return "{0}\n{1}".format([LOT] if [LOT] != None else "<CLR red='255'>No LOT</CLR>",  [SUBDIVISION] if [SUBDIVISION] != None else "<CLR red='255'>No SUBDIVISION</CLR>")

will give:

result2.png

View solution in original post

11 Replies
XanderBakker
Esri Esteemed Contributor

It is very similar:

def FindLabel ( [LOT] , [SUBDIVISION]  ):
  return [LOT] +  "\n"  +  [SUBDIVISION]
BrynDunbar
New Contributor II

Thank you for the reply.

I typed this into the Expression box with Python selected as the Parser and Advanced checked.

def FindLabel ( [LOT]  , [SUBDIVISION]  😞

  return "Name: " + [LOT]  + '\n' + [SUBDIVISION]

I got an error,

Error 0 on line 0.

Error running expression:

FindLabel(ESRIExpressionArg0,ESRIExpressionArg1)

Tracedback (most recent call last):

  File"<expression>", line 1, in <module>

  File"<string>", line 2, in FindLabel

TypeError: cannot concatenate 'str' and 'NoneType' objects.

Does this error have to do with 'null' values? Or do I need to cast the variable or do a type conversion?

ChristianHinderman
New Contributor

I had a similar problem recently. I think one of the label fields must have a null value. If you only have to worry about null values in subdivision (and both fields are strings) this should work:

def FindLabel ( [LOT], [SUBDIVISION] 😞

  if [SUBDIVISION]:

    label = "Name: " + [LOT] + "\n" + [SUBDIVISION]

  else:

    label = "Name: " + [LOT]

  return label

deleted-user-rmQdIupSdOqe
New Contributor III

It is the NULL values in the fields that make these python expressions error out.   The answer above solves that problem.

I have one that looks like this, where sometimes [ContractName] and [ContractYear] are null, so I nest in the if statements.

def FindLabel ([ContractName], [ContractYear]):

  if [ContractName]:

    if [ContractYear]:

      label = [ContractName] + '\n' + [ContractYear]

  else:

    label = "No" + '\n' + "Contract"

  return labelStacked Python Labels.JPG

BrynDunbar
New Contributor II

Thank you very much! This is the first time that i have used GeoNet and I am very pleased with the responses that I have received; what a great resource it is; and the sense of community that it provides for GIS users.

I used your example and put this in the Label Expression box:

def FindLabel ([LOT], [SUBDIVISION]):

  if [LOT]:

    if [SUBDIVISION]:

      label = [LOT] + '\n' + [SUBDIVISION]

  else:

    label = "   "

  return label

It shows what I want it to show and I understand the code. This nesting idea was what I needed. Thanks again!

0 Kudos
BrynDunbar
New Contributor II

Thanks Christian! Your response and this thread definitely helped me find the solution that I was looking for. This is what I have so far.

def FindLabel ([LOT], [SUBDIVISION]):

  if [LOT]:

    if [SUBDIVISION]:

      label = [LOT] + '\n' + [SUBDIVISION]

  else:

    label = "   "

  return label

It labels Lots and Subdivisions, but when they are Null, you can't see the label because I made it "  " say nothing but a space. I am sure there are better ways to write the code, but this seems to be doing what I want for now.

Thank you again.

0 Kudos
deleted-user-rmQdIupSdOqe
New Contributor III

The other option is to use the labeling method "Define classes of features and label each class differently" option, setting up a SQL expression first, to filter out data that has NULL values.

sql for null values.JPG

Then your python label expression for that label class (my label class is Default) is:

stacked label python example.JPG

XanderBakker
Esri Esteemed Contributor

You could change the expression to:

def FindLabel([LOT] ,[SUBDIVISION]):
  return "{0}\n{1}".format([LOT], [SUBDIVISION])

It suppresses the error. However, this will return None if there is no data:

result0.png

The nice part of using the python expression is that you can enhance the result, replacing the None for a different text

def FindLabel([LOT] , [SUBDIVISION]):
  return "{0}\n{1}".format([LOT] if [LOT] != None else "No LOT",  [SUBDIVISION] if [SUBDIVISION] != None else "No SUBDIVISION")

... will result in:

result.png

... or adding some formatting to the text:

def FindLabel([LOT] , [SUBDIVISION]):
  return "{0}\n{1}".format([LOT] if [LOT] != None else "<CLR red='255'>No LOT</CLR>",  [SUBDIVISION] if [SUBDIVISION] != None else "<CLR red='255'>No SUBDIVISION</CLR>")

will give:

result2.png

XanderBakker
Esri Esteemed Contributor

I wouldn't go for label classes in this case, since in the example above, with a simple line I am tackling 4 classes,,,