Prasad85

Finding Nearest Faeture

Discussion created by Prasad85 on Aug 12, 2010
Hi ,

  I am using following code find out the nearest feature but i am always getting closestfeatureId as -1.

Please anyone help me how to resolve this issue
IFeatureSelection pFeatureSelection;
            ISpatialFilter pSpatialfilter;
            pSpatialfilter = new SpatialFilterClass();
            ITopologicalOperator pTopoOp;
            IGeometry pGeometry;
            IMxDocument mxDocument;
            IMap pMap;
            IPoint pMapPoint;
            IFeatureIndex2 pFeatureIndex = new FeatureIndexClass();
            IIndexQuery2 pIndexquery;
            IFeatureLayer pFLayer;
            IFeatureLayer pFLayer1;
            int closestFeature_id ;
            double distance=1000;
            IGeometry pGeometry1;
            IFeature pNearestFeature;
            IFeatureSelection pFeatSelection2;
            IEnumFeature pEnumFeature;
            IGeoDataset pGeodataset;
            ESRI.ArcGIS.esriSystem.ITrackCancel pTrackcance = new CancelTracker();
            ICursor   pFeatCur;
            try
            {
                mxDocument = m_application.Document as IMxDocument;
                pMap = mxDocument.FocusMap;
                pFLayer = pMap.get_Layer(0) as IFeatureLayer;
                pMapPoint = new ESRI.ArcGIS.Geometry.Point();
                pMapPoint = mxDocument.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
                pTopoOp = pMapPoint as ITopologicalOperator ;
                pGeometry = pTopoOp.Buffer(20);
                pFeatureSelection = pFLayer as IFeatureSelection;
                pSpatialfilter.GeometryField = pFLayer.FeatureClass.ShapeFieldName;
                pSpatialfilter.Geometry = pGeometry;
                pSpatialfilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                pFeatureSelection.SelectFeatures(pSpatialfilter, esriSelectionResultEnum.esriSelectionResultNew, false);

                pGeometry1 = pMapPoint  as IGeometry;
                pFLayer1 = pMap.get_Layer(1) as IFeatureLayer;
                pGeodataset=pFLayer1.FeatureClass as IGeoDataset  ;

                pFeatureIndex.FeatureClass = pFLayer1.FeatureClass;
                pFeatureIndex.set_OutputSpatialReference(pFLayer1.FeatureClass.ShapeFieldName, pGeodataset.SpatialReference);
                pFeatureIndex.Index(pTrackcance, pGeodataset.Extent);

                pFeatureSelection.SelectionSet.Search(null, false, out pFeatCur);
                IFeature Pfeature = pFeatCur.NextRow() as IFeature ;
                pIndexquery = pFeatureIndex as IIndexQuery2;
               

                if (pFeatureSelection.SelectionSet.Count != 0)
                {




                    pGeometry1 = Pfeature.Shape;

                    pIndexquery.NearestFeature(pGeometry1, out closestFeature_id, out  distance);
                    if (closestFeature_id != -1)
                    {
                        pNearestFeature = pFLayer1.FeatureClass.GetFeature(closestFeature_id);
                        if (pNearestFeature != null)
                        {
                            IQueryFilter pQueryFilter = new QueryFilterClass();
                            pQueryFilter.WhereClause = "FID=" + closestFeature_id;
                            pFeatSelection2 = pFLayer1 as IFeatureSelection;
                            pFeatSelection2.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultAdd, true);
                            mxDocument.ActiveView.Refresh();
                        }
                    }
                }


            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

            // TODO:  Add Proximity.O

Outcomes