public ILayer GetWmsMapServerLyr(string url, string serviceName) { IPropertySet pPropertyset = new PropertySetClass(); pPropertyset.SetProperty("url", url); IWMSConnectionName pWmsConnectionName = new WMSConnectionNameClass(); pWmsConnectionName.ConnectionProperties = pPropertyset; IWMSConnectionFactory wmsConnFac = new WMSConnectionFactoryClass(); IName nn = wmsConnFac.Open(pPropertyset, 0, null) as IName; IWMSGroupLayer pWmsMapLayer = new WMSMapLayerClass(); IDataLayer pDataLayer = pWmsMapLayer as IDataLayer; pDataLayer.Connect(pWmsConnectionName as IName); IWMSServiceDescription pWmsServiceDesc = pWmsMapLayer.WMSServiceDescription; 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.ToLower() == serviceName.ToLower()) { pWmsMapLayer.InsertLayer(layer, 0); layer.Visible = true; break; } } } } ILayer pLayer = pWmsMapLayer as ILayer; pLayer.Name = pWmsServiceDesc.WMSTitle; pLayer.Visible = true; return(pLayer); }
/// <summary> /// 添加 WMS 网络图层 /// </summary> /// <param name="map">地图对象</param> /// <param name="address">网址</param> /// <param name="layerName">图层名称</param> /// <param name="layerPosition">图层添加位置</param> public void addWMSLayer(IMap map, string address, string layerName, int layerPosition) { IPropertySet propSet = new PropertySet(); propSet.SetProperty("url", address); IWMSConnectionName wmsConName = new WMSConnectionName(); wmsConName.ConnectionProperties = propSet; IWMSGroupLayer wmsMapLayer = new WMSMapLayer() as IWMSGroupLayer; IDataLayer dataLayer = wmsMapLayer as IDataLayer; dataLayer.Connect(wmsConName as IName); IWMSServiceDescription wmsServiceDescription = wmsMapLayer.WMSServiceDescription; ILayer theLayer = null; for (int i = 0; i < wmsServiceDescription.LayerDescriptionCount; i++) { IWMSLayerDescription wmsLayerDesc = wmsServiceDescription.get_LayerDescription(i); if (wmsLayerDesc.LayerDescriptionCount == 0) { IWMSLayer wmsLayer = wmsMapLayer.CreateWMSLayer(wmsLayerDesc); theLayer = wmsLayer as ILayer; } else { IWMSGroupLayer wmsGroupLayer = wmsMapLayer.CreateWMSGroupLayers(wmsLayerDesc); for (int j = 0; j < wmsGroupLayer.Count; j++) { ILayer layer = wmsGroupLayer.get_Layer(i); layer.Visible = true; wmsMapLayer.InsertLayer(layer, 0); } theLayer = wmsMapLayer as ILayer; } } theLayer.Name = wmsServiceDescription.WMSTitle; theLayer.Visible = true; map.AddLayer(theLayer); map.MoveLayer(theLayer, layerPosition); }
protected override void OnClick() { IMxDocument mxDoc = (IMxDocument)ArcMap.Application.Document; IWMSGroupLayer wmsMapLayer = new WMSMapLayerClass(); IWMSConnectionName connName = new WMSConnectionNameClass(); IPropertySet propSet = new PropertySetClass(); propSet.SetProperty("URL", "https://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer"); connName.ConnectionProperties = propSet; //Put the WMS service layers in a DataLayer IDataLayer dataLayer = (IDataLayer)wmsMapLayer; dataLayer.Connect((IName)connName); // Get access to WMS service and layer propeties IWMSServiceDescription serviceDesc = wmsMapLayer.WMSServiceDescription; IWMSLayerDescription groupDesc = serviceDesc.LayerDescription[0]; //Clear existing WMS service group layer. wmsMapLayer.Clear(); //Create an empty layer and populate it with the desired sub layer index. ILayer newLayer; IWMSLayer newWMSLayer = wmsMapLayer.CreateWMSLayer(groupDesc.LayerDescription[1]); newLayer = (ILayer)newWMSLayer; wmsMapLayer.InsertLayer(newLayer, 0); //Add the layer to the map. mxDoc.FocusMap.AddLayer((ILayer)wmsMapLayer); IActiveView activeView = (IActiveView)mxDoc.FocusMap; activeView.Refresh(); ArcMap.Application.CurrentTool = null; }
private static List<IWMSLayerDescription> ListWMSlayersByDescription(IWMSLayerDescription root) { List<IWMSLayerDescription> lyrNames = new List<IWMSLayerDescription>(); lyrNames.Add(root); for (int i = 0; i < root.LayerDescriptionCount; i++) { IWMSLayerDescription layerDesc = root.get_LayerDescription(i); lyrNames.Add(layerDesc); if (layerDesc.LayerDescriptionCount != 0) { List<IWMSLayerDescription> namesL2 = ListWMSlayersByDescription(layerDesc); lyrNames.AddRange(namesL2); } } return lyrNames; }
/// <summary> /// Adds WMS document to map /// </summary> /// <param name="ipMxDoc"></param> /// <param name="strServer"></param> /// <param name="strLayerName"></param> /// <param name="strSecretName"></param> /// <returns></returns> private bool addLayerWMS(IMxDocument ipMxDoc, string strServer, string strLayerName, string strSecretName) { IPropertySet ipPropSet = new PropertySet(); string strFinalChar; // check the final char strFinalChar = strServer.Substring(strServer.Length - 1); if (strFinalChar == "?" && strFinalChar != "&") { if (strServer.Contains("?")) { ipPropSet.SetProperty("URL", (strServer + '&')); } else { ipPropSet.SetProperty("URL", (strServer + '?')); } } else { ipPropSet.SetProperty("URL", strServer + '?'); } IMap map = (IMap)ipMxDoc.FocusMap; IActiveView activeView = (IActiveView)map; IWMSGroupLayer wmsGroupLayer = (IWMSGroupLayer) new WMSMapLayerClass(); IWMSConnectionName wmsConnectionName = new WMSConnectionName(); wmsConnectionName.ConnectionProperties = ipPropSet; IDataLayer dataLayer; bool connected = false; try { dataLayer = (IDataLayer)wmsGroupLayer; IName connName = (IName)wmsConnectionName; connected = dataLayer.Connect(connName); } catch (Exception ex) { connected = false; } if (!connected) { return(false); } // get service description out of the layer. the service description contains // information about the wms categories and layers supported by the service IWMSServiceDescription wmsServiceDesc = wmsGroupLayer.WMSServiceDescription; IWMSLayerDescription wmsLayerDesc = null; ILayer newLayer; ILayer layer; IWMSLayer newWmsLayer; IWMSGroupLayer newWmsGroupLayer; for (int i = 0; i < wmsServiceDesc.LayerDescriptionCount; i++) { newLayer = null; wmsLayerDesc = wmsServiceDesc.get_LayerDescription(i); if (wmsLayerDesc.LayerDescriptionCount == 0) { // wms layer newWmsLayer = wmsGroupLayer.CreateWMSLayer(wmsLayerDesc); newLayer = (ILayer)newWmsLayer; if (newLayer == null) { } } else { // wms group layer newWmsGroupLayer = wmsGroupLayer.CreateWMSGroupLayers(wmsLayerDesc); newLayer = (ILayer)newWmsGroupLayer; if (newLayer == null) { } } // add newly created layer // wmsGroupLayer.InsertLayer(newLayer, 0); } // configure the layer before adding it to the map layer = (ILayer)wmsGroupLayer; layer.Name = wmsServiceDesc.WMSTitle; // add to focus map map.AddLayer(layer); // add to the service list string strItem = EncodeServiceList(strServer, wmsServiceDesc.WMSTitle, strSecretName); // m_pLogger.Msg "adding to service list : " & strItem colServiceList.Add(strItem); // set flag return(true); }