遍历axMapControl选择要素

12 篇文章 0 订阅
        private void button1_Click(object sender, EventArgs e)
        {
            if (axMapControl_UseLayer.LayerCount < 1)
            {
                MessageBox.Show("请选择裁剪图层");
                return;
            }
            IEnvelope envelop1 = map.FullExtent;


            IFeatureCursor featureCursor = GetSelectedFeature((axMapControl_UseLayer.get_Layer(0) as IFeatureLayer));
            if (featureCursor == null)
            {
                featureCursor = (axMapControl_UseLayer.get_Layer(0) as IFeatureLayer).FeatureClass.Search(null, false);
            }
            IGeometry area = null;//区域
            List<IPolygon> polygons = new List<IPolygon>();
            if (featureCursor != null)
            {
                IFeature feature = featureCursor.NextFeature();
                while (feature != null)
                {
                    IPolygon p = feature.Shape as IPolygon;
                    p.Project(axMapControl_UseLayer.SpatialReference);
                    polygons.Add(p);
                    feature = featureCursor.NextFeature();
                }
                area = UnionPolygon(polygons.ToArray());
                map.Map.ClipGeometry = area;


                //axmapcontrol中显示合并图层的大小
                double xmin = map.Map.ClipGeometry.Envelope.XMin - 0.36;
                double ymin = map.Map.ClipGeometry.Envelope.YMin - 0.36;
                double xmax = map.Map.ClipGeometry.Envelope.XMax + 0.36;
                double ymax = map.Map.ClipGeometry.Envelope.YMax + 0.36;
                envelop1.PutCoords(xmin, ymin, xmax, ymax);
            }


            map.Map.ClipGeometry = area;
            ((IMapClipOptions)map.Map).ClipType = esriMapClipType.esriMapClipShape;
            ISymbolBorder border = new SymbolBorderClass();
            ILineSymbol lineSymbol = border.LineSymbol;
            lineSymbol.Color = GetIColor(255, 0, 0);
            border.LineSymbol = lineSymbol;


            ((IMapClipOptions)map.Map).ClipBorder = border;
            map.Extent = envelop1;
            this.Close();

        }

        private IFeatureCursor GetSelectedFeature(IFeatureLayer featureLayer)//获取当前地图选中的要素
        {
            try
            {
                if (featureLayer == null) return null;


                IFeatureSelection pFeatSel = (IFeatureSelection)featureLayer;
                ISelectionSet pSelectionSet = pFeatSel.SelectionSet;


                if (pSelectionSet.Count == 0)
                {
                    return null;
                }
                ICursor pCursor;
                pSelectionSet.Search(null, false, out pCursor);
                return (IFeatureCursor)pCursor;
            }
            catch (Exception ex)
            {
                MessageBox.Show("选择要素失败!", "警告");
                return null;
            }

        }

         private IPolygon UnionPolygon(IPolygon[] polygons)//合并面
        {
            if (polygons.Length == 0)
            {
                return null;
            }
            IPolygon result = polygons[0];
            ITopologicalOperator2 topo = result as ITopologicalOperator2;
            for (int i = 1; i < polygons.Length; i++)
            {
                topo.IsKnownSimple_2 = false;
                topo.Simplify();
                result.SnapToSpatialReference();


                ITopologicalOperator2 t2 = polygons[i] as ITopologicalOperator2;
                t2.IsKnownSimple_2 = false;
                t2.Simplify();
                polygons[i].SnapToSpatialReference();


                result = topo.Union(polygons[i]) as IPolygon;
                topo = result as ITopologicalOperator2;
            }
            return result;
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值