deanrobbins

Multi-row feature/shape table computations

Discussion created by deanrobbins on Feb 22, 2012
I have seen few examples or methods that worked the way that I needed them to.  So, after some playing, I was able to use arcpy cursors in their simplest form in conjunction with Python lists to compute values across multiple rows under a single field.  My application was for determining distances between points for grade calculations and assignment of elevation data to those points for design purposes.  Every time that I tried to use multiple cursors on the same dataset, it did not work.  I am assuming that once a cursor is created, a lock prevents other cursors from accessing the same data (even if they are searchCursors -> readOnly).  Below is the code.  Enjoy!

"""
Use an initial SearchCursor to populate a Python list with the field values.
Loop through the Python list and create a newList of derived values.
Use the newList with an UpdateCursor to populate the field of interest.
"""

import arcpy
print "Creating SearchCursor..."
searchRows = arcpy.SearchCursor("designFeatures", "", "", "MEAS", "MEAS D")
rowList = []
print "Appending searchCursor row fields to new Python list..."
for each in searchRows:
    rowList.append(each.MEAS)
print "Appending operation completed."
print "Initial Python list: " + str(rowList)

newList = []
i = 0
j = 1
print "Computing alignment segment lengths for design purposes..."
if i < len(rowList):
    print "Appending calculated values to a new Python list..."
    for each in rowList:
        if j == len(rowList):
            newList.append(rowList[i])
            break
        else:
            someCalculation = rowList[i] - rowList[j]
            newList.append(someCalculation)
            i += 1
            j += 1
print "rowList calculations completed and appended to newList."
print "New List: " + str(newList)


print "Creating update cursor..."
updateRows = arcpy.UpdateCursor("designFeatures", "", "", "MEAS; SEG_LENGTH", "MEAS D")


print "Updating SEG_LENGTH field..."
##for each in newList:
##    for every in updateRows:
##        every.SEG_LENGTH = each
i = 0
for each in updateRows:
    if i < len(newList):
        each.SEG_LENGTH = newList[i]
        updateRows.updateRow(each)
        i += 1
print "SEG_LENGTH field rows have been updated."
print "Creating SearchCursor for field value verification..."
verifyRows = arcpy.SearchCursor("designFeatures", "", "", "MEAS; SEG_LENGTH", "MEAS D")


for each in verifyRows:
    print each.SEG_LENGTH


del searchRows, updateRows, verifyRows

Outcomes