IPoint point = new PointClass(); point.PutCoords(100, 200); //ITopologicalOperator接口用於幾何對象的幾何操做 ITopologicalOperator topo = point as ITopologicalOperator; //作一個幾何對象的緩衝區(結果也是個幾何對象),參數1是緩衝半徑 //點線面的緩衝區都是面,因此能夠直接as到IPolygon IPolygon polygon = topo.Buffer(100) as IPolygon; ITopologicalOperator2 topo2 = point as ITopologicalOperator2; //錯誤幾何對象的修復,若是當幾何對象有幾何錯誤,例如面有自相交,能夠用此修復 topo2.IsKnownSimple_2 = false; topo2.Simplify(); topo = polygon as ITopologicalOperator; //獲取面的邊,面的邊是線 IPolyline polyline = topo.Boundary as IPolyline; IPolygon polygon2 = new PolygonClass(); topo = polygon as ITopologicalOperator; //求兩個幾何對象的重疊部分 //兩個幾何對象的重疊部分,能夠有不少種幾何類型組合,例如面與面重疊是面,線與線重疊是線或者點,點與點重疊是點,點與面重疊是點,線與面重疊是線等等 //參數2是返回結果是多少維的意思,根據經驗若是返回結果是點就是0維(esriGeometry0Dimension),線就是1維,面就是2維 //官方文檔還有詳細說明,使用者要結合文檔和實際使用狀況相互對照來學習 IGeometry geometry3 = topo.Intersect(polygon2, esriGeometryDimension.esriGeometry2Dimension); //兩個幾何對象的幾何操做還有: //Union 求兩個幾何對象合併後的,也就是求並集 //Clip 裁剪 //Cut 用線把面一份爲二 //Difference 擦除 //IRelationalOperator用於判斷兩個幾何對象的空間關係 //IRelationalOperator的每種空間關係在官方文檔有具體截圖 IRelationalOperator relaOper = polygon as IRelationalOperator; //求兩個幾何對象是否有重疊部分,注意:Overlaps判斷的兩個幾何對象的幾何類型必須相同 bool result = relaOper.Overlaps(polygon2); //其餘空間關係判斷有: //Contains-徹底包含 //Crosses-穿過? //Disjoint-徹底不相交 //Equals-徹底重疊(就是兩個幾何對象徹底同樣,經常使用!!!!!) //Touches-邊沿重疊? //Within-徹底包含2 IPoint point2 = new PointClass(); point.PutCoords(200, 300); //計算兩點距離 double distance = GeometryHelper.TwoPointDistance(point, point2);
調用的封裝函數函數
/// <summary> /// 計算兩點距離 /// </summary> /// <param name="point1"></param> /// <param name="point2"></param> /// <returns></returns> public static double TwoPointDistance(IPoint point1, IPoint point2) { if (point1 == null || point2 == null) return 0; return TwoPointDistance(point1.X, point1.Y, point2.X, point2.Y); } /// <summary> /// 計算兩點距離 /// </summary> /// <param name="x1"></param> /// <param name="y1"></param> /// <param name="x2"></param> /// <param name="y2"></param> /// <returns></returns> public static double TwoPointDistance(double x1, double y1, double x2, double y2) { return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)); }