I try to label a point layer for values in lines 1-11 only in this attribute table in field "OBJECTID" with this syntax (i saw this question also Labeling specific features in ArcGIS for Desktop?):

def FindLabel ( [OBJECTID]):
    for [OBJECTID]:
        return [OBJECTID] [0:10]

but i get an error:

syntax error: invaild syntax (< string>, line 2)


i get this result when using ahmadhanb SQL query:

enter image description here


Instead of using expression, use the Method "Define classes of features and labels each class differently" -> Use SQL Query and define the range of objectID that you want to label. For example in your case, it will be "ObjectID" >= 1 AND "ObjectID" <= 10. Then Choose field_name as the desired Label Field:

enter image description here

enter image description here


Let's see what was problem with your approach!

Rules to be maintained:

  1. Label expression function gets value one by one-So you can not use indexing to get single objectid value from a list of objectid values.
  2. If you want to work with the ObjectID field then you have to cast this field explicitely since it is not python data type as mentioned here.So to use this field in the expression function use int/long casting/conversion.

see the image-demo

Now lets proceed: If I maintain the above rules and write a function as below this will work :)

def FindLabel ([OBJECTID]):
    if long([OBJECTID])>1 and long([OBJECTID]) <=10:
        label = [OBJECTID]
    elif long([OBJECTID])>10 and long([OBJECTID]) <=20:
        #do something
    elif long([OBJECTID])>20 and long([OBJECTID]) <=30:
        #do something

        label = None
    return  label


When I run the above function, It gets one objectid values and checks the condition as stated if long([OBJECTID])>1 and long([OBJECTID]) <=10 if it passes the label variable is populated otherwise set to None.

Other non-programmatic way:

You can use the Maplex Labelling Engine, which is the de-facto standard for all complex and effective labelling. demo

Comparision between two ways:

If you like programming-

I will say,if you use expression you will have a fine grained test and more control.So it can be said that all types of customised labelling can be attained using expression but using SQL you will not be able to solve some problem e.g. each lablel should have the common tailing text, like X district, Y district, Z district

If you do not like programming-

In some case, blend of both i.e. expression and SQL will give you perfect choice e.g. you need to label all OBJECTID (2-10) and each lablel should have the common tailing text, like X district, Y district, Z district

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.