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);

下面是其中用到的方法的代码code

public static IPolygon EnvelopeToPolygon(IEnvelope envelope)
        {
            IPointCollection pointColl = new PolygonClass();

            IPoint point = new PointClass();
            point.PutCoords(envelope.XMin, envelope.YMin);
            pointColl.AddPoint(point);

            point = new PointClass();
            point.PutCoords(envelope.XMax, envelope.YMin);
            pointColl.AddPoint(point);

            point = new PointClass();
            point.PutCoords(envelope.XMax, envelope.YMax);
            pointColl.AddPoint(point);

            point = new PointClass();
            point.PutCoords(envelope.XMin, envelope.YMax);
            pointColl.AddPoint(point);

            point = new PointClass();
            point.PutCoords(envelope.XMin, envelope.YMin);
            pointColl.AddPoint(point);

            return pointColl as IPolygon;
        }
相关文章
相关标签/搜索