gilest

NearestFeatures and Building index on IFeatureIndex

Discussion created by gilest on Nov 2, 2010
Latest reply on Nov 3, 2010 by gilest
I'm trying to find the 5 closest (point) features to an address returned from a geocoding service.  The code has to work with an ArcView license (version 9.3.1) so I cannot use GP tools like Near or PointDistance.  I'm using IFeatureIndex and IIndexQuery to get the nearest feature to my geocoded point but am looking for some advice on speeding things up. 

In the code below, for the 1st pass I just use the whole featureclass for the index on the FeatureIndex (idxFeat).  In subsequent attempts I filter out those already found using a queryfilter & a featurecursor on the FeatureIndex.  This works but the 1st indexing (against the whole FC) takes 3 seconds each additional pass takes 14 seconds. 

Anyone see any way I could speed this up or know of a better way?

Thanks,  Terry

               envQryLimit.CenterAt(ptAddrGeo);  //ptAddrGeo = geocoded pt in WGS84 coords
                    

                IQueryFilter QFilt = new QueryFilterClass();
                string strWhere = "";

                idxFeat.set_OutputSpatialReference(FLayer.FeatureClass.ShapeFieldName, srWGS84);
                idxFeat.FeatureClass = FLayer.FeatureClass;

                int fid;
                double dist;

                IList<Int32> fids = new List<Int32>();

                do
                {
                    if (fids.Count > 0)
                    {
                        //skip those already found
                        strWhere = FLayer.FeatureClass.OIDFieldName + " not in (";
                        for (int i = 0; i < fids.Count; i++)
                        {
                            strWhere += fids[i].ToString() + ",";
                        }
                        strWhere = strWhere.Remove(strWhere.Length-1) + ")";  //trailing comma
                        
                        QFilt.WhereClause = strWhere;
                        idxFeat.FeatureCursor = (IFeatureCursor) FLayer.FeatureClass.Search(QFilt,false);
                    }
                    System.Diagnostics.Debug.WriteLine(DateTime.Now.ToLongTimeString());
                    idxFeat.Index(null, envQryLimit);
                    System.Diagnostics.Debug.WriteLine(DateTime.Now.ToLongTimeString());

                    idxQry.NearestFeature(ptAddrGeo, out fid, out dist);
                    if (!(fids.Contains(fid)))
                    {
                        fids.Add(fid); 
                    }


                } while (fids.Count < 5); //5 is the # of points needed

Outcomes