Selecting Consecutive Rows in a Feature Class in Python

1757
3
01-25-2012 05:23 AM
TommyBurk
New Contributor II
I am trying to find a piece of python code that will select consecutive rows in a feature class.

I want to select all features in a feature class where a field is less than 6 (Hours < 6). I can do this part.

I then want to select the next feature below each of those that are less than 6, no matter what it's field value is, so I can get a start and end point. I have tried using the search cursor, but I'm not real familiar with how it works. Is there a bit of code that would accomplish what I'm trying to do? Thanks for any help!

Tommy Burk
Tags (2)
0 Kudos
3 Replies
AndrewChapkowski
Esri Regular Contributor
Hey Tommy,

In your example of Hour < 6 and you want the next value, would you say your have unique values, or do you want a list of unique values, then create ranges off of that list? 

If that is the case, try using the Fequency tool set and set the field to the Hour field or whatever field you want the unique list of values to.

You can then use a search cursor on that table to get a complete list of values and easily select the next lowest value by sorting your list.
0 Kudos
curtvprice
MVP Esteemed Contributor
You can then use a search cursor on that table to get a complete list of values and easily select the next lowest value by sorting your list.


Just a thought -- you don't need to use the Sort tool; just specify the sorting when you request the search cursor.
0 Kudos
TommyBurk
New Contributor II
Hey Andrew,

Thanks for your response. I think I did a poor job of explaining what I am trying to do. I have a feature class that is sorted by a date/time field. The earliest date is first. I have then run a datediff tool to determine the difference in time (in hours) and populated a field (hours) to show the difference in hours between features. I want to select the features that have less than 6 hours between them, which I can do. This allows me to get a starting point. I then want to include in that selection the next feature below that, so the next feature in the table, which will allow me to have an end point. I am having trouble figuring out how to create query for this second group of features I would like to select because the hours field is difference between the next row and not the previous row.

Name             Date/Time          Hours

Feature 1       Jan 23/9AM            0
Feature 2       Jan 23/12PM           3
Feature 3       Jan 24/6AM           18
Feature 4       Jan 24/12PM          6
Feature 5       Jan 25/6PM           30
Feature 6       Jan 27/6PM           48

So I can select feature 1, 2, and 4, but I also want to select feature 3, simply based on the fact that it's following feature 2, which I already selected. My assumption is that feature 2 and feature 3 are the starting and ending point of an event. I also want to select feature 5 (the end point), because I have feature 4 (the start point) selected. Feature 6 will not be in the selection because it is not a starting point (hour < 6) and does not follow a point where hour < 6.  I am hoping that this will allow me to create a feature class that will include the starting and ending points of certain events. I apologize. I hope this makes sense. I'm trying to learn python on the fly. Thanks again for any help you can provide.

Tommy Burk
0 Kudos