遍历axMapControl选择要素

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

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页