/// <summary> /// 连接到WMS服务器,载入WMS图层 /// </summary> /// <param name="axMapControl1">axMapControl控件</param> /// <param name="url">WMS服务器地址</param> public static void Connect2WMS(AxMapControl axMapControl1, string url) { #region 初始化环境 IPropertySet pPropertyset = new PropertySetClass(); pPropertyset.SetProperty("url", url); IWMSConnectionName pWMSConnectionName = new WMSConnectionNameClass(); pWMSConnectionName.ConnectionProperties = pPropertyset; ILayerFactory pLayerFactory = new EngineWMSMapLayerFactoryClass();//Provides access to members that control the creation of layers through a factory. IWMSGroupLayer pWmsMapLayer = new WMSMapLayerClass(); IDataLayer pDataLayer = pWmsMapLayer as IDataLayer; pDataLayer.Connect(pWMSConnectionName as IName); IWMSServiceDescription pWmsServiceDesc = pWmsMapLayer.WMSServiceDescription; #endregion #region 获取WMS图层 for (int i = 0; i < pWmsServiceDesc.LayerDescriptionCount; i++) { IWMSLayerDescription pWmsLayerDesc = pWmsServiceDesc.get_LayerDescription(i); ILayer pNewLayer = null; if (pWmsLayerDesc.LayerDescriptionCount == 0) { IWMSLayer pWmsLayer = pWmsMapLayer.CreateWMSLayer(pWmsLayerDesc); pNewLayer = pWmsLayer as ILayer; } else { IWMSGroupLayer pWmsGroupLayer = pWmsMapLayer.CreateWMSGroupLayers(pWmsLayerDesc); for (int j = 0; j < pWmsGroupLayer.Count; j++) { ILayer layer = pWmsGroupLayer.get_Layer(j); if (layer.Name != "admin") { pWmsMapLayer.InsertLayer(layer, 0); layer.Visible = true; } } } } #endregion #region 收尾工作 ILayer pLayer = pWmsMapLayer as ILayer; pLayer.Name = pWmsServiceDesc.WMSTitle; pLayer.Visible = true; axMapControl1.AddLayer(pLayer, 0); IEnvelope pEnv = axMapControl1.FullExtent; pEnv.Expand(0.8, 0.8, true); axMapControl1.Extent = pEnv; #endregion }
//pHostOrUrl 服务地址 public void GetWMSServerLayer(string pHostOrUrl, bool pIsLAN) { IWMSConnectionFactory pWmsFac = new WMSConnectionFactory(); IWMSConnection pWmsConn = null; IPropertySet pProSet = new PropertySet(); if (pIsLAN) { pProSet.SetProperty("machine", pHostOrUrl); } else { pProSet.SetProperty("url", pHostOrUrl); } ///连接服务 try { pWmsConn = pWmsFac.Open(pProSet, 0, null); System.Runtime.InteropServices.Marshal.ReleaseComObject(pWmsFac); pWmsFac = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(pProSet); pProSet = null; if (pWmsConn != null) { ///获取所有的服务名称 IWMSConnectionName pWmsConnectionName = pWmsConn.FullName as IWMSConnectionName; ILayerFactory pLayerFactory = new EngineWMSMapLayerFactoryClass(); if (pLayerFactory.get_CanCreate(pWmsConnectionName)) { IEnumLayer pEnumLayer = pLayerFactory.Create(pWmsConnectionName); pEnumLayer.Reset(); ILayer pLayer = pEnumLayer.Next(); while (pLayer != null) { if (pLayer is IWMSMapLayer) { IWMSMapLayer pWmsMapLayer = pLayer as IWMSMapLayer; IWMSGroupLayer pWmsGroupLayer = pWmsMapLayer as IWMSGroupLayer; DevComponents.AdvTree.Node nService = new DevComponents.AdvTree.Node(); nService.Text = pLayer.Name; int iService = advTree1.Nodes.Add(nService);//一级服务名 for (int j = 0; j < pWmsGroupLayer.Count; j++) { ILayer pTmpLayer = pWmsGroupLayer.get_Layer(j); IWMSGroupLayer pTmpWmsGroupLayer = pTmpLayer as IWMSGroupLayer; if (pTmpWmsGroupLayer != null) { DevComponents.AdvTree.Node nMap = new DevComponents.AdvTree.Node(); nMap.Text = pTmpLayer.Name; int iMap = nService.Nodes.Add(nMap);//二级地图名 for (int k = 0; k < pTmpWmsGroupLayer.Count; k++) { ILayer pTmpTmplayer = pTmpWmsGroupLayer.get_Layer(k); DevComponents.AdvTree.Node nLayer = new DevComponents.AdvTree.Node(); nLayer.Text = pTmpTmplayer.Name; nMap.Nodes.Add(nLayer);//三级图层名 } } } } pLayer = pEnumLayer.Next(); } } System.Runtime.InteropServices.Marshal.ReleaseComObject(pLayerFactory); pLayerFactory = null; } } catch { } advTree1.ExpandAll(); }