Using Python Labeling Expressions in ArcGIS Pro

9657
4
Jump to solution
01-12-2018 07:48 AM
JosephGerland
New Contributor

Hello, I am definitely a novice when it comes to scripting. I know there is a way to do what I am asking, but I lack the skills to do it right now.

I have a wetlands shapefile that has many wetland features in it that need to be labeled according to their attributes. What I mean by this is: 

All forested wetland features need to be labeled a dark green Arial 10 pt. bold with a 1 pt. white halo

All riverine features need to be labeled a deep blue Arial 10 pt. bold with 1 pt. white halo

All ponds need to be labeled a light blue Arial 10 pt. bold with 1 pt. white halo

etc..

The shapefile has a field in the attribute table called "ATTRIBUTE" which contains NWI code for what type of wetland the feature is.

So, forested wetland's "ATTRIBUTE" field starts with the letters "PFO"

Riverine features' "ATTRIBUTE" field starts with the letter "R"

Ponds' "ATTRIBUTE" field starts with "PUB"

and etc...

The actual NWI code in the "ATTRIBUTE" field is longer, but the first three letters or so is what distinguishes a forested wetland from a scrub-shrub wetland 

ex. PFO1C is a forested wetland while PSS1C is a scrub-shrub wetland. So, I just need the script to use a "begins with" type distinguishment.

I have used SQL to define classes in the Label Manager and then manually modified the text symbol for each class, but what would really help is to be able to have a python script saved that can do the symbols and the classifying in one step so-to-speak.

Not sure if anyone needs this, but the SQL expression I have been using in each label class is simply:

   ATTRIBUTE LIKE 'PFO%'

and I just substitute "PFO" for whatever I need for that labeling class.

I attached an image of what I want to do. I hope for the same result as this (which I did by creating label classes and using the simple SQL expression above in each class and then manually modifying the symbols in each class) but just in a single python script. Could anybody help me get started? I just don't understand how to structure the script or use appropriate syntax.

Thanks so much!An example of the type of labeling I would like to automate.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Actually you can use halo and other effects, since as far as I can deduce all the labels will have the same halo setting. The only thing that needs to be influenced is the color. The effect you can get is this:

What you will need to do is this. 

1) Define manually the symbol for the labels to be Arial 10pt and Halo white 1pt. This is what all labels have in common 

2) Define the class for the label like this:

The code is basically this:

def FindLabel ( [Wetlands] ):
    label_text = [Wetlands]
    dct_startswith = {'PFO': "<CLR red='0' green='100' blue='30'>",
                      'R': "<CLR red='30' green='80' blue='160'>",
                      'PUB': "<CLR red='50' green='200' blue='240'>"}

    color = "<CLR red='130' green='130' blue='130'>"
    for start in dct_startswith.keys():
        if label_text.upper().startswith(start):
            color = dct_startswith[start]

    return color + label_text + "</CLR>"

What it does is read the attribute with the wetlands values (line 1 and 2). Next it defines a dictionary that in this case has 3 keys (the values we want to check the start of the wetlands class on). See lines 3 to 5.

As default color we define a light grey color RGB intensity will be set to 130 (see line 7) and then we start to loop through the keys (PFO, R and PUB) and we check if the wetland value starts with this value. If so the color is adjusted accordingly.

At the end we return the color setting, along with the actual label text and the closing tag for the color.

And that is basically all you need to do. Change the colors as you please and add additional values to the dictionary if you need to distinguish more classes. 

View solution in original post

4 Replies
VincentLaunstorfer
Occasional Contributor III

Hi,

It looks like you need a Python code block with "if" conditional statments to decode the first 3 letters of your ATTRIBUTE field, as shown here:

All details available here:

Specify text for labels—ArcGIS Pro | ArcGIS Desktop 

With the <FNT></FNT> pseudo HTML tages, you can apply some formatting to the labels such as color, size, police... but it will be limited. To me, it won't be possible to apply Halo, Callout and any advanced formatting...

Label classes as you did was the right way.

Hope this help

XanderBakker
Esri Esteemed Contributor

Actually you can use halo and other effects, since as far as I can deduce all the labels will have the same halo setting. The only thing that needs to be influenced is the color. The effect you can get is this:

What you will need to do is this. 

1) Define manually the symbol for the labels to be Arial 10pt and Halo white 1pt. This is what all labels have in common 

2) Define the class for the label like this:

The code is basically this:

def FindLabel ( [Wetlands] ):
    label_text = [Wetlands]
    dct_startswith = {'PFO': "<CLR red='0' green='100' blue='30'>",
                      'R': "<CLR red='30' green='80' blue='160'>",
                      'PUB': "<CLR red='50' green='200' blue='240'>"}

    color = "<CLR red='130' green='130' blue='130'>"
    for start in dct_startswith.keys():
        if label_text.upper().startswith(start):
            color = dct_startswith[start]

    return color + label_text + "</CLR>"

What it does is read the attribute with the wetlands values (line 1 and 2). Next it defines a dictionary that in this case has 3 keys (the values we want to check the start of the wetlands class on). See lines 3 to 5.

As default color we define a light grey color RGB intensity will be set to 130 (see line 7) and then we start to loop through the keys (PFO, R and PUB) and we check if the wetland value starts with this value. If so the color is adjusted accordingly.

At the end we return the color setting, along with the actual label text and the closing tag for the color.

And that is basically all you need to do. Change the colors as you please and add additional values to the dictionary if you need to distinguish more classes. 

JosephGerland
New Contributor

Awesome, that works perfectly! Thanks!

0 Kudos
anyry
by
New Contributor III

Is it possible to set halo only using python?

0 Kudos