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:
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
Solved! Go to Solution.
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
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?
el is a double, and notes is string.
Good point with the else, I forgot to put one!!
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
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