Select to view content in your preferred language

Label script help- excluding one record from labeling

11794
6
Jump to solution
12-18-2012 11:45 AM
KelleeRoberts
New Contributor
Hello-

I'm fairly new to mainpulating the label expressions in ArcMap. I am not sure how to exclude some records from displaying. For example there are 20 cities on my map, but I'd like to exclude 2 from labeling on my map. I want to be able to still see the point where they are...but not the text. I also would prefer not to make a new shapefile, column, or create a layer based on selection. Any ideas on the expression that could be written? I'd prefer VB, but open to jscript too.
I'm using ArcMap 10 and Maplex. The field I'm using is called "POI_NAME"

Thanks!
Tags (2)
1 Solution

Accepted Solutions
MatthewPatterson
Esri Contributor
Hello,

You could modify this sample expression, make sure to hit the advanced box and paste this in.  You will need to change City1 and City2 to the values of the cities you want to exude.

Function FindLabel ([POI_NAME])
If [POI_NAME] = "City1" or [POI_NAME] = "City2" then
          FindLabel = ""
Else
          FindLabel = [POI_NAME]
End if
End Function

The code isn't pasting properly, but make sure that you tab in right before each instance of FindLabel.

View solution in original post

6 Replies
MatthewPatterson
Esri Contributor
Hello,

You could modify this sample expression, make sure to hit the advanced box and paste this in.  You will need to change City1 and City2 to the values of the cities you want to exude.

Function FindLabel ([POI_NAME])
If [POI_NAME] = "City1" or [POI_NAME] = "City2" then
          FindLabel = ""
Else
          FindLabel = [POI_NAME]
End if
End Function

The code isn't pasting properly, but make sure that you tab in right before each instance of FindLabel.
KelleeRoberts
New Contributor
Thank you Matt! Worked like a charm. Also, the formatting was just fine. I tried adding a tab before each "FindLable" but wasn't able to. Decided to give it a shot anyway, and it worked. I'll be saving this code for future use! Thanks again.
0 Kudos
FredaKuhl
New Contributor

This worked perfect, Thank you!

0 Kudos
KeithAddison1
Frequent Contributor

How can I get this to work for a numeric wildcard?  I want to exclude from labeling all values of SITE that start with 18 so I put this together:

def FindLabel ( [SITE] 😞
 if int([SITE]) == '18%':
  return " "
 else:
  return [SITE]

The syntax checks out, but all the values that start with 18 are still labeled.  If I take away the quotes around the 18 I get a syntax error.  What gives?  Is there no wildcard term for numbers in Python?

0 Kudos
XanderBakker
Esri Esteemed Contributor

I think you are mixing two formats (VBscript and Python). The field SITE using the Python parser should be expressed as !SITE!.

You could use something like this with the Python parser:

def FindLabel(site):
    return "" if str(site).startswith('18') == True else site

Your expression should be:

Findlabel(!SITE!)

I tested it with this:

def main():
    tests = [18, 182, 1807, 18874, 1901, 17, 1981]
    for test in tests:
        print "in: {}, label:{}".format(test, FindLabel(test))

def FindLabel(site):
    return "" if str(site).startswith('18') == True else site

if __name__ == '__main__':
    main()

which resulted in:

in: 18, label:
in: 182, label:
in: 1807, label:
in: 18874, label:
in: 1901, label:1901
in: 17, label:17
in: 1981, label:1981

All values that start with 18 result in an empty string

KeithAddison1
Frequent Contributor

Its weird how the in program label expression builder when set to Python still uses [XYZ] for the fields. (in ArcMap 10.3)  Thanks, I wasn't aware of the startswith method, the code works now as:

def FindLabel ( [SITE] 😞
 if str([SITE]).startswith('18') == True:
  return " "
 else:
  return [SITE]