Increment Numbers with Sort Field

447
4
06-04-2012 04:58 AM
AlanToms
Occasional Contributor
Hello All,

I would like to create code to calculate a field with increment numbers, similar to this code, but with an option of a sort order.  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.  Another option I was considering was a SearchCursor then calling the Calculate Field geoprocess for each record.  I don't think this would improve performance though.

Are there any suggestions or thoughts?


Thank you
Alan
Tags (2)
0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
Cursors are much faster than field calculations, so performance would improve using an UpdateCursor.
0 Kudos
AlanToms
Occasional Contributor
Cursors are much faster than field calculations, so performance would improve using an UpdateCursor.


I have a feature class with 266,927 records which I used to test the field calculation and the UpdateCursor.  The field calculation was substantially faster than the UpdateCursor.  The field calculation completed in 37 seconds.  The UpdateCursor took 8 minutes and 50 seconds.  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.  If UpdateCursor is supposed to be quicker any idea where I may have slowed the process down?

Field Calculation
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): 
  rec = pStart 
 else: 
  rec = rec + pInterval 
 return rec


My UpdateCursor code
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.  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):
    if xSortOrder == "Descending":
        return xSortField + " D"
    else:
        return xSortField + " A"

# UpdateCursor
def incrementNumbers(vFC,vField,vStart,vIncrement,vFieldList,vSort):
    try:
        value = int(vStart)
        cur = gp.UpdateCursor(vFC,"","",vFieldList,vSort)
        row = cur.Next()
        while row:
                row.setValue(vField,value)
                cur.UpdateRow(row)
                row = cur.Next()
                value = value + int(vIncrement)
    except:      
        # Warning messages
        print gp.AddWarning(gp.GetMessages(1))
        # Error messages
        print gp.AddError(gp.GetMessages(2))
    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)
0 Kudos
MathewCoyle
Frequent Contributor
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.
0 Kudos
AlanToms
Occasional Contributor
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.


I figured the indexed may make a difference so I used the ObjectID field when I ran it.  I'll attempt to rewrite it using arcpy and see how it turns out.  Thanks for the feedback on this.

Alan
0 Kudos