<?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: Increment Numbers with Sort Field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386967#M30532</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;If the field isn't indexed and has a lot of unique variables the sort can take a while. I wouldn't think it would be in the order of 8 minutes though. The 9.3 cursors were not quite as fast if I recall. If you are reading the feature class in the update cursor instead of a layer how you are with the field calculator that can also have an impact.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I figured the indexed may make a difference so I used the ObjectID field when I ran it.&amp;nbsp; I'll attempt to rewrite it using arcpy and see how it turns out.&amp;nbsp; Thanks for the feedback on this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Alan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 06 Jun 2012 13:04:55 GMT</pubDate>
    <dc:creator>AlanToms</dc:creator>
    <dc:date>2012-06-06T13:04:55Z</dc:date>
    <item>
      <title>Increment Numbers with Sort Field</title>
      <link>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386963#M30528</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would like to create code to calculate a field with increment numbers, similar to this &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/2937-auto-sequential-numbers-with-python"&gt;code&lt;/A&gt;&lt;SPAN&gt;, but with an option of a sort order.&amp;nbsp; I�??m thinking I could use an UpdateCursor with a sort and loop through the records, but I assume the performance would suffer vs a strait up field calculation.&amp;nbsp; Another option I was considering was a SearchCursor then calling the Calculate Field geoprocess for each record.&amp;nbsp; I don't think this would improve performance though.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are there any suggestions or thoughts?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Alan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jun 2012 11:58:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386963#M30528</guid>
      <dc:creator>AlanToms</dc:creator>
      <dc:date>2012-06-04T11:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: Increment Numbers with Sort Field</title>
      <link>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386964#M30529</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Cursors are much faster than field calculations, so performance would improve using an UpdateCursor.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jun 2012 12:37:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386964#M30529</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-06-04T12:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Increment Numbers with Sort Field</title>
      <link>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386965#M30530</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Cursors are much faster than field calculations, so performance would improve using an UpdateCursor.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a feature class with 266,927 records which I used to test the field calculation and the UpdateCursor.&amp;nbsp; The field calculation was substantially faster than the UpdateCursor.&amp;nbsp; The field calculation completed in 37 seconds.&amp;nbsp; The UpdateCursor took 8 minutes and 50 seconds.&amp;nbsp; My code is a bit more complicated because it includes a sort order, but I wouldn�??t expect to be that much of a performance hit.&amp;nbsp; If UpdateCursor is supposed to be quicker any idea where I may have slowed the process down?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Field Calculation&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rec=0
def autoIncrement():
 global rec
 pStart = 1 #adjust start value, if req'd 
 pInterval = 1 #adjust interval value, if req'd
 if (rec == 0): 
&amp;nbsp; rec = pStart 
 else: 
&amp;nbsp; rec = rec + pInterval 
 return rec&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My UpdateCursor code&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Input Variables
inputFC = gp.GetParameterAsText(0) #Input Feature Class
inputField = gp.GetParameterAsText(1) #Field to calculate
inputStart = gp.GetParameterAsText(2) #Starting value.&amp;nbsp; Default value of 1 in ArcToolbox
inputIncrement = gp.GetParameterAsText(3) #Incement value. Default value of 1 in ArcToolbox
inputSortField = gp.GetParameterAsText(4) #Field to sort by
inputSortOrder = gp.getparameterastext(5) #Sort order. Value List (Ascending, Descending) with a default value of Ascending in ArcToolbox

def sortVariable(xSortField,xSortOrder):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if xSortOrder == "Descending":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return xSortField + " D"
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return xSortField + " A"

# UpdateCursor
def incrementNumbers(vFC,vField,vStart,vIncrement,vFieldList,vSort):
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value = int(vStart)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cur = gp.UpdateCursor(vFC,"","",vFieldList,vSort)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = cur.Next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while row:
&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; row.setValue(vField,value)
&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; cur.UpdateRow(row)
&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; row = cur.Next()
&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; value = value + int(vIncrement)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Warning messages
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print gp.AddWarning(gp.GetMessages(1))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Error messages
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print gp.AddError(gp.GetMessages(2))
&amp;nbsp;&amp;nbsp;&amp;nbsp; print gp.AddMessage("Complete")


# Variable of fields to use in the UpdateCursor
qFieldList = inputField + "," + inputSortField

# Variable for sort to use in the UpdateCursor
qSort = sortVariable(inputSortField,inputSortOrder)

incrementNumbers(inputFC,inputField,inputStart,inputIncrement,qFieldList,qSort)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:46:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386965#M30530</guid>
      <dc:creator>AlanToms</dc:creator>
      <dc:date>2021-12-11T17:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: Increment Numbers with Sort Field</title>
      <link>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386966#M30531</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If the field isn't indexed and has a lot of unique variables the sort can take a while. I wouldn't think it would be in the order of 8 minutes though. The 9.3 cursors were not quite as fast if I recall. If you are reading the feature class in the update cursor instead of a layer how you are with the field calculator that can also have an impact.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Jun 2012 12:24:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386966#M30531</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-06-06T12:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Increment Numbers with Sort Field</title>
      <link>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386967#M30532</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;If the field isn't indexed and has a lot of unique variables the sort can take a while. I wouldn't think it would be in the order of 8 minutes though. The 9.3 cursors were not quite as fast if I recall. If you are reading the feature class in the update cursor instead of a layer how you are with the field calculator that can also have an impact.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I figured the indexed may make a difference so I used the ObjectID field when I ran it.&amp;nbsp; I'll attempt to rewrite it using arcpy and see how it turns out.&amp;nbsp; Thanks for the feedback on this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Alan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Jun 2012 13:04:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/increment-numbers-with-sort-field/m-p/386967#M30532</guid>
      <dc:creator>AlanToms</dc:creator>
      <dc:date>2012-06-06T13:04:55Z</dc:date>
    </item>
  </channel>
</rss>

