# arcgis engine计算点到线的最短距离

IProximityOperator接口用于获取两个几何图形的距离，以及给定一个Point，求另一个几何图形上离离给定点最近的点。IProximityOperator接口的主要方法有：QueryNearesPoint,ReturnDistance, ReturnNearestPoint
ReturnDistance方法用于返回两个几何对象间的最短距离，QueryNearesPoint方法用于查询获取几何对象上离给定输入点的最近距离的点的引用，ReturnNearestPoint方法用于创建并返回几何对象上离给定输入点的最近距离的点

1. IMap pMap = axMapControl1.Map;
2.            ILayer pLayer = null;
3.            IPoint po=null;
4.            IPolyline pl=null;
5.            IFeatureLayer featurelayer=null;
6.            IFeatureClass featureclass = null;
7.            IGraphicsContainer gra;
8.            IElement ptele;
9.            IPointCollection lineptcol;
10.            gra = axMapControl1.Map as IGraphicsContainer;
11.            lineptcol = new PolylineClass();
12.            for (int i = 0; i < pMap.LayerCount; i++)
13.            {
14.                pLayer = pMap.get_Layer(i);
15.                 featurelayer = pLayer as IFeatureLayer;
16.                 featureclass = featurelayer.FeatureClass;
17.                IFeature feature = featureclass.GetFeature(0);
18.
19.                if (feature.Shape is IPoint)
20.                {
21.                    po = feature.Shape as IPoint;
22.                }
23.                else {
24.                     pl = feature.Shape as IPolyline;
25.                }
26.                //MessageBox.Show("qqqq");
27.            }
28.
29.            double dis = GetTwoGeometryDistance(po, pl);
30.            IPoint po2 = NearestPoint(po, pl);
31.            object a = Type.Missing;
32.            lineptcol.AddPoint(po, ref a, ref a);
33.            lineptcol.AddPoint(po2, ref a, ref a);
34.            IElement lineele = new LineElementClass();
35.            IPolyline pline = new PolylineClass();
36.            pline = lineptcol as IPolyline;
37.            lineele.Geometry = pline as IGeometry;
39.            axMapControl1.Refresh();
40.            MessageBox.Show(dis.ToString());

1. public double GetTwoGeometryDistance(IGeometry pGeometryA, IGeometry pGeometryB)
2.         {
3.             IProximityOperator pProOperator = pGeometryA as IProximityOperator;
4.             if (pGeometryA != null || pGeometryB != null)
5.             {
6.                 double distance = pProOperator.ReturnDistance(pGeometryB);
7.                 return distance;
8.             }
9.             else
10.             {
11.                 return 0;
12.             }
13.         }

1. //离给定的几何图形最近的点
2.         public IPoint NearestPoint(IPoint pInputPoint, IGeometry pGeometry)
3.         {
4.             try
5.             {
6.                 IProximityOperator pProximity = (IProximityOperator)pGeometry;
7.                 IPoint pNearestPoint = pProximity.ReturnNearestPoint(pInputPoint, esriSegmentExtension.esriNoExtension);
8.                 return pNearestPoint;
9.             }
10.             catch (Exception Err)
11.             {
12.                 MessageBox.Show(Err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
13.                 return null;
14.             }
15.         }

posted on 2018-05-08 01:36  gisery  阅读(2557)  评论(0编辑  收藏