Select to view content in your preferred language

Get Previous Feature Using FeatureCursor

1020
5
Jump to solution
11-06-2013 05:58 AM
Corbinde_Bruin
Frequent Contributor
Hello all,

What is the best practice for stepping backwards in a FeatureCursor. I use the NextFeature() method to loop through a FeatureCursor and make edits, but now I need tot be able to scroll back and forth between features in a FeatureCursor.

If there is another way, I do not need to use the FeatureCursor. I just don't know of another way.

I looked into using an enumerator, but that looks like it has the same exact problem. I see a Next() method and a Reset() method, but no Previous() method.

I feel I am missing something basic here. Right now I am requerying the database every time I want to access a 'previous' feature. I'd love to see some ArcObjects best practices here. I've searched the documentation and forums, but I was unable to find anything related.

Thank you,
Corbin de Bruin
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
You could put the features into a collection (an array, dictionary, hashtable, etc). After running the feature cursor once to build the collection, you can access the features as you need.

One of my projects involved doing a series of calculations on the attributes of a group of features. Many of the calculations used values that were the result of previous calculations. The person who originally wrote the code used a separate cursor for each individual calculation, which took forever. I was able to rewrite the code where all of the attributes for the features where written into an array using a featurecursor. All the calculations where then done by looping through this array, which speed things up immensely.

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor
You could put the features into a collection (an array, dictionary, hashtable, etc). After running the feature cursor once to build the collection, you can access the features as you need.

One of my projects involved doing a series of calculations on the attributes of a group of features. Many of the calculations used values that were the result of previous calculations. The person who originally wrote the code used a separate cursor for each individual calculation, which took forever. I was able to rewrite the code where all of the attributes for the features where written into an array using a featurecursor. All the calculations where then done by looping through this array, which speed things up immensely.
0 Kudos
Corbinde_Bruin
Frequent Contributor
Writing to an array is another method I had toyed around with, and it's what I have moved to using today.

Do you use Array.FindIndex at all? This is what I would up using to loop through my records and find the previous record.

gRow = Array.FindLastIndex(rowArray, AddressOf previousPhotoRowPredicate)


 Private Function previousPhotoRowPredicate(ByVal pRow As Row) As Boolean

        If pRow.OID < gCurrentRow.OID Then
            Return True
        Else
            Return False
        End If

    End Function
0 Kudos
KenBuja
MVP Esteemed Contributor
I didn't need to look for particular feature, so I never played around with Array.FindIndex
0 Kudos
NeilClemmons
Honored Contributor
Unless you're using an ORDER BY clause in the query or doing something else to sort the results, the features in a feature cursor will not be in any reliable order.  For example, if the ObjectId of the current feature is 2 you can't assume that the ObjectId of the previous feature was 1.  This may or may not apply to your specific scenario but I just wanted to point it out in case it does matter.
0 Kudos
Corbinde_Bruin
Frequent Contributor
Neil,

That's good to know. I've added a QueryFilterDefinition.PostfixClause to fix this issue.

Thank you,
Corbin de Bruin
0 Kudos