Пример #1
0
        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.");
            }
        }
Пример #2
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);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        /// <summary>
        /// 获取WMS的地图服务图层
        /// </summary>
        /// <param name="serviceUrl">服务地址</param>
        /// <returns></returns>
        public ILayer GetWMSLayer(string serviceUrl)
        {
            IPropertySet propertyset = new PropertySetClass();

            propertyset.SetProperty("url", serviceUrl);

            IWMSConnectionName wmsConnectionName = new WMSConnectionNameClass();

            wmsConnectionName.ConnectionProperties = propertyset;

            IWMSGroupLayer wmsMapLayer = new WMSMapLayerClass();
            IDataLayer     dataLayer   = wmsMapLayer as IDataLayer;

            dataLayer.Connect(wmsConnectionName as IName);
            IWMSServiceDescription wmsServiceDesc = wmsMapLayer.WMSServiceDescription;

            ILayer layer = wmsMapLayer as ILayer;

            layer.Name    = wmsServiceDesc.WMSTitle;
            layer.Visible = true;
            return(layer);
        }
        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;
        }
Пример #7
0
        /// <summary>
        /// Parse WMS Service
        /// </summary>
        /// <param name="urlWMS"></param>
        public void OpenWMS(string urlWMS)
        {
            try
            {
                ///Create an WMSMapLayer Instance - this will be added to the map later
                IWMSGroupLayer pWMSMapLayer = new WMSMapLayerClass() as IWMSGroupLayer;

                ///Create and configure wms connection name, this is used to store the connection properties
                IWMSConnectionName pConnName = new WMSConnectionNameClass();
                IPropertySet       pPropSet  = new PropertySetClass();
                pPropSet.SetProperty("URL", urlWMS);
                pConnName.ConnectionProperties = pPropSet;

                ///Use the name information to connect to the service
                IDataLayer pDataLayer = pWMSMapLayer as IDataLayer;
                IName      pName      = pConnName as IName;
                pDataLayer.Connect(pName);

                ///Get service description, which includes the categories of wms layers
                IWMSServiceDescription pServiceDesc = pWMSMapLayer.WMSServiceDescription;

                ///Configure the layer before adding it to the map
                ILayer pLayer = pWMSMapLayer as ILayer;
                pLayer.Name = pServiceDesc.WMSTitle;
                AddLayer(pLayer);
            }
            catch (System.Runtime.InteropServices.COMException cEx)
            {
                ///Catch comexception handler
                MessageBox.Show("The wms url is invalid!\r\rError message:\r" + cEx.Message);
            }
            catch (Exception ex)
            {
                ///Catch general exception handler
                MessageBox.Show(ex.StackTrace);
            }
        }
Пример #8
0
        public static List<IWMSLayerDescription> listWMSlayers(IWMSServiceDescription serviceDesc)
        {
            List<IWMSLayerDescription> lyrNames = new List<IWMSLayerDescription>();

            for (int i = 0; i < serviceDesc.LayerDescriptionCount; i++)
            {
                lyrNames.AddRange(
                    ListWMSlayersByDescription(serviceDesc.get_LayerDescription(i)));
            }
            return lyrNames;
        }
Пример #9
0
        /// <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);
        }