ArcEngine开发——通过MapDocument组件类来访问maps和layers

MapDocument是一个很有用的组件类,可用于读取和修改地图文档(.mxds)。本文演示如何打开一个地图文档,并且利用一个循环来访问其所有的地图(maps)和图层(layers)。在该示例中,将这些对象的名称输出到控制台。

示例代码

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;

class Program
{
    static void Main(string[] args)
    {
        OpenMXDViaMapDocument(@"F:\Test.mxd");
        Console.ReadKey();
    }

    static void OpenMXDViaMapDocument(string path)
    {
        IMapDocument pMapDocument = new MapDocumentClass();
        if (pMapDocument.get_IsMapDocument(path))
        {
            pMapDocument.Open(path, null);
            IMap pMap;
            for (int i = 0; i <= pMapDocument.MapCount - 1; i++)
            {
                pMap = pMapDocument.get_Map(i);
                Console.WriteLine(pMap.Name);
                IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
                pEnumLayer.Reset();
                ILayer pLayer = pEnumLayer.Next();
                while (pLayer != null)
                {
                    Console.WriteLine("--" + pLayer.Name);

                    pLayer = pEnumLayer.Next();
                }

            }
        }
    }

}

地图文档组织结构

演示程序输出

另外,可以为get_Layers方法指定更为详细的参数,以获取符合条件的Layer。

UID uid = new UID();
uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";//这是获取IFeaureLayer,可以换其它的
IEnumLayer pEnumLayer = axMapControl1.Map.get_Layers(uid, true);
pEnumLayer.Reset();
ILayer pLayer = pEnumLayer.Next();
while (pLayer != null)
{
       //...
}

具体的UID可用的其它类型如下:

原文地址:https://www.cnblogs.com/hans_gis/p/2030253.html