I am attempting to assign 0/1 values to each row with a point feature class based upon a time dependent condition with ArcMap Model Builder 10.2.2.
The points are categorized based upon a series ID field and each given a unique time stamp. In order to standardize the data I would like to remove records that are less than 2 hours from the previous record.
I have done this using a field calculator which returns a true (1) or false (0) value, followed by a Select Tool.
The code block operates through a moving time window based upon a start time and and end time which gives three possible outcomes:
The first is whether the record is the first in the sequence within its category and therefore is labelled as true. The start time of the window is taken as this record's time stamp and the end time is as 2 hours from this point.
The second is the timestamp of the record is less than 2 hours from the start time and therefore is labelled as false. The time window remains the same.
The third is the time stamp is more than 2 hours from the start time and therefore is labelled as true. The start time of the window is now taken as the time stamp of the record and the end time is 2 hours from this point.
The function must continuously compare its category id against the previous record's to determine whether to create a new sequence, to do this I have used a Search Cursor.
I also extracted the first row of the data to create an inline variables in the model windowStart (the first record's time stamp) and windowEnd (the first record + 2), these are then called in when defining the function's variables.
I have also add a Counter field which counts and numbers the number of rows which have been order by category (cat_id) and date. Finally I have a sequence field which assigns the record a value based upon its order within its relative category.
My code block is:
import datetime
import arcpy
from datetime import datetime, timedelta
def FindTime(counter, date, cat_id, interval, table, windowStart, windowEnd):
if counter == 1: # if record is first in list
previousCat = "cat_id"
return 1 # condition is true
else:
currentDate = date
currentCat_id = "cat_id"
cursor = arcpy.SearchCursor(r'%FeatureClass%')
row_num = 0
for row in cursor:
n = row.getValue("cat_id")
if row_num > 0:
oldN = n
row_num = row_num - 1
previousCat_id = (row.getValue("cat_id"))
else:
previousCat_id = "cat_id"
if currentCat_id == previousCat_id:
if (currentDate < windowEnd) and (currentDate > windowStart):
# if inbetween times
return 0 # condition is false
else:
windowStart = currentDate # set up new window
windowDuration = r"%Interval%"
windowEnd = windowStart + windowDuration
return 1
This code runs without errors, however returns all values as true (1) when there are numerous that do not meet the condition.
I have also attempted to use VB as a substitute using the code:
dim condition
If counter = 1 Then
windowStart =“Featureclass”.Cells(counter, colDict(“date”)).value
windowEnd = DateAdd("h", 2, windowStart) 'End of our 2hr window
previousCat_id =“Featureclass”.Cells(counter, colDict(“cat_id”)).value
[time_frame_2hr_VB] = 1
Else
currentDate =“Featureclass”.Cells(counter, colDict(“date”)).value
currentCat_id =“Featureclass”.Cells(counter, colDict(“cat_id”)).value
'Are we still the same cat_id
If currentCat_id = previousCat_id Then
Delta = DateDiff("s", windowEnd, currentDate)
If currentDate < windowEnd And currentDate > windowStart And Delta < 0 Then
[time_frame_2hr_VB] = 0
Else
windowStart =“Featureclass”.Cells(counter, colDict(“date”)).value
windowEnd = DateAdd("h", 2, windowStart) 'End of our 2hr window
[time_frame_2hr_VB] = 1
End If
Else 'different cat_id so reset the window and cat_id
windowStart =“Featureclass”.Cells(counter, colDict(“date”)).value
windowEnd = DateAdd("h", 2, windowStart) 'End of our 2hr window
previousCat_id =“Featureclass”.Cells(counter, colDict(“cat_id”)).value
[time_frame_2hr_VB] = 1
End If
End If
Next Counter
This also returns all positive values within time_frame_2hr_VB field (the Boolean true/false).