To start from the end:
PS: Not sure why code indentation is getting messed up when I paste it here...
If you enclose your code in code tags the indentation will be preserved. To get code tags, just press the "#"-button when you write your message.Now to your problem:I would start by splitting up the logic in two parts: The looping, and the categorizing of the contour lines, with a function for the latter:
# untested, probably needs a bit of debugging, and typo fixing
CONTOUR_CLASSES = ((20, '5m Contour'),
(4, '1m Contour'),
# Do you really want the next class? Remove to group with 0.25m contours
(3, '0.75m Contour'),
(2, '0.5m Contour'),
(1, '0.25m Contour'))
def classify_contour_value(elevation):
for cc, cc_desc in CONTOUR_CLASSES:
if int(elevation * 4) % cc == 0: # if dividable by cc it belongs to that class
return cc_desc
The looping needs some improvement too:
fc = r"D:/LIDAR/Lidar.gdb/Contour_pt25m"
# Creates the cursor
rows = gp.UpdateCursor(fc)
# Looping gets so much simpler if you let python do the work for you:
for row in iter(rows.Next, None): # Convert the geoprocessing iterator to a python iterator
con_value = row.CONTOUR # shouldn't be named 'txt' if is a floating point value...
con_txt = classify_contour_value(con_value):
row.Layer = con_txt
rows.UpdateRow(row)
# row = rows.Next() # So easy to forget this line and you get an infinite loop.
# with a for-loop instead you transfer the resposiblity for this call to the python internals
# Clean up cursors
##del rows
##del row
# Since the python del statement doesn't delete anything, I prefer setting values to None, but
# that is mostly a matter of taste:
row = None
rows = None
My question is:
Is this the cleanest way around this problem? Or is there some other python function I should use?
There are other ways to do it too. Can't tell you which way is "THE cleanest". There certainly isn't a core python function for classifying elevation values in your way, so some coding is necessary, no matter what.