import arcpy
fc = r'D:\s\py\pyscratch.gdb\gps_points'
# open the feature class and create the cursor
rows = arcpy.UpdateCursor(fc)
trip = 0
for row in rows:
if row.HOME_AWAY == 0:
trip += 1 # start of new trip, increment counter
row.TRIP = trip # calc the TRIP field to be current trip#
rows.updateRow(row) # save
print "Trip %s started at %s" % (trip, row.TIME)
# keep cycling through records until HOME_AWAY is not 1
while row.HOME_AWAY == 1:
row.TRIP = trip
rows.updateRow(row)
rows.next() # move to next record
# this is for the trailing end of a trip, the second 0
# print " %s ended at %s" % (trip, row.TIME)
row.TRIP = trip
rows.updateRow(row)
# remove programming objects and data locks
# the data itself is left alone
del row, rows
Python automatically adds an implicit rows.next()
at the end for the for row in rows
block.
This assumes data integrity. It will mess up if there are ever an odd number of zero Home/Away records in a row (000
or 00000
). A trip that only consists of start and stop should be okay, e.g. a 3 trip sequence of 01..10 00 01..10
, where the spaces denote the gaps between trips. In other words, validate the results!