Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to update a field "TPN" based on another field "PASS_NUM". In my script "TPN" is a newly added number field and thus blank, "PASS_NUM" is also a number field but it contains the wrong numbers. I want to assign values to "TPN" when "PASS_NUM" is no longer equal the the value in the row above it. The feature class table is sorted by "PASS_NUM". See example of attribute table I want to achieve here:

enter image description here

Is there any way to check if the value in the row above is equal to the next value (some sort of onchange event)? The PASS_NUM field does not increment by 1 each time (i.e. it may skip 7 as in the example) but I want to TPN to only go up one. Is this possible using the UpdateCursor in arcpy? Or is there another way to achieve this through scripting?

Thanks Much!

share|improve this question

2 Answers 2

I don't know what your environment is nor how you are querying/updating the table, but conceptually, I'd store a copy of the previous value that I can use to check against in the next loop. Perhaps something like:

oldValue = 0

[code to query current value]

if not currentValue == oldValue:
    TPN = newValue
oldValue = currentValue
share|improve this answer

Try this out. The SQL query may need to be changed depending on your circumstances. I gave this a spin on a few small feature classes and seemed to do the job.

import arcpy

fc = "X:\...\feature_class"

# features may or may not be sorted in attribute table;
# append each value of PASS_NUM to list and sort
print "collecting pass_num values"
pass_numbers = []
for row in arcpy.da.SearchCursor(fc, "PASS_NUM"):
    if row[0] not in pass_numbers:
        pass_numbers.append(row[0])
pass_numbers.sort()
print "found %s values"%str(len(pass_numbers))

# TPN_value starts @ 1 and increases +1 with each pass
print "updating values"
TPN_value = 1
for number in pass_numbers:
    rows = arcpy.da.UpdateCursor(fc, ["TPN"], "PASS_NUM = %s"%str(number))
    for row in rows:
        row[0] = TPN_value
        rows.updateRow(row)
    del rows
    TPN_value += 1

print "finished"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.