3

I need to do some operation with every combinations of features in two FeatureClasses. The "for" iterating operator is very very slow-working, so I decided to try "while" operator and using FeatureCursor. So nested iteration I have like below:

    IFeatureCursor fcur1 = fc1.Search(null, false);
    IFeatureCursor fcur2 = fc2.Search(null, false);

    IFeature f1 = fcur1.NextFeature();
    IFeature f2 = fcur2.NextFeature();

        while (f1 != null)
        {
            //external cycle code
            MessageBox.Show("external cycle FID " + f1.Value[0].ToString());

            while (f2 != null)
            {
                //internal cycle code
                MessageBox.Show("internal cycle FID " + f1.Value[0].ToString());
                f2 = fcur2.NextFeature();
            }
            f1 = fcur1.NextFeature();
        }

For example, there are 3 features in each FeatureClass fc1 and fc2.

enter image description here

I use a MessageBoxes to show FID of feature f1 to understand what happens in external and internal iterations.

The expected order of MessageBoxes is:

external cycle FID 0
 internal cycle FID 0
 internal cycle FID 0
 internal cycle FID 0
external cycle FID 1
 internal cycle FID 1
 internal cycle FID 1
 internal cycle FID 1
external cycle FID 2
 internal cycle FID 2
 internal cycle FID 2
 internal cycle FID 2

But result of my code is:

external cycle FID 0
 internal cycle FID 0
 internal cycle FID 0
 internal cycle FID 0
external cycle FID 1
external cycle FID 2

So, external cycle operates with every feature f1, but internal cycle gets the first feature f1 only. Why another features f1 doesn't go to internal cycle?

3
  • 1
    You need to reset the fcur2 feature cursor for each fcur1 iteration
    – Barbarossa
    Commented Sep 12, 2017 at 19:58
  • How to reset the cursor? I can't see this method in fcur2 object
    – Alex
    Commented Sep 12, 2017 at 20:04
  • 1
    My mistake. I was thinking there was a reset method. Either way, you should reset the cursor by re-executing the query.
    – Barbarossa
    Commented Sep 12, 2017 at 20:08

1 Answer 1

2

You need to reset the the feature cursor after going through each iteration. See my edits:

IFeatureCursor fcur1 = fc1.Search(null, false);
IFeatureCursor fcur2;

IFeature f1; 
IFeature f2;

    while ((f1 = fcur1.NextFeature()) != null)
    {
        //external cycle code
        MessageBox.Show("external cycle FID " + f1.Value[0].ToString());

        fcur2 = fc2.Search(null, false);
        while ((f2 = fcur2.NextFeature()) != null)
        {
            //internal cycle code
            MessageBox.Show("internal cycle FID " + f1.Value[0].ToString());
        }
    }

You may also want to release the second cursor after iterating through its features.

1
  • It works!!! Barbarossa, many thanks! I didn't have a big hope that someone would introspect my long question
    – Alex
    Commented Sep 12, 2017 at 20:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.