3

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:

  1. 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.

  2. 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.

  3. 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).

asked Feb 26, 2019 at 13:22
0

1 Answer 1

2

Your code is flawed.

You set currentCat_id = "cat_id", a bit of text. You then only ever reference it on this line:

if currentCat_id == previousCat_id:

As CurrentCat_id never changes in your function, then the if currentCat_id == previousCat_id always fails hence you always return 1.

That is why it is always returning True...

answered Feb 26, 2019 at 14:16
3
  • 1
    Thanks, would you recommend adding a search cursor to update this with each row? Commented Feb 26, 2019 at 14:45
  • 2
    I would fix this issue first, why are you comparing a constant string with a changing previous ID, makes no sense. May be you should be setting CurrentCat_id correctly first? Commented Feb 26, 2019 at 15:11
  • Ah ok, I thought adding the second cursor would mean the cat_id is updated as the function was applied to each row. I would like to compare the current cat_ID with the previous, how would I ensure this is dynamic? Commented Feb 26, 2019 at 15:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.