Select to view content in your preferred language

NearestFeatures and Building index on IFeatureIndex

2151
1
11-02-2010 06:42 AM
TerryGiles
Frequent Contributor
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.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
0 Kudos
1 Reply
TerryGiles
Frequent Contributor
When in doubt as a database person.... I set the SubFields on the QueryFilter used to generate the FeatureCursor passed to the FeatureIndex to just the FID and Shape.  14 seconds is now 2. 

Hope this helps someone else!

 if (fids.Count > 0)
                    {
                        //skip those already found
                        strWhere = FLayer.FeatureClass.OIDFieldName + " not in (";
                        for (int i = 0; i < fids.Count; i++)
                        {
                            strWhere += fids.ToString() + ",";
                        }
                        strWhere = strWhere.Remove(strWhere.Length-1) + ")";  //trailing comma
                        
                        QFilt.SubFields = FLayer.FeatureClass.OIDFieldName + "," +  FLayer.FeatureClass.ShapeFieldName;
                        QFilt.WhereClause = strWhere;
                        idxFeat.FeatureCursor = (IFeatureCursor) FLayer.FeatureClass.Search(QFilt,false);
                    }


0 Kudos