test = str(contour)[-2:] #converts the last 2 digits of your contour value to a string if int(test)/10 == 1: then sort = 10 elif % int(test) 5 == 0: sort = 5 else: sort = 1
def Reclass(contour): newContour = str(contour) #string will have decimal newContour = newContour.split(".")[0] try: newContour[-2:] if newContour[-2:] == '10': return 10 except IndexError: pass if newContour[-1] == '5': return 5 else: return 1
Reclass(!Contour!)
def reclass(contours): if contours % 10 == 0: return 10 elif contours % 5 == 0: return 5 else: return 1 reclass( !Contours!)
This works for me (am I missing something?)
if contours % 10 == 0:
Converting to string then back to integers can be tricky. I ran into to some unsuspected errors when testing this...
Part of the problem is that we don't know how the OPs code "doesn't work" since that isn't stated in the original question.I assume it doesn't work because the statement
!sort! = 10;should be
return 10(and so on for the others).
Also returning a value that way still only means that function has to be wrapped into another function that actually updates the Sort field as the OP desires.This code is intended to be run in the field calculator (as per the OP).
won't trap only those contours that end with 10, just those that are evenly divisible by 10 - so the OP would get contours ending in 20, 30, 40, etc. being reclassified. Though its a bit unclear, I don't think that is his intention.You're right - it's unclear. But I assume they want all contours ending with a zero, not literally "10", so they can symbolize those contours differently. I could be wrong...
But I assume they want all contours ending with a zero, not literally "10", so they can symbolize those contours differently. I could be wrong...
This code is intended to be run in the field calculator (as per the OP).
I forget about how the code works inside the field calculator as i rarely do this sort of thing in that contex; does this code get run directly against the Sort field then and the returned value is assigned to that field? I was thinking there's still need to be a line of code where Sort is assigned a value. Seems there wouldn't need to be a function defined in that event.