arcgis engine calculate shortest distance from point to line

The IProximityOperator interface is used to obtain the distance between two geometries, and given a Point, find the closest point on the other geometry to the given point. The main methods of the IProximityOperator interface are: QueryNearesPoint, ReturnDistance, ReturnNearestPoint The
ReturnDistance method is used to return the shortest distance between two geometric objects, the QueryNearesPoint method is used to query and obtain the reference of the point with the closest distance to a given input point on the geometric object, and the ReturnNearestPoint method Creates and returns the closest point on a geometry object to a given input point

  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;
  38.            gra.AddElement(lineele, 0);
  39.            axMapControl1.Refresh();
  40.            MessageBox.Show(dis.ToString());

Calculate distance between geometries

  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.         }

the closest point to the given geometry

  1. //The closest point to the given geometry
  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.         }

计算出来最近的点,然后和初始的那个点连成一个线,也就做出了直线的中垂线

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325696375&siteId=291194637