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.
Answer
For this you can use UpdateCursor, which opens the feature class or table and steps through each record (row) incrementally.
The script below works on this test data
+-----------------------+
| Time| Home_Away|Trip |
+-----|----------|------+
| 1 | 0 | |
| 2 | 1 | |
| 4 | 1 | |
| 5 | 0 | |
| 6 | 0 | |
| 7 | 1 | |
| 9 | 1 | |
| 12 | 1 | |
| 13 | 0 | |
+-----------------------+
.
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
The trailing end of trip block is actually run for the beginning of a trip also, but since the trip counter is correct the double calc on the begin-trip-row doesn't matter . Uncomment the print statement in that block to see what I mean.
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!
No comments:
Post a Comment