C#+AE(ArcEngine)二次开发:通过点击选择面状要素并计算其面积

将代码写在Mapcontrol的OnMouseDown事件中,具体代码如下:

        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            //实例化一个点
            IPoint pPoint = new PointClass();
            //以该点作拓扑算子
            ITopologicalOperator pTopologicalOperator=pPoint as ITopologicalOperator;
            //将点击的位置坐标赋予pPoint
            pPoint.PutCoords(e.mapX, e.mapY);
            //以缓冲半径为0进行缓冲  得到一个点
            IGeometry pGeometry = pTopologicalOperator.Buffer(0);
            //以该点进行要素选择(只能选中面状要素,点和线无法选中)
            axMapControl1.Map.SelectByShape(pGeometry, null, false);
            //刷新视图
            axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);


            //IMap.FeatureSelection默认只返回 shape 信息,可以通过 IEnumFeatureSetup::AllFields 方法来设置允许返回属性


            // 获取选择集
            ISelection pSelection = axMapControl1.Map.FeatureSelection;
            // 打开属性标签
            IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;
            pEnumFeatureSetup.AllFields = true;
            // 获取要素
            IEnumFeature pEnumFeature = pSelection as IEnumFeature;
            IFeature pFeature = pEnumFeature.Next();
            while (pFeature != null)
            {
                double area = 0;
                double mu = 0;
                if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                {
                    //计算面积
                    IArea pArea = pFeature.Shape as IArea;
                    area = area + pArea.Area;//得到的面积单位是平方米
                    mu = area * 0.0015;//转换为亩
                }
                textBox1.Text = Convert.ToString(Math.Round(area,6));
                textBox2.Text = Convert.ToString(Math.Round(mu, 6));
                break;
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值