5

I'm trying to label certain features using a specific field (osm_name_58_en). However, when this field is null, I want to label the feature with (som_english_32_name). If both are null then I want to label with the (amenity) field.

This is the code I'm trying but I'm not getting anything in return. Maybe I'm completely out to left field? I'm a python and advanced labeling newbie.

def FindLabel ( [amenity], [osm_english_32_name], [osm_name_58_en] ):
    if not ([osm_name_58_en] is None):
        return [osm_name_58_en]
    elif ([osm_name_58_en] is None and not([osm_english_32_name] is None)):
        return [osm_english_32_name] + '\n' + [amenity] 
    else:
        return [amenity];
5
  • I believe you're looking for isnull or not isnull rather than none.
    – Chris W
    Commented May 21, 2015 at 17:46
  • 1
    @Chris W, there is no such thing as "null" in python.The python version of null is in fact none.
    – papadoo
    Commented May 21, 2015 at 17:48
  • @papadoo You're right. I'm not a python guy and was mostly going off the standard SQL expression formulas typically used in field calculator and labeling. The answer at gis.stackexchange.com/questions/73009 would suggest None needs to be in quotes.
    – Chris W
    Commented May 21, 2015 at 17:53
  • 1
    @Alex Parisien The first check you are doing in your elif statement is not necessary - in order for you to get to the elif, you already know that [osm_name_58_en] is None or else the if conditional would have been met. All you need to check is elif [osm_english_32_name] is not None:
    – papadoo
    Commented May 21, 2015 at 17:58
  • @Alex. Can you include a picture of the table?
    – DWynne
    Commented May 21, 2015 at 18:05

2 Answers 2

3

The Correct form of your code is :

def FindLabel ( [amenity], [osm_english_32_name], [osm_name_58_en] ):
    if not ([osm_name_58_en] is None):
        return [osm_name_58_en]
    elif not([osm_english_32_name] is None):
        return [osm_english_32_name] + '\n' + [amenity] 
    else:
        return [amenity]

The semicolon in the last line is removed. and your elif statement is repaired

2
  • Thank you for all your help! Simplifying the elif statement and removing the semicolon made it work. I have a lot of python tutorials to get through I think - Thank you for the responses! Commented May 22, 2015 at 12:52
  • 1
    Consider reading SE Tour, and marking one of answer as correct for future visitors Commented May 22, 2015 at 12:57
1

The code presented in your question doesn't match what is asked for in the question.

"I'm trying to label certain features using a specific field (osm_name_58_en). However, when this field is null, I want to label the feature with (som_english_32_name). If both are null then I want to label with the (amenity) field."

This sentence implies that you only want one field for any given label.

If this is the case the following code will do what you want.

def FindLabel ( [amenity], [osm_english_32_name], [osm_name_58_en] ):
    if not [osm_name_58_en] is None:
        return [osm_name_58_en]
    elif not [osm_english_32_name] is None:
        return [osm_english_32_name]
    elif not [amenity] is None:
        return [amenity]  

Now, if you want to get the code in the question working correctly here is what you need.

def FindLabel ( [amenity], [osm_english_32_name], [osm_name_58_en] ):
    if not ([osm_name_58_en] is None):
        return [osm_name_58_en]
    elif not([osm_english_32_name] is None):
        return [osm_english_32_name] + '\n' + [amenity] 
    else:
        return [amenity]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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