找个输入IPoint在某个FeatureClass上距离最近的要素

 

 

 1 /// <summary>
 2         /// 得到输入点在输入图层上的最近点
 3         /// </summary>
 4         /// <param name="randomPoints">输入的点,注意小数位数不能太长,具体支持多长没有测试,我用小数点2位可以成功,但是七八位肯定不行</param>
 5         /// <param name="feaPoints">输出在featureclass上距离最近的要素,这里是点类型输出的是点</param>
 6         /// <param name="length">输入点到featureclass上最近点的距离</param>
 7         /// <returns></returns>
 8         public bool NearestPoint(IPoint[] randomPoints, out IPoint[] feaPoints, out double length)
 9         {
10             feaPoints = new IPoint[randomPoints.Length];
11             length = 0;
12             try
13             {
14                 fd = (LinkReady.ReadPipeLineWorkspace() as IFeatureWorkspace).OpenFeatureDataset(LY.UG.Common.DictionaryConfig.DatasetName["道路网络数据集名称"]);//("SDE.地下管线");
15                 nc = fd as INetworkCollection;
16                 IFeatureClass feaRoadJunction = (fd as IFeatureClassContainer).get_ClassByName(LY.UG.Common.DictionaryConfig.DatasetName["道路网络点图层"]);
17 
18                 int[] feaID = new int[randomPoints.Length];
19                 double[] feaDis = new double[randomPoints.Length];
20                 IFeatureIndex pFeatureIndex = new FeatureIndex();
21                 IIndexQuery pIndexQuery;
22                 IGeoDataset geodataset = (IGeoDataset)feaRoadJunction;
23 
24                 ITrackCancel trackCancel = new TrackCancel();
25                 pFeatureIndex.FeatureClass = feaRoadJunction;//.setFeatureClassByRef(pFeatureClass1);
26 
27                 pFeatureIndex.Index(trackCancel, geodataset.Extent);
28                 pIndexQuery = pFeatureIndex as IIndexQuery;
29                 for (int i = 0; i < randomPoints.Length; i++)
30                 {
31                     pIndexQuery.NearestFeature(randomPoints.GetValue(i) as IPoint, out feaID[i], out feaDis[i]);
32                     feaPoints[i] = new PointClass();
33                     feaPoints[i] = feaRoadJunction.GetFeature(feaID[i]).ShapeCopy as IPoint;
34                     length = length + feaDis[i];
35                 }
36 
37                 return true;
38             }
39             catch (Exception ee)
40             {
41                 return false;
42             }
43         }

 

 

posted @ 2014-04-03 14:02  寻梦放歌  阅读(466)  评论(0编辑  收藏  举报