Conditioning labels

4962
12
Jump to solution
02-10-2015 10:56 AM
Ulises
by
Occasional Contributor III

Hi everyone,

I'm trying to define an expression for labeling parcel polygons in ArcMap.  The feature class has 3 attributes of interest: CATEGORIA, NUM_CATASTRO, and PARCEL_SP.  If CATEGORIA (integer field with subtypes defined) is not 0 (N/A), then label the polygon with the value in PARCEL_SP, otherwise label with NUM_CATASTRO.  I'm not a programmer and I'm just starting with Python so I'm not sure what is missing.  So far I have...

def FindLabel (  [CATEGORIA],[NUM_CATASTRO], [PARCEL_SP] ):
  if [CATEGORIA] ==0:
    return [PARCEL_SP]
  else:
    return  [NUM_CATASTRO]

The issue is that I can see labels for NUM_CATASTRO where they should, but none for PARCEL_SP. 

If I reverse the values...

def FindLabel (  [CATEGORIA],[NUM_CATASTRO], [PARCEL_SP] ):
  if [CATEGORIA] ==0:
    return  [NUM_CATASTRO]
  else:
    return [PARCEL_SP]

it labels the PARCEL_SP where is supposed to but not the NUM_CATASTRO.

Same thing using the following statement...

def FindLabel (  [CATEGORIA],[NUM_CATASTRO], [PARCEL_SP] ):
  if [CATEGORIA] !=0:
    return   [PARCEL_SP]
  else:
    return  [NUM_CATASTRO]

Any help will be appreciated...

Thanks

Ulises Feliciano Troche
0 Kudos
1 Solution

Accepted Solutions
TomSellsted
MVP Regular Contributor

Ulises,

It looks like python automatically casts numeric field values as strings, so you would need to change your expression to

if [CATEGORIA] == "0":

or

if int([CATEGORIA]) == 0:

View solution in original post

12 Replies
TomSellsted
MVP Regular Contributor

Greeting Ulises!

It is most likely that the values in [CATEGORIA] are not what you are expecting them to be in python.  If the field is not numeric, it will never evaluate to 0, but a text value of  "0".  It may be a Null value and again it would not evaluate to be 0.  Your logic looks sound, please check the values and make sure it is the right field type.

Regards,

Tom

0 Kudos
Ulises
by
Occasional Contributor III

Thanks for the reply, Tom.

Field CATEGORIA is numeric, yet I'm assuming is behaving like it isn't, yet haven't been able to identify why.

Ulises Feliciano Troche
0 Kudos
TomSellsted
MVP Regular Contributor

Ulises,

Are there 0 values in the field [CATEGORIA] or could it be a value of null?

Regards,

Tom

0 Kudos
Ulises
by
Occasional Contributor III

No null values...

Ulises Feliciano Troche
0 Kudos
TomSellsted
MVP Regular Contributor

Ulises,

It looks like python automatically casts numeric field values as strings, so you would need to change your expression to

if [CATEGORIA] == "0":

or

if int([CATEGORIA]) == 0:

Ulises
by
Occasional Contributor III

Thanks again.  Is not working either way.  First suggestion "0" still behaves as mentioned earlier and second option (using int) returned an error...

ValueError: invalid literal for int() with base 10:'N/A'

N/A is the descriptive value for the subtype coded with the integer 0.  Also changed the expression to evaluate against "N/A" with no success.

Ulises Feliciano Troche
0 Kudos
TedKowal
Occasional Contributor III

Your logic look good!

If you are sure that your Catagoria is numeric not null ect..... Then have tried another logical method such as :

def FindLabel (  [CATEGORIA],[NUM_CATASTRO], [PARCEL_SP] ):  
  if [CATEGORIA] > 0:  
    return [PARCEL_SP]  
  else:  
    return  [NUM_CATASTRO]

?

0 Kudos
Zeke
by
Regular Contributor III

I don't think python would be casting numeric data types to strings. That doesn't make sense.

0 Kudos
TomSellsted
MVP Regular Contributor

Greg,

That is straight from the "Building Label Expressions" in the ArcGIS Documentation.  I did a test on my data and it absolutely does cast the field values into strings.

Regards,

Tom

0 Kudos