Generating Contour types using contour value

352
1
03-28-2011 03:38 PM
DustinEdge
Occasional Contributor
Greetings All

I've looked around and can't seem to find a ready-made solution to my problem. (Maybe I'm looking in the wrong place)

Anyhoo....I have generated a whole bunch of contours from a DEM and would like an attribute that would separate the contours into: 5m contours, 1m contours etc.

I fudged a python script together that takes the contour value, separates the fraction from the whole number, evaluates the the fraction and then populates the Layer field.

My question is:
Is this the cleanest way around this problem? Or is there some other python function I should use?

This is my code....
fc = r"D:/LIDAR/Lidar.gdb/Contour_pt25m"

# Creates the cursor
rows = gp.UpdateCursor(fc)
row = rows.Next()

# takes each contour value, separates the decimal from the whole number...
#  evaluates it and then assigns the index type to the LAYER field

while row:
    ConTxt = row.CONTOUR
    TxtSplt = math.modf(ConTxt)
     if TxtSplt[0]==0.5:
        row.Layer = "0.5m Contour"
    elif TxtSplt[0]==0.25:
        row.Layer = "0.25m Contour"
    elif TxtSplt[0]==0.75:
        row.Layer = "0.75m Contour"
    elif TxtSplt[0]==0.0:
        Txt2 = float(ConTxt)/5
        IntTxt = math.modf(Txt2)
        if IntTxt[0]==0:
            row.Layer = "5m Contour"
        else:
            row.Layer = "1m Contour"
    rows.UpdateRow(row)
    row = rows.Next()

# Clean up cursors
del rows
del row


Thanks in advance

Dustin

PS: Not sure why code indentation is getting messed up when I paste it here...
Tags (2)
0 Kudos
1 Reply
NiklasNorrthon
Occasional Contributor III
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.
0 Kudos