ArcGIS Engine遍历图层,获取指定的图层

方法一:根据mxd文件路径进行遍历

同理可以根据其他条件获取指定的图层,此处图层名称只是一个例子。

        //mxdPath 文件路径"C:\\test.mxd",layername图层名称
        public IFeatureLayer LoadMxd(string mxdPath, string layername)
        {
            if (!File.Exists(mxdPath))
            {
                MessageBox.Show("不存在该文件!");
                return null;
            }
            IFeatureLayer pFlayer;
            IMapDocument pMapdocument = new MapDocumentClass();
            pMapdocument.Open(mxdPath, "");
            IMap Pmap = pMapdocument.ActiveView.FocusMap;
            Pmap.MapUnits = ESRI.ArcGIS.esriSystem.esriUnits.esriMeters;
            if (Pmap.LayerCount != 0)
            {
                UID puid = new UIDClass();
                puid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";  //只读取IFeaturelayer 
                IEnumLayer pEnumLayer = Pmap.get_Layers(puid, true);
                pEnumLayer.Reset();
                pFlayer = (IFeatureLayer)pEnumLayer.Next();
                while (pFlayer != null)
                {
                    if (pFlayer.Name == layername)
                    { return pFlayer; }
                    pFlayer = (IFeatureLayer)pEnumLayer.Next();
                }
            }
            return null;
        }

方法二:根据当前已经打开的mxd地图窗口进行遍历

遍历图层主要使用 ICompositeLayer 类,可以理解为图层集合。主要思想就是获取当前地图的最上层图层列表,循环判断是图层集合还是图层,若是图层集合则继续向下遍历,若是图层则判断图层名称,直到找到指定的图层。

同理可以根据其他条件获取指定的图层,此处名称只是一个例子。



        private ILayer relayer;//存储最终获取的图层
        
        private void test()
        {
            //axMapControl1当前地图窗口控件
            IMapControlDefault pMapcontrol = (axMapControl1.Object as IMapControl2) as IMapControlDefault;
            ILayer testlayer = GetLayer(pMapcontrol,"地图名称");
        }

        //根据图层名称获取图层
        private ILayer GetLayer(IMapControlDefault mapcontrol,string LayerName) 
        {
            relayer = null;
            ICompositeLayer pCompositeLayer;
            for (int i = 0; i < mapcontrol.LayerCount; i++)//遍历所有图层
            {
                if (mapcontrol.get_Layer(i) is ICompositeLayer)
                {
                    string test = mapcontrol.get_Layer(i).Name;
                    pCompositeLayer = mapcontrol.get_Layer(i) as ICompositeLayer;
                    Togetlayer(pCompositeLayer, LayerName);
                    if (relayer != null)
                    {
                        return relayer;
                    }
                }
                else if (mapcontrol.get_Layer(i).Name == LayerName)
                {
                    return mapcontrol.get_Layer(i);
                }
            }
            return null;
        }
        
        //遍历要素集下的所有图层
        private void Togetlayer(ICompositeLayer pCompositeLayer, string name)
        {
            ICompositeLayer icolayer;
            for (int i = 0; i < pCompositeLayer.Count; i++)
            {
                string test = pCompositeLayer.get_Layer(i).Name;
                if (pCompositeLayer.get_Layer(i) is ICompositeLayer)
                {
                    icolayer = pCompositeLayer.get_Layer(i) as ICompositeLayer;
                    Togetlayer(icolayer, name);
                }
                if (pCompositeLayer.get_Layer(i).Name == name)
                {
                    relayer = pCompositeLayer.get_Layer(i);
                }
            }
        }

 

你可能感兴趣的:(Arcgis,Engine)