Python labelclasses expression help

3601
9
06-11-2015 09:20 AM
GeoffOlson
Occasional Contributor

I'm working with many shapefiles that all need to be symbolized and labeled the same way.  I've gotten symbol layer matching to work, but I've having difficulty with the labels, particularly with the expression working.  My expressions works fine loading the text file from the layer properties window, but not through the Python console.  My expression uses the Advanced Python dialogue box.

Here is my code so far.  It's copied and pasted from the expression window and wrapped in triple quotes, but it does not end up labeling.  Is there a particular way to enter the expression?

mxd = arcpy.mapping.MapDocument("Current")# Define mxd to current file.

inputData0 = "AM_Bourbonnais_Pkwy_WB_LINE_Spring_2015"

mapLyr0 = arcpy.mapping.ListLayers(mxd, inputData0) [0]

lFunc = """def FindLabel ( [Speed] ):\n

  label = str(round(float([Speed]),0))\n

  if label[1] == '.':\n

    label = label[:1]\n

    label = label + " MPH"\n

    return label\n

  else:\n

    label = label[:2]\n

    label = label + " MPH"\n

    return label"""

mapLyr0.labelClasses[0].expression = lFunc

Tags (2)
0 Kudos
9 Replies
GeoffOlson
Occasional Contributor

It appears I was having a refresh problem or something, because now it seems to work.  Strange.

0 Kudos
BlakeTerhune
MVP Regular Contributor

You can mark as assumed answered so people know you don't still need help.

SepheFox
Frequent Contributor

Just as an FYI, Geoff, I don't believe you need any of those \n.

GeoffOlson
Occasional Contributor

Thanks Sephe.  I included them because when I look at the expression copied to the label's expression box, it's a single line of text.  I don't know how it gets interpreted to I included them.

0 Kudos
SepheFox
Frequent Contributor

In the label expression box returns can be used to organize your label, but they don't effect the resulting label. The \n character is used to actually enforce a new line in your label text. The same is true for your python script--you can use a simple return to go to a new line, and \n is only needed if you want your label text to have a line break.

0 Kudos
GeoffOlson
Occasional Contributor

I tried my script again today, and it doesn't want to work.  I think the problem is that layers are defaulted to using VBscript for the expression, but mine is Advanced Python.  If I run the script on a layer that has the default VBscript, it won't label properly.  If the layer is only set to Python, it doesn't label properly.  When I have the expression type set to Python and the advanced box checked, then it works.  Are there additional parameters to set the label expression type to Python and Advanced?

0 Kudos
BlakeTerhune
MVP Regular Contributor

Looking at the documentation, I don't see anything where you can explicitly specify which expression type to use.

It does give an example of printing the label class expression, so try that and see if it includes anything about which type it's using and adjust your format accordingly.

0 Kudos
GeoffOlson
Occasional Contributor

Unfortunately it just outputs what I coded as the input.

0 Kudos
JamesKelly2
New Contributor

Did you ever find any resolution on this? I am running into the same issue.

0 Kudos