Arcgis Engine(ae)接口詳解(5):IGeometry幾何基礎操作


            //點操作~~~~~~~~~~~~~~~~~~~~~~~~~

            //通過坐標生成點
            IPoint point = new PointClass();
            point.PutCoords(100, 200);

            //獲取點坐標
            double x = point.X;
            double y = point.Y;

            //線操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //通過點集生成線
            IPolyline polyline = new PolylineClass();
            //思路是通過點集接口IPointCollection添加線的點,創建線和面同樣可用此方法
            IPointCollection pointColl = polyline as IPointCollection;

            point = new PointClass();
            point.PutCoords(100, 200);
            pointColl.AddPoint(point);

            point = new PointClass();
            point.PutCoords(300, 100);
            pointColl.AddPoint(point);

            //通過點集生成線 完成~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //獲取線的起點和終點
            IPoint pointStart = polyline.FromPoint;
            IPoint pointEnd = polyline.ToPoint;

            //獲取線的長度
            double length = polyline.Length;

            //獲取線的矩形范圍(envelop),面的獲取方式也一樣
            //線和面都有envelope,不過點的envelop沒有意義
            IEnvelope envelope = polyline.Envelope;

            //獲取線是否是閉合的線
            bool isClosed = polyline.IsClosed;

            //把線的方向反轉,就是起點和終點,點的順序反轉
            polyline.ReverseOrientation();

            //獲取線的所有點,對於面同樣可用此方法
            for (int i = 0; i < pointColl.PointCount; i++)
            {
                IPoint point1 = pointColl.Point[i];
            }

            //面操作~~~~~~~~~~~~~~~~~~~~~~~~~~~

            IPolygon polygon = new PolygonClass();

            //通過點集生成線:與線一樣
            //注意:生成面時點集要求第一個點做坐標和最后一個點的坐標一樣,也可理解為同一個點添加了兩次,否則會出錯

            //獲取面的周長
            length = polygon.Length;

            IArea area = polygon as IArea;
            //獲取面的面積
            double area1 = area.Area;

            //獲取面的所有點,跟線獲取的方法一樣

            //幾何通用操作~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //點線面都可以as到IGeometry,所有幾何對象的類型都可以,因此所有幾何類型接口都繼承了IGeometry
            IGeometry geometry = polygon as IGeometry;

            //獲取幾何類型,可以區分出點,線,面等
            //如果有種情況獲取到的幾何對象的類型是IGeometry,那可以通過這個判斷是什么幾何類型,然后在as到對應的接口再做下一步操作
            esriGeometryType geometryType = geometry.GeometryType;

            //獲取是否空幾何對象
            //空幾何對象和null不同,例如點對象可是沒有點坐標,線對象沒有一個點等等
            //空幾何對象其中一種情況是,從feature獲取到的幾何對象,可能是空的,因為一條要素肯定有一個對應的幾何對象,可是編輯時又可以不錄入幾何對象(而只是錄入屬性字段值)
            bool isEmpty = geometry.IsEmpty;

            //矩形范圍(Envelope)操作~~~~~~~~~~~~~~~~~~~~~~~~~~~

            IEnvelope envelope2 = polygon.Envelope;

            //獲取矩形的坐標,矩形用最小點(左下角的點)和最大點(右上角的點)兩個點就足夠表示
            double xmin = envelope2.XMin;
            double ymin = envelope2.YMin;
            double xmax = envelope2.XMax;
            double ymax = envelope2.YMax;

            //獲取矩形的寬和高
            double height = envelope2.Height;
            double width = envelope2.Width;

            //通過坐標創建矩形
            envelope2 = new EnvelopeClass();
            envelope2.PutCoords(100, 200, 300, 400);

            //擴大和縮小
            //有兩種情況,根據參數3設置,false=按長度,true=按比例
            //下例是水平擴大10(米),垂直擴大20(米)
            envelope2.Expand(10, 20, false);
            //下例是水平設為原來的0.8倍(可以理解為縮小了20%),垂直設為原來的1.1倍(可以理解為放大了10%)
            envelope2.Expand(0.8, 1.1, true);

            //移動
            //把矩形中心點移到某個點(實際是整個矩形移動)
            envelope2.CenterAt(point);

            //矩形轉面
            //邏輯上矩形也是面,但在ae對象中IEnvelop和IPolygon不能互轉,下面是edm的轉換方法
            polygon = GeometryHelper.EnvelopeToPolygon(envelope);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM