Error Labelling Features Using Basic Python

3610
4
Jump to solution
03-08-2015 08:44 PM
BenVan_Kesteren1
Occasional Contributor III

Hi,

I am using the below code to label contours of a small area. Due to laziness and simplicity, this polyline featureclass has both the contours AND pipes together, but they are easy to identify, as all pipes have a description in the notes field, and they have NULL in the elevation field, and visa versa.

Due to these fields being this way, I am hoping to setup a simple python script to label the contours with ELEVATION and pipes with NOTES attribute.

I have my code working 99%, but as the contours are so close together I want to only show every 2nd line, so they are all contours with elevation / 0.5 with no remainder.

EG it would only return these: 150.00  150.25  150.50  150.75  151.00 151.25......

This code is giving me an error:

def FindLabel ( [ELEVATION], [NOTES] ):
  el = [ELEVATION]
  nt = [NOTES]
  if el > 0:
    if (el%0.5)==0:
      return el # returns the elevation as it contains a value
  else:
    return nt # returns [NOTES] as label

Error:

2015-03-09_14-41-03_Weave_Alternate.mxd - ArcMap.jpg

I can remove the line 'if (el%0.5)==0:' and it works fine (just a bit messy). So i know this line contains the problem.

Can anyone tell me where I have gone wrong please?

Thanks

0 Kudos
1 Solution

Accepted Solutions
OwenEarley
Occasional Contributor III

Cast your elevation value to a float and it should work:

def FindLabel ( [ELEVATION] , [NOTES] ):
  el = [ELEVATION]
  nt = [NOTES]
  if el > 0:  
    if (float(el)%0.5)==0: 
      return el
  else: 
    return nt

View solution in original post

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

I think it may be the way you have set up your if tests

1  what happens if el isn't greater than 0?​  is it supposed to return nt?

2 what happens if el % 0.5 isn't == 0 ? (put some spaces around operands by the way) it kind of goes nowhere right now and it may be getting confused, so put an else statement to finish the though on that comparison

...

if ( el % 0.5) == 0.0:  # shouldn't be testing for equality but within a tolerance

   do something

else:

   do something else

also are el and nt both strings? are they supposed to be returned as strings?

0 Kudos
BenVan_Kesteren1
Occasional Contributor III

el is a double, and notes is string.

Good point with the else, I forgot to put one!!

0 Kudos
OwenEarley
Occasional Contributor III

Cast your elevation value to a float and it should work:

def FindLabel ( [ELEVATION] , [NOTES] ):
  el = [ELEVATION]
  nt = [NOTES]
  if el > 0:  
    if (float(el)%0.5)==0: 
      return el
  else: 
    return nt
0 Kudos
BenVan_Kesteren1
Occasional Contributor III

yes this float() fixed the error. Combining your suggestion with an else: statement it now runs flawlessly...

def FindLabel ( [ELEVATION], [NOTES] ):
  el = [ELEVATION]
  nt = [NOTES]
  div = 1
  if float(el) > 0:
    if (float(el)%div)==0.0:
      return el # returns the elevation as it contains a value
    else:pass
  else:
    return [NOTES] # returns [NOTES] as label