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;
-
}
-
}
计算出来最近的点,然后和初始的那个点连成一个线,也就做出了直线的中垂线