how to find  any two features whether Cover in One featureclass?

630
3
10-22-2013 11:17 PM
yanli
by
New Contributor II
i want to find  any two features whether Cover in One featureclass. the code as following,
it works ok,but if  the featureclass have  100000 features ,it work so slowly.
how  to make it fast by modify the code? thank  you

      public void CheckInOneLayer(IFeatureClass IChecklayer)
        {
          
            string Layername = IChecklayer.AliasName;
            int nn = 0;
            int count=IChecklayer.FeatureCount(null);
            IFeatureCursor IfeaCur = IChecklayer.Search(null, false);
            IFeature Ifea;
            while ((Ifea = IfeaCur.NextFeature()) != null)
            {
                nn++;
            

                GetFeature(Ifea, IChecklayer);
             
                
            }

            MessageBox.Show("finish!");

        }

        public void GetFeature(IFeature Mfeature, IFeatureClass IChecklayer)
        {
            try
            {
                string layername = IChecklayer.AliasName;
                IRelationalOperator pRelOperator;
                pRelOperator = Mfeature.Shape as IRelationalOperator;

                IFeatureCursor pFeatureCursor = IChecklayer.Search(null, false);
                IFeature pComFeature;
                while ((pComFeature = pFeatureCursor.NextFeature()) != null)
                {
                    if (pRelOperator.Overlaps(pComFeature.Shape))
                    {
                         //here get the feature!!!!!

                        //addToGrid(Mfeature, pComFeature, layername);

                    }

                }


                if (pFeatureCursor != null)
                {
                    Marshal.ReleaseComObject(pFeatureCursor);
                }
            }
            catch (Exception e)
            {

                MessageBox.Show("Error:"+e.Message.ToString());
            }

        }
0 Kudos
3 Replies
AlexanderGray
Occasional Contributor III
There are a few optimizations I can think of.
First off, in your GetFeature method, you create a new cursor every time.  Cursor take a lot of processing.  Re-using the cursor might give you a performance boost.  That would also avoid having to release it every time.
I also notice you keep all the fields.  You can stream line your cursors by defining the subfield of a query filter to be only the objectid and the shape field, the where clause can be empty. 
In GetFeatures, you search all the features and do a relational operation, you could do a spatialfilter query instead and get only features that overlap returned.
If it is possible memory-wise, you could build a dictionary or two dimensional array with the objectId and shape for the IcheckLayer by cursoring through them once to build the dictionary, then you could loop through the dictionary instead.  
Finally, you can look at the Overlay toolbox in the analysis tools.  Intersect, for example, will keep all the overlaping sections of both featureclasses with the attributes from both.  You could get the output features and get the objectid of both original featrures.
0 Kudos
yanli
by
New Contributor II
There are a few optimizations I can think of.
First off, in your GetFeature method, you create a new cursor every time.  Cursor take a lot of processing.  Re-using the cursor might give you a performance boost.  That would also avoid having to release it every time.
I also notice you keep all the fields.  You can stream line your cursors by defining the subfield of a query filter to be only the objectid and the shape field, the where clause can be empty. 
In GetFeatures, you search all the features and do a relational operation, you could do a spatialfilter query instead and get only features that overlap returned.
If it is possible memory-wise, you could build a dictionary or two dimensional array with the objectId and shape for the IcheckLayer by cursoring through them once to build the dictionary, then you could loop through the dictionary instead.  
Finally, you can look at the Overlay toolbox in the analysis tools.  Intersect, for example, will keep all the overlaping sections of both featureclasses with the attributes from both.  You could get the output features and get the objectid of both original featrures.


thank you  !! I also have some quesions.
1.how  to  Re-using the cursor ? can you give me a example.
2. spatialfilter query  is more Efficiency�?
0 Kudos
AlexanderGray
Occasional Contributor III
1. IFeatureCursor pFeatureCursor = IChecklayer.Search(null, false);
pFeatureCursor = IChecklayer.Search(null, false);

2. Spatial query will return fewer features than all of them and these features will be known to overlap so no need to test them all individually.  As far as perfomance, putting metrics in and trying it both ways is the only way to know for sure which is best.  all my suggestiongs are the obvious things I would try to get a faster process.  Bbefore you do anything else, I suggest you get the time at the start and end and report the difference, that way you can evaluate any change against a baseline.
0 Kudos