I am attempting to classify a set of gps time-encoded point data into behaviors based on different attributes.
I have created an attribute that is 0 for home and 1 for away based on location, and now want to number the trips away from home (a set of points 01111111111110
would be one trip because it started and ended at home). I have added the attribute field that will have the trip numbers, but don't know how to calculate the field so it is based on the home/away field.
Here is an example of the GPS data (using "*" to indicate irrelevant information and simply indexing times as 1, 2, etc.), the "Home/Away" indicator described above, and the desired trip indicator, "Trip", which I need to compute:
Time Lat Lon Home/Away Trip
1 * * 0 0
2 * * 1 1
3 * * 1 1
....
12 * * 1 1
13 * * 0 0
14 * * 0 0
15 * * 1 2
16 * * 1 2
....
34 * * 1 2
35 * * 0 0
36 * * 0 0
37 * * 1 3
....
My data set is too large to manually go through and number each trip in the attribute table, so is there any way to calculate the field based on how the home/away attribute is ordered and each "clump" of away points is designated as a trip?
These are the bare bones of what Python code might look like (I'm not experienced with code).
Expression:
trip = Reclass(!home!)
Codeblock:
def Reclass(home):
if (home = 0):
return 0
elif (home = 1 and lastValue = 0):
return _(incremental numbering?)_
elif (home = 1 and lastValue = 1):
return lastValue
After using matt wilkie's recommended script I've made some changes so that my first trip is number 1, my second is 2, etc etc.
Here is the code modified from matt's:
import arcpy
rows = arcpy.UpdateCursor("test2")
trip = 0
for row in rows:
if row.home == 0:
prev = row.home
row.TRIP = trip
rows.updateRow(row)
elif row.home == 1 and prev == 0:
trip += 1
prev = row.home
row.TRIP = trip
rows.updateRow(row)
rows.next()
elif row.home == 1 and prev == 1:
prev = row.home
row.TRIP = trip
rows.updateRow(row)
rows.next()
row.TRIP = trip
rows.updateRow(row)
del row, rows
Then I just select for home = 0 and calculate my trip field back to 0. Neatly ordered trips.