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;
-
}
-
}
計算出來最近的點,然后和初始的那個點連成一個線,也就做出了直線的中垂線