<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Generating Contour types using contour value in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/generating-contour-types-using-contour-value/m-p/683313#M52920</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To start from the end:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;PS: Not sure why code indentation is getting messed up when I paste it here...&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now to your problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# untested, probably needs a bit of debugging, and typo fixing

CONTOUR_CLASSES = ((20, '5m Contour'),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (4, '1m Contour'),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Do you really want the next class? Remove to group with 0.25m contours
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (3, '0.75m Contour'), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (2, '0.5m Contour'),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (1, '0.25m Contour'))

def classify_contour_value(elevation):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for cc, cc_desc in CONTOUR_CLASSES:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(elevation * 4) % cc == 0: # if dividable by cc it belongs to that class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return cc_desc
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The looping needs some improvement too:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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
&amp;nbsp;&amp;nbsp;&amp;nbsp; con_value = row.CONTOUR # shouldn't be named 'txt' if is a floating point value...
&amp;nbsp;&amp;nbsp;&amp;nbsp; con_txt = classify_contour_value(con_value):
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = con_txt
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;My question is:&lt;BR /&gt;Is this the cleanest way around this problem? Or is there some other python function I should use?&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:44:49 GMT</pubDate>
    <dc:creator>NiklasNorrthon</dc:creator>
    <dc:date>2021-12-12T04:44:49Z</dc:date>
    <item>
      <title>Generating Contour types using contour value</title>
      <link>https://community.esri.com/t5/python-questions/generating-contour-types-using-contour-value/m-p/683312#M52919</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Greetings All&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My question is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Is this the cleanest way around this problem? Or is there some other python function I should use?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my code....&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-style:italic;"&gt;fc = r"D:/LIDAR/Lidar.gdb/Contour_pt25m"&lt;BR /&gt;&lt;BR /&gt;# Creates the cursor&lt;BR /&gt;rows = gp.UpdateCursor(fc)&lt;BR /&gt;row = rows.Next()&lt;BR /&gt;&lt;BR /&gt;# takes each contour value, separates the decimal from the whole number...&lt;BR /&gt;#&amp;nbsp; evaluates it and then assigns the index type to the LAYER field&lt;BR /&gt;&lt;BR /&gt;while row:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ConTxt = row.CONTOUR&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TxtSplt = math.modf(ConTxt)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if TxtSplt[0]==0.5:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = "0.5m Contour"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif TxtSplt[0]==0.25:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = "0.25m Contour"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif TxtSplt[0]==0.75:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = "0.75m Contour"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif TxtSplt[0]==0.0:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Txt2 = float(ConTxt)/5&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IntTxt = math.modf(Txt2)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if IntTxt[0]==0:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = "5m Contour"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = "1m Contour"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.Next()&lt;BR /&gt;&lt;BR /&gt;# Clean up cursors&lt;BR /&gt;del rows&lt;BR /&gt;del row&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dustin&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS: Not sure why code indentation is getting messed up when I paste it here...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Mar 2011 22:38:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-contour-types-using-contour-value/m-p/683312#M52919</guid>
      <dc:creator>DustinEdge</dc:creator>
      <dc:date>2011-03-28T22:38:53Z</dc:date>
    </item>
    <item>
      <title>Re: Generating Contour types using contour value</title>
      <link>https://community.esri.com/t5/python-questions/generating-contour-types-using-contour-value/m-p/683313#M52920</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To start from the end:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;PS: Not sure why code indentation is getting messed up when I paste it here...&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now to your problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# untested, probably needs a bit of debugging, and typo fixing

CONTOUR_CLASSES = ((20, '5m Contour'),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (4, '1m Contour'),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Do you really want the next class? Remove to group with 0.25m contours
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (3, '0.75m Contour'), 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (2, '0.5m Contour'),
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (1, '0.25m Contour'))

def classify_contour_value(elevation):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for cc, cc_desc in CONTOUR_CLASSES:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if int(elevation * 4) % cc == 0: # if dividable by cc it belongs to that class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return cc_desc
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The looping needs some improvement too:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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
&amp;nbsp;&amp;nbsp;&amp;nbsp; con_value = row.CONTOUR # shouldn't be named 'txt' if is a floating point value...
&amp;nbsp;&amp;nbsp;&amp;nbsp; con_txt = classify_contour_value(con_value):
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Layer = con_txt
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;My question is:&lt;BR /&gt;Is this the cleanest way around this problem? Or is there some other python function I should use?&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:44:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generating-contour-types-using-contour-value/m-p/683313#M52920</guid>
      <dc:creator>NiklasNorrthon</dc:creator>
      <dc:date>2021-12-12T04:44:49Z</dc:date>
    </item>
  </channel>
</rss>

