Let's see what was problem with your approach!
Rules to be maintained:
- 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.
- 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-
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
#elif...
#elif...
#elif...
else:
label = None
return label
Explanation:
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.
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