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.
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?
-
1You need to reset the fcur2 feature cursor for each fcur1 iterationBarbarossa– Barbarossa2017年09月12日 19:58:56 +00:00Commented Sep 12, 2017 at 19:58
-
How to reset the cursor? I can't see this method in fcur2 objectAlex– Alex2017年09月12日 20:04:42 +00:00Commented Sep 12, 2017 at 20:04
-
1My mistake. I was thinking there was a reset method. Either way, you should reset the cursor by re-executing the query.Barbarossa– Barbarossa2017年09月12日 20:08:43 +00:00Commented Sep 12, 2017 at 20:08
1 Answer 1
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.
-
It works!!! Barbarossa, many thanks! I didn't have a big hope that someone would introspect my long questionAlex– Alex2017年09月12日 20:22:52 +00:00Commented Sep 12, 2017 at 20:22