レイヤのツリー構造


大抵一覧とるだけで良いが親とったり階層構造としては扱えない

IMap map = this.axMapControl1.Map;

ESRI.ArcGIS.esriSystem.UID uid = new UIDClass();
uid.Value = "{" + typeof(IBasemapLayer).GUID.ToString() + "}";

 IEnumLayer enumLayer = map.get_Layers(uid, false);
enumLayer.Reset();

IBasemapLayer basemapLayer = enumLayer.Next() as
IBasemapLayer;

適当にツリー構造化した例

IMap map = this.axMapControl1.Map;
var layerCt = new LayersCounter();

IEnumerable<LayerAndPath> layers = layerCt.EnumLayerPath(map);

StringBuilder sb = new StringBuilder();
foreach (var layerPath in layers)
{
    for (int i = 1; i < layerPath.Path.Length; i++)
    {
        sb.Append(" ");
    }
    sb.Append(layerPath.Layer.Name);
    sb.Append(Environment.NewLine);
}
Debug.Print(sb.ToString());

出力は下記のようにグループレイヤの階層構造を空白で表す形

道路
 高速道路
 国道
線路
 地下鉄
 JR
 私鉄

詳細は下記

構造体

public struct LayerAndPath
{
    public int[] Path;

    public string[] NamePath;

    public ILayer Layer;

    public object Parent;

    public LayerAndPath(ILayer layer, int[] path, string[] namePath, object parent)
    {
        this.Path = path;
        this.Layer = layer;
        this.NamePath = namePath;
        this.Parent = parent;
    }//end method
}//end class

親から回してとるクラス

public class LayersCounter
{
    public IEnumerable<LayerAndPath> EnumLayerPath(IMap map)
    {
        return getLayerPath(map, new int[0], new string[0]);
    }

    private IEnumerable<LayerAndPath> getLayerPath(object parent, int[] path, string[] namePath)
    {
        int layerCount = 0;
        if (parent is IMap)
            layerCount = ((IMap)parent).LayerCount;
        else
            layerCount = ((ICompositeLayer)parent).Count;

        for (int i = 0; i < layerCount; i++)
        {
            ILayer layer = null;
            if (parent is IMap)
                layer = ((IMap)parent).get_Layer(i);
            else
                layer = ((ICompositeLayer)parent).get_Layer(i);

            var lst = new List<int>(path);
            var lstName = new List<string>(namePath);
            lst.Add(i);
            lstName.Add(layer.Name);

            var result = new LayerAndPath(layer, lst.ToArray(), lstName.ToArray(), parent);
            yield return result;

            if (layer is ICompositeLayer &&
                layer is IGroupLayer)
            {
                //再帰呼び
                var childLayer = this.getLayerPath(layer, lst.ToArray(), lstName.ToArray());
                foreach (var item in childLayer)
                {
                    yield return item;
                }//end c loop
            }//end if

        }//end loop

        yield break;
    }//end method

}//end class
カテゴリー: 開発 タグ: パーマリンク