arcgis engine计算点到线的最短距离
IProximityOperator接口用于获取两个几何图形的距离,以及给定一个Point,求另一个几何图形上离离给定点最近的点。IProximityOperator接口的主要方法有:QueryNearesPoint,ReturnDistance, ReturnNearestPoint
ReturnDistance方法用于返回两个几何对象间的最短距离,QueryNearesPoint方法用于查询获取几何对象上离给定输入点的最近距离的点的引用,ReturnNearestPoint方法用于创建并返回几何对象上离给定输入点的最近距离的点
- IMap pMap = axMapControl1.Map;
- ILayer pLayer = null;
- IPoint po=null;
- IPolyline pl=null;
- IFeatureLayer featurelayer=null;
- IFeatureClass featureclass = null;
- IGraphicsContainer gra;
- IElement ptele;
- IPointCollection lineptcol;
- gra = axMapControl1.Map as IGraphicsContainer;
- lineptcol = new PolylineClass();
- for (int i = 0; i < pMap.LayerCount; i++)
- {
- pLayer = pMap.get_Layer(i);
- featurelayer = pLayer as IFeatureLayer;
- featureclass = featurelayer.FeatureClass;
- IFeature feature = featureclass.GetFeature(0);
- if (feature.Shape is IPoint)
- {
- po = feature.Shape as IPoint;
- }
- else {
- pl = feature.Shape as IPolyline;
- }
- //MessageBox.Show("qqqq");
- }
- double dis = GetTwoGeometryDistance(po, pl);
- IPoint po2 = NearestPoint(po, pl);
- object a = Type.Missing;
- lineptcol.AddPoint(po, ref a, ref a);
- lineptcol.AddPoint(po2, ref a, ref a);
- IElement lineele = new LineElementClass();
- IPolyline pline = new PolylineClass();
- pline = lineptcol as IPolyline;
- lineele.Geometry = pline as IGeometry;
- gra.AddElement(lineele, 0);
- axMapControl1.Refresh();
- MessageBox.Show(dis.ToString());
计算几何图形之间的距离
- public double GetTwoGeometryDistance(IGeometry pGeometryA, IGeometry pGeometryB)
- {
- IProximityOperator pProOperator = pGeometryA as IProximityOperator;
- if (pGeometryA != null || pGeometryB != null)
- {
- double distance = pProOperator.ReturnDistance(pGeometryB);
- return distance;
- }
- else
- {
- return 0;
- }
- }
离给定的几何图形最近的点
- //离给定的几何图形最近的点
- public IPoint NearestPoint(IPoint pInputPoint, IGeometry pGeometry)
- {
- try
- {
- IProximityOperator pProximity = (IProximityOperator)pGeometry;
- IPoint pNearestPoint = pProximity.ReturnNearestPoint(pInputPoint, esriSegmentExtension.esriNoExtension);
- return pNearestPoint;
- }
- catch (Exception Err)
- {
- MessageBox.Show(Err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return null;
- }
- }
计算出来最近的点,然后和初始的那个点连成一个线,也就做出了直线的中垂线
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步