小菜的ArcObjects学习之路------如果通过MapDocument访问地图和图层

来源:互联网 发布:天谕捏脸数据玉虚男 编辑:程序博客网 时间:2024/05/17 09:09
              How to access maps and layers via the MapDocument

The MapDocument class is a utility class for reading and modifying map documents (.mxds). This topic demonstrates how to open a map document and loop through all its maps and layers. 

Accessing maps and layers via the MapDocument 
In the following code example, the names of these objects are reported to the console. Do the following steps to access maps and layers:
1:Create a MapDocument.
[C#]
IMapDocument pMapDocument = new MapDocumentClass();
  1. Enumerate the maps in the MapDocument and print their names to the console.See the following code example:
[C#]
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();
        }

    }
}

                       如果通过MapDocument访问地图和图层

MapDocument类是实用类的阅读和修改地图文件(.mxds)。本主题演示如何通过其所有的地图和图层打开地图文档和循环。 

在下面的代码示例,这些对象的名字上报到控制台。请执行下列步骤来访问地图和图层:
1:创建一个MapDocument。
[C#]
IMapDocument pMapDocument = new MapDocumentClass();

2枚举在MapDocument地图和将自己的名字打印到控制台上。看到下面的代码示例:
[C#]
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();
        }

    }
}