Select to view content in your preferred language

Using if in Label Expression - Python

9255
12
Jump to solution
01-03-2013 06:28 AM
MikeBly
Regular Contributor
Hi All,

I am trying to setup a label expression using python parser in ArcGIS 10.1. Simply, this expression looks at parcel attributes, and if the parcel size is >= 41, label it, otherwise, do not label. Also, if parcel meets requirement, split the string (ownername) at the comma and stack label, so last name on top, first name on bottom.

If the string does not contain a comma, stack at first space in string.

I have done part of this using VBscript parser, to the point where it splits at comma and stacks. I would like to move this over to a working python maintain consistency is scripting language that we use. I cannot get python to even look at the size of the parcel.

A working example of the VB is below:

Function FindLabel ( [ANAME], [ACRES] )
if ([ACRES] >= 41) Then
  myArray = Split ( [ANAME] , "," )
  strLabel = myArray (0)
  strname = Split ( [ANAME] , " " )
   For i = 1 To UBound (myArray)
   strLabel = strLabel & "," & vbNewLine & myArray (i)  
   FindLabel = strLabel
Next
End if
End Function


Thanks,

Mike
Tags (2)
0 Kudos
12 Replies
T__WayneWhitley
Honored Contributor
Very good.
Yes, you need to enter CODE tags: goes after the code you are posting;
 
before your code (I reversed them here).  I understand you can also do it interactively with the # sign such thing, but I'm a little old school...  have it your way and enjoy!

EDIT:
1st character in string in list?--- then:

S[0][0:1]

So that means if S is a list, take the 1st member [0] and 'slice' 1 character from the front, returning that character... make sense?
0 Kudos
T__WayneWhitley
Honored Contributor
Mike, I responded to your msg but it doesn't appear that the code posted there saved the indention properly - I'll post it again here...it is rather 'slopped' together since you'll likely be tweaking it anyway, so it's even more critical you get the indention right:
(Note that the initial query on your acreage field has been removed since you can place that 'filter' on the layer by creating a default label class, applying a similar SQL query)
def FindLabel ([ANAME]):
     text = [ANAME]
     S = text.split(', ')
     if len(S) == 1:
          S2 = S[0].split(' ', 1)
          S2 = S2[0] + '\n' + S2[1]
     else:
          S2 = S[0] + ',\n'
          ABBREV = ''
          S3 = S[1].split(' ')
          for each in S3:
               if not each in ('&', 'AND'):
                    ABBREV = ABBREV + each[0:1] + '. '
               else:
                    ABBREV = ABBREV + each[0:1] + ' '
          S2 = S2 + ABBREV[0:-1]
     return S2


Given the sample strings you provided and the fact that you only wanted it 'broken' at the 1st space or comma, below are 3 example results calling this label function.

Enjoy,
Wayne

--------------------
SMITH, JOHN & JANE JEAN

SMITH,
J. & J. J.
--------------------
MAXIMUM POWER CORP

MAXIMUM
POWER CORP
--------------------
DOE, JOHN

DOE,
J.
--------------------
0 Kudos
MikeBly
Regular Contributor
HI Wayne,

Thank you for this feedback. This code worked very well, of course there is some tweaking to be done. But works great, thanks for your continued assistance with this problem. I am still very much a rookie with scripting, working through this code taught me a lot though.

Cheers,

Mike
0 Kudos