Jump to content

 
Photo

Label Expressions ArcMap

* * * * * 1 votes

  • Please log in to reply
6 replies to this topic

#1
Keith Map Service

Keith Map Service

    Contributor

  • Validated Member
  • PipPip
  • 18 posts
  • Gender:Male
  • Location:Mobile, AL
  • Interests:Geography, mapping, GIS, Fishing, Outdoors, Mountain Biking
  • United States

How do you tell ArMap to draw labels after so many words in a field on a new line.

Essentially I have two fields: One field I want on the top line and the other field is long. I want this field
split on multiple lines after so many words, say every 15 words.

How could this be done? I have failed to find the solution with other vb code.

[Field 1] - sh767
[Field 2] - Joe went to the store and traveled to see Sue

Output:
sh767
Joe went to the store
and traveled to see Sue

thanks in advance
Keith Map Service, Inc.
http://keithmaps.com

#2
dsl

dsl

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 225 posts
  • Gender:Male
  • Location:Denver
  • United States

Try something like this in your expression:

Function FindLabel ([Field 1], [Field 2])
Dim lbl as String
lbl = [Field1] & vbnewline

If (Len([Field2] >15) then
lbl = lbl & Left([Field2], 15) & vbnewline
lbl = lbl & Mid([Field2], 16, (Len([Field2])-15))
Else
lbl = lbl & ([Field2]
end if
FindLabel = lbl
End Function

Some useful references for more complex expressions:
http://www.w3schools...ipt/default.asp
http://www.esri.com/...cript_label.pdf

Hope that helps,
David

#3
heath b

heath b

    Key Contributor

  • Validated Member
  • PipPipPip
  • 71 posts
  • Gender:Male
  • Location:Portland, OR
  • Interests:the great outdoors, bouldering, martinis, unusual graphical representation
  • United States

David's got you on the right track, but in order to ensure that you break your label between words, you will need to locate the appropriate place to break the string (defined by a space). Rather than define how many words you want per line, this defines how many characters max. are on the first line (in this example it's 40). The InStrRev() function finds the furthest left instance of the specified character (" ") from the defined location in the string. This should work for you.

Function FindLabel ( [Field 1], [Field 2] )
Dim lbl
Dim Space
lbl = [Field 1] & vbnewline


If Len([Field 2]) > 40 Then
Space = -1
Do
Space = InStrRev([Field 2], " ", Space)
Loop Until Space < 40

If Space = -1 Then
lbl = lbl & [Field 2]
Else
lbl = lbl & Left([Field 2], space) & vbnewline
lbl = lbl & Mid([Field 2], (space + 1), (Len([Field 2]) - space))
End If
Else
lbl = lbl & [DIS_MGR_NM]
End If
FindLabel = lbl
End Function

Or, conversely, if you happen to have the Maplex labeling engine for ArcMap, you can define that in the placement properties.

Heath

#4
Keith Map Service

Keith Map Service

    Contributor

  • Validated Member
  • PipPip
  • 18 posts
  • Gender:Male
  • Location:Mobile, AL
  • Interests:Geography, mapping, GIS, Fishing, Outdoors, Mountain Biking
  • United States

After applying the code below, The AVOID_ID field only draws. The COMMENTS field does not draw at all.



Function FindLabel ( [AVOID_ID] , [COMMENTS] )
Dim lbl
Dim Space
lbl = [AVOID_ID] & vbNEWLINE


If Len( [COMMENTS] ) > 40 Then
Space = -1
Do
Space = InStrRev( [COMMENTS] , " ", Space)
Loop Until Space < 40

If Space = -1 Then
lbl = lbl & [COMMENTS]
Else
lbl = lbl & Left([COMMENTS], space) & vbnewline
lbl = lbl & Mid([COMMENTS], (space + 1), (Len([COMMENTS]) - space))
End If
Else
lbl = lbl & [COMMENTS]
End If
FindLabel = lbl
End Function
Keith Map Service, Inc.
http://keithmaps.com

#5
heath b

heath b

    Key Contributor

  • Validated Member
  • PipPipPip
  • 71 posts
  • Gender:Male
  • Location:Portland, OR
  • Interests:the great outdoors, bouldering, martinis, unusual graphical representation
  • United States

Ah yes, I screwed up just a bit. That function will continue to find the same " " if the first one doesn't meet the Do Loop criteria. I fixed the code so that it will loop through until it finds the correct space. I'm far from an expert programmer, but I enjoy the tinkering. I hope this should work for you:

Function FindLabel ( [AVOID_ID] , [COMMENTS] )
Dim lbl
Dim Space
lbl = [AVOID_ID] & vbNEWLINE


If Len( [COMMENTS] ) > 40 Then
Space = 0
Do
Space = Space - 1
Space = InStrRev( [COMMENTS] , " ", Space)
Loop Until Space <= 40

If Space <= 0 Then
lbl = lbl & [COMMENTS]
Else
lbl = lbl & Left([COMMENTS], Space) & vbnewline
lbl = lbl & Mid([COMMENTS], (Space + 1), (Len([COMMENTS]) - Space))
End If
Else
lbl = lbl & [COMMENTS]
End If
FindLabel = lbl
End Function

Best of luck,
Heath

#6
Keith Map Service

Keith Map Service

    Contributor

  • Validated Member
  • PipPip
  • 18 posts
  • Gender:Male
  • Location:Mobile, AL
  • Interests:Geography, mapping, GIS, Fishing, Outdoors, Mountain Biking
  • United States

That did the trick. I am new to whole scripting. Just out of curiosity, is there a way to repeat to a third line?
Also, is there any good books or reference material about VB for ArcGIS specifically?

thanks
Keith Map Service, Inc.
http://keithmaps.com

#7
heath b

heath b

    Key Contributor

  • Validated Member
  • PipPipPip
  • 71 posts
  • Gender:Male
  • Location:Portland, OR
  • Interests:the great outdoors, bouldering, martinis, unusual graphical representation
  • United States

Glad it worked. Yes, you can add as many lines as you need to with another loop and variable. I don't have time to write the script out, but it can be done with a Do Until Loop and a variable to hold each subsequent line of the string that you're looking at minus what's already been printed on a line (something like assigning NewVariable = COMMENTS, and then each time you write a line assign NewVariable = Right (NewVariable, (Len(NewVariable) - Space)). You can continue to add new lines until the Do Until has been satisfied (Len(NewVariable) < 40 for example).

As for references, I don't use a book. I tend to use the same references Dave mentioned above. I had at one point a book on VBA for Arc, but after learning the basics find that the online resources work well enough and I don't need to carry a book around... ;)

Happy scripting!
Heath




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

-->