2010년 7월 26일 월요일

[ArcObjects]WMS Layer

ArcObjects에서 OGC WMS(Web Map Service) 레이어를 추가하는 코드입니다.

아래 코드의 ServiceURL에 http://gnis.lbsplus.com/gnis/wms?GDX=tilo.xml&를 입력한 결과는 아래 이미지...


▣ Sample Code
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.GISClient;

public static void AddWMSLayer(IMap targetMap, String serviceURL)
{
    ILayer wmsLayer = OpenWMSLayer(serviceURL, string.Empty);

    if (wmsLayer != null)
    {
        targetMap.AddLayer(wmsLayer);

        IActiveView activeView = (IActiveView)targetMap;
        activeView.Refresh();
    }
}

public static ILayer OpenWMSLayer(string serviceURL, string layerName)
{
    IPropertySet propSet = new PropertySetClass();
    propSet.SetProperty("URL", serviceURL);

    IWMSConnectionName connName = new WMSConnectionNameClass();
    connName.ConnectionProperties = propSet;

    IWMSGroupLayer wmsMapLayer = new WMSMapLayerClass();
   
    IDataLayer dataLayer = (IDataLayer)wmsMapLayer;
    dataLayer.Connect((IName)connName);

    IWMSServiceDescription serviceDesc = wmsMapLayer.WMSServiceDescription;

    for (int k = 0; k < serviceDesc.LayerDescriptionCount; k++)
    {
        IWMSLayerDescription layerDesc = serviceDesc.get_LayerDescription(k);

        ILayer newLayer = null;
        if (layerDesc.LayerDescriptionCount == 0)
        {
            try
            {
                IWMSLayer newWMSLayer = wmsMapLayer.CreateWMSLayer(layerDesc);
                newLayer = (ILayer)newWMSLayer;
            }
            catch (Exception Ex)
            {
                System.Diagnostics.Debug.WriteLine(Ex.Message);
            }

            if (newLayer == null)
            {
                IWMSGroupLayer grpLayer = wmsMapLayer.CreateWMSGroupLayers(layerDesc);
                newLayer = (ILayer)grpLayer;
            }
        }
        else
        {
            IWMSGroupLayer grpLayer = wmsMapLayer.CreateWMSGroupLayers(layerDesc);
            newLayer = (ILayer)grpLayer;
        }

        newLayer.Visible = true;
        wmsMapLayer.InsertLayer(newLayer, 0);
    }

    if (wmsMapLayer == null || wmsMapLayer.Count == 0)
        return null;

    ILayer wmsLayer = (ILayer)wmsMapLayer;
    wmsLayer.Visible = true;

    if (string.IsNullOrEmpty(layerName))
    {
        wmsLayer.Name = serviceDesc.WMSTitle;
    }
    else
    {
        wmsLayer.Name = layerName;
    }

    return wmsLayer;
}


▣ 결과
WMS UI
ArcMap WMS Layer