6

I'm using this python script as an expression for labeling in ArcMap:

def FindLabel ( [volkorrekt] ):
 if [volkorrekt] >= '5':
  return "<FNT size = '20'>"+  str( [volkorrekt] )+"</FNT>"
 elif [volkorrekt] >= '2':
   return  "<FNT size = '14'>"+  str( [volkorrekt] )+"</FNT>"
 elif [volkorrekt] >= '1':
  return "<FNT size = '8'>"+  str( [volkorrekt] )+"</FNT>"
 else:
  return ""

I want ArcMap to only label the feature if the value volkorrekt is bigger than 1, this is working. I also want ArcMap to change the font size according to my if-statement (font size 20 if value >= 5, 14 if >= 2 and 8 if >= 1) but this only works for some but not for all features.

This is my current result:

enter image description here

Examples for wrong labels: The "19,487..." is displayed very small though it's one of the biggest values, the "9,552..." however is displayed much to large.

How can I get ArcMap to label the features in the way I described?

3

I think you are mixing numbers and text. What type of field is vollkorrekt?

As it is now, you are comparing the value to a string, i.e. [vollkorrekt] >= '1'. The comparison is therefore alphabetical instead of numeric, meaning that '19' is smaller than '5', but bigger than '1'.

If the type of the field is numeric, you only have to remove the single quotes: if [volkorrekt] >= 5: etc.

If the type of the field is text, you have to convert the value to a float: if float([volkorrekt]) >= 5:

Here's the complete code, modified to increase readability:

def FindLabel( [volkorrekt] ):
    value = float( [volkorrekt].replace(',', '.' ))
    if value >= 5:
        size = 20
    elif value >= 2:
        size = 14
    elif value >= 1:
        size = 8
    else:
        return ""

    return "<FNT size='{0}'>{1}</FNT>".format(size, value)
  • The type of volkorrekt is double. I removed the single quotes, now all of my labels are in font size 20 AND also the values < 1 are displayed. – Simon Mar 15 '16 at 14:57
  • @Simon Sorry, my bad. I should actually have tried the code in ArcGIS. It seems that the value is passed to the FindLabel methode as a string already, even if the field is text. You need to convert it back to a float, taking into account the system's decimal character. – Berend Mar 15 '16 at 15:52
8

This is because you try to use > as a character in your label, as well as for formatting/programming your label. Nothing wrong with that, it's how it works. But if you want to use those characters in your label you need to escape them.

Instead of the actual characther you need to use &lt; (<), &gt; (>) and &amp; (&).

An alternative approach is to use label classes. Then you can have SQL queries instead of your IF statements, which IMO is easier to control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.