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;
        }
示例#2
0
        /// <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
        }
示例#3
0
        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);
        }
        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;
        }
示例#5
0
        public static ILayer getWMSLayerByName(string WMSurl, string layerName)
        {
            IPropertySet propSet = new PropertySetClass();
            IWMSConnectionName connName = new WMSConnectionNameClass();
            IWMSGroupLayer wmsMapLayer = new WMSMapLayerClass();
            IDataLayer dataLayer = (IDataLayer)wmsMapLayer;
            IWMSServiceDescription serviceDesc;
            ILayer qrylyr; ILayer wmsLyr;
            //connect to url
            propSet.SetProperty("URL", WMSurl);
            connName.ConnectionProperties = propSet;
            dataLayer.Connect((IName)connName);
            //get service description
            serviceDesc = wmsMapLayer.WMSServiceDescription;
            //loop through layers

            List<IWMSLayerDescription> lyrs = listWMSlayers(serviceDesc);
            //query layers
            List<IWMSLayerDescription> qry = (
                from IWMSLayerDescription q in lyrs
                where q.Name == layerName select q).ToList<IWMSLayerDescription>() ;

            //nothing found
            if ( qry.Count == 0 ) return null;

            //else
            IWMSLayerDescription layerDesc = qry[0];
            //clear allready loaded contents
            wmsMapLayer.Clear();

            if (layerDesc.LayerDescriptionCount == 0)
            {
                qrylyr = (ILayer)wmsMapLayer.CreateWMSLayer(layerDesc);
                wmsMapLayer.InsertLayer(qrylyr, 0);
            }
            else
            {
                qrylyr = (ILayer)wmsMapLayer.CreateWMSGroupLayers(layerDesc);
                wmsMapLayer.InsertLayer(qrylyr, 0);
            }
            makeCompositeLayersVisibile(qrylyr);

            wmsMapLayer.Expanded = true;
            ((IWMSMapLayer)wmsMapLayer).BackgroundColor = new RgbColor() { NullColor = true };
            wmsLyr = (ILayer)wmsMapLayer;
            wmsLyr.Name = serviceDesc.WMSTitle;
            wmsLyr.Visible = true;

            return wmsLyr;
        }