internal void addWebMappingServiceToMap(Uri WMS_URI) { try { // create a new WMS layer for the ArcMap session IWMSGroupLayer wmsMapLayer = new WMSMapLayer() as IWMSGroupLayer; IWMSConnectionName connName = new WMSConnectionName(); // set the property of the WMS layer to the Google Maps Engine // WMS GetCapabilities URL IPropertySet propSet = new PropertySet(); propSet.SetProperty("URL", WMS_URI.ToString()); connName.ConnectionProperties = propSet; // create a new data layer for the WMS layer to use IDataLayer dataLayer = wmsMapLayer as IDataLayer; dataLayer.Connect((IName)connName); // create a new ArcMap layer and add the WMS. Set the name. IWMSServiceDescription serviceDesc = wmsMapLayer.WMSServiceDescription; ILayer layer = wmsMapLayer as ILayer; layer.Name = serviceDesc.WMSTitle; // add the WMS layer to the ArcMap session m_map.AddLayer(layer); } catch (System.Exception ex) { // an error occured and was not able to add the WMS layer to the map log.Error(ex); System.Windows.Forms.MessageBox.Show("Unable to add a WMS layer to the map."); } }
/// <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); }
public static void AddWMSTopMap(string title, string wmsURL, IGroupLayer parentGrpLayer) { // WMS GetCapabilities URL IPropertySet propSet = new PropertySet(); propSet.SetProperty("URL", wmsURL); IWMSGroupLayer wmsMapLayer = new WMSMapLayer() as IWMSGroupLayer; IWMSConnectionName connName = new WMSConnectionName(); // set the property of the WMS layer to the Google Maps Engine connName.ConnectionProperties = propSet; // create a new data layer for the WMS layer to use IDataLayer dataLayer = wmsMapLayer as IDataLayer; dataLayer.Connect((IName)connName); // create a new ArcMap layer and add the WMS. Set the name. IWMSServiceDescription serviceDesc = wmsMapLayer.WMSServiceDescription; ILayer layer = wmsMapLayer as ILayer; layer.Name = title; layer.Visible = true; //layer.Name = serviceDesc.WMSTitle; // add the WMS layer to the ArcMap session ((IMapLayers)ArcMap.Document.FocusMap).InsertLayerInGroup(parentGrpLayer, layer, true, 0); //((IMapLayers)ArcMap.Document.FocusMap).InsertLayer(layer, true, 0); if (wmsMapLayer is ICompositeLayer) { ChangeGroupLayerVisibility((ICompositeLayer)wmsMapLayer, true); } ArcMap.Document.UpdateContents(); ArcMap.Document.ActiveView.Refresh(); ArcMap.Document.CurrentContentsView.Refresh(null); }
/// <summary> /// Add WMS map service layer to map /// </summary> /// <param name="msi">Map service information</param> private void AddLayerWMS(esri.gpt.csw.MapServiceInfo msi, Boolean fromServerUrl) { if (msi == null) { throw new ArgumentNullException(); } try { string url = AppendQuestionOrAmpersandToUrlString(msi.Server); // append serviceParam to server url // todo: does msi.ServiceParam have a leading "?" or "&"? if (msi.ServiceParam.Length > 0 || !fromServerUrl) { url = url + msi.ServiceParam; url = AppendQuestionOrAmpersandToUrlString(url); } IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("URL", url); IMxDocument mxDoc = (IMxDocument)m_application.Document; IMap map = (IMap)mxDoc.FocusMap; IActiveView activeView = (IActiveView)map; IWMSGroupLayer wmsGroupLayer = (IWMSGroupLayer)new WMSMapLayerClass(); IWMSConnectionName wmsConnectionName = new WMSConnectionName(); wmsConnectionName.ConnectionProperties = propertySet; // connect to wms service IDataLayer dataLayer; bool connected = false; try { dataLayer = (IDataLayer)wmsGroupLayer; IName connName = (IName)wmsConnectionName; connected = dataLayer.Connect(connName); } catch (Exception ex) { ShowErrorMessageBox (StringResources.ConnectToMapServiceFailed + "\r\n" + ex.Message); connected = false; } if (!connected) return; // get service description out of the layer. the service description contains // inforamtion about the wms categories and layers supported by the service IWMSServiceDescription wmsServiceDesc = wmsGroupLayer.WMSServiceDescription; IWMSLayerDescription wmsLayerDesc; 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) { throw new Exception(StringResources.CreateWmsLayerFailed); }; } else { // wms group layer newWmsGroupLayer = wmsGroupLayer.CreateWMSGroupLayers(wmsLayerDesc); newLayer = (ILayer)newWmsGroupLayer; if (newLayer == null) { throw new Exception(StringResources.CreateWmsGroupLayerFailed); } } // add newly created layer // if(wmsGroupLayer.get_Layer(i).Name != newLayer.Name) wmsGroupLayer.InsertLayer(newLayer, 0); }*/ // configure the layer before adding it to the map layer = (ILayer)wmsGroupLayer; layer.Name = wmsServiceDesc.WMSTitle; ExpandLayer(layer, true); SetLayerVisibility(layer, true); // add to focus map map.AddLayer(layer); return; } catch (Exception ex) { ShowErrorMessageBox (StringResources.AddWmsLayerFailed + "\r\n" + ex.Message); } }
/// <summary> /// Add WMS map service layer to map /// </summary> /// <param name="msi">Map service information</param> private void AddLayerWMS(pdok4arcgis.MapServiceInfo msi, Boolean fromServerUrl, string sld) { if (msi == null) { throw new ArgumentNullException(); } try { string url = AppendQuestionOrAmpersandToUrlString(msi.Server); // append serviceParam to server url // todo: does msi.ServiceParam have a leading "?" or "&"? if (msi.ServiceParam.Length > 0 && !fromServerUrl) { url = url + msi.ServiceParam; url = AppendQuestionOrAmpersandToUrlString(url); } if (!String.IsNullOrEmpty(sld)) { url = String.Format("{0}SLD={1}&", url, sld); } IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("URL", url); IMxDocument mxDoc = (IMxDocument)m_application.Document; IMap map = (IMap)mxDoc.FocusMap; IActiveView activeView = (IActiveView)map; IWMSGroupLayer wmsGroupLayer = (IWMSGroupLayer)new WMSMapLayerClass(); IWMSConnectionName wmsConnectionName = new WMSConnectionName(); wmsConnectionName.ConnectionProperties = propertySet; // connect to wms service IDataLayer dataLayer; bool connected = false; try { dataLayer = (IDataLayer)wmsGroupLayer; IName connName = (IName)wmsConnectionName; connected = dataLayer.Connect(connName); } catch (Exception ex) { ShowErrorMessageBox(StringResources.ConnectToMapServiceFailed + "\r\n" + "url:" + url +"\r\n" + ex.Message); connected = false; } if (!connected) return; ILayer layer = (ILayer)wmsGroupLayer; if (wmsGroupLayer.Count == 0) { // Old code that was present for 9.3 but causes issues for 10.1, as the layers have already been added at this point. The documentation for IWMSMapLayer still states that this is necessary to get the layers (but the behaviour changed , probably in ArcGIS 10.0) // get service description out of the layer. the service description contains // inforamtion about the wms categories and layers supported by the service IWMSServiceDescription wmsServiceDesc = wmsGroupLayer.WMSServiceDescription; IWMSLayerDescription wmsLayerDesc; ILayer newLayer; 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) { throw new Exception(StringResources.CreateWmsLayerFailed); }; } else { // wms group layer newWmsGroupLayer = wmsGroupLayer.CreateWMSGroupLayers(wmsLayerDesc); newLayer = (ILayer)newWmsGroupLayer; if (newLayer == null) { throw new Exception(StringResources.CreateWmsGroupLayerFailed); } } // add newly created layer wmsGroupLayer.InsertLayer(newLayer, 0); } // configure the layer before adding it to the map layer.Name = wmsServiceDesc.WMSTitle; } ExpandLayer(layer, true); SetLayerVisibility(layer, true); // add to focus map map.AddLayer(layer); return; } catch (Exception ex) { ShowErrorMessageBox (StringResources.AddWmsLayerFailed + "\r\n" + ex.Message); } }
/// <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; }
/// <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); }