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.
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).
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.
!sort! = 10;
return 10
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.
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.
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...
def reclass(contours): if contours % 10 == 0: return 10 elif contours % 5 == 0: return 5 else: return 1 reclass( !Contours!)
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!)
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
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.