Exemple #1
0
        /// <summary>
        /// Creates a new WmsTileSource
        /// </summary>
        /// <param name="info">WmsInfo with the data needed for creation.</param>
        /// <returns>The created WmsTileSource</returns>
        public static WmsTileSource Create(WmsInfo info)
        {
            var schema = new TileSchema
            {
                Format = "image/png",
                Srs    = info.Crs,
            };
            const int tileWidth  = 256;
            const int tileHeight = 256;
            var       request    = new WmsRequest(
                new Uri(info.WmsCapabilities.Capability.Request.GetMap.DCPType[0].Http.Get.OnlineResource.Href),
                schema,
                tileWidth,
                tileHeight,
                new List <string> {
                info.Layer.Name
            },
                info.Style == null ? null : new List <string> {
                info.Style
            },
                info.CustomParameters,
                info.WmsCapabilities.Version.VersionString);

            return(new WmsTileSource(new HttpTileProvider(request, fetchTile: d => RequestHelper.FetchImage(d, info.Credentials)), schema));
        }
Exemple #2
0
        public static WmsTileSource Create(WmsInfo info)
        {
            var schema = new TileSchema
            {
                Format = "image/png",
                Srs    = info.CRS,
                Height = 256,
                Width  = 256,
            };

            var onlineResource = info.WmsCapabilities.Capability.Request.GetCapabilities.DCPType[0].Http.Get.OnlineResource.Href;

            return(new WmsTileSource(new WebTileProvider(new WmsRequest(new Uri(onlineResource),
                                                                        schema,
                                                                        new List <string> {
                info.Layer.Name
            },
                                                                        info.Style == null? null : new List <string> {
                info.Style
            },
                                                                        info.CustomParameters, info.WmsCapabilities.Version.VersionString),
                                                         fetchTile: d => RequestHelper.FetchImage(d, info.Credentials)
                                                         ),
                                     schema));
        }
Exemple #3
0
        private void BtnOkClick(object sender, EventArgs e)
        {
            if (_wmsCapabilities == null)
            {
                MessageBox.Show(Resources.SelectServerToView, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (tvLayers.SelectedNode == null)
            {
                MessageBox.Show(Resources.SelectLayerToView, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (lbCRS.SelectedItem == null)
            {
                MessageBox.Show(Resources.SelectCrsToView, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ProjectionInfo projectionInfo;

            try
            {
                var crs = ((string)lbCRS.SelectedItem).ToUpper();
                if (string.Equals(crs, "CRS:84"))
                {
                    crs = "EPSG:4326";
                }

                var epsgCode = Convert.ToInt32(crs.Replace("EPSG:", string.Empty));
                switch (epsgCode)
                {
                case 3857:
                    projectionInfo = KnownCoordinateSystems.Projected.World.WebMercator;
                    break;

                case 4326:
                    projectionInfo = KnownCoordinateSystems.Geographic.World.WGS1984;
                    break;

                default:
                    projectionInfo = ProjectionInfo.FromEpsgCode(epsgCode);
                    break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.UnsupportedCrs, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Parse custom parameters
            var cs = string.IsNullOrWhiteSpace(tbCustomParameters.Text) ? new Dictionary <string, string>() : tbCustomParameters.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.Split('=')).ToDictionary(d => d[0], d => d[1]);

            WmsInfo      = new WmsInfo(tbServerUrl.Text, _wmsCapabilities, (Layer)tvLayers.SelectedNode.Tag, cs, (string)lbCRS.SelectedItem, projectionInfo, ((StyleWrapper)lbStyles.SelectedItem)?.Style.Name, GetUserCredentials());
            DialogResult = DialogResult.OK;
        }
Exemple #4
0
        public WMSServerParameters(WmsInfo data)
        {
            InitializeComponent();

            WmsInfo = data;
            if (WmsInfo == null)
            {
                return;
            }

            _wmsCapabilities = WmsInfo.WmsCapabilities;
            tbServerUrl.Text = WmsInfo.ServerUrl;
            if (WmsInfo.Credentials != null)
            {
                tbLogin.Text    = WmsInfo.Credentials.UserName;
                tbPassword.Text = WmsInfo.Credentials.Password;
            }

            ShowServerDetails(_wmsCapabilities);
            InitLayers(_wmsCapabilities);

            // Select layer
            tvLayers.SelectedNode = FindNodeByLayer(tvLayers.Nodes, WmsInfo.Layer);
            if (tvLayers.SelectedNode != null)
            {
                tvLayers.SelectedNode.EnsureVisible();
            }
            tvLayers.Select();
            tvLayers.Focus();

            // Select CRS
            lbCRS.SelectedItem = WmsInfo.CRS;

            // Select Style
            for (int i = 0; i < lbStyles.Items.Count; i++)
            {
                var style = (StyleWrapper)lbStyles.Items[i];
                if (style.Style.Name == WmsInfo.Style)
                {
                    lbStyles.SelectedIndex = i;
                    break;
                }
            }

            // Show custom parameters
            if (WmsInfo.CustomParameters != null)
            {
                tbCustomParameters.Text = string.Join(Environment.NewLine,
                                                      WmsInfo.CustomParameters.Select(d => string.Format("{0}={1}", d.Key, d.Value)));
            }
        }
Exemple #5
0
        private void BtnGetCapabilitiesClick(object sender, EventArgs e)
        {
            var serverUrl = tbServerUrl.Text;

            if (string.IsNullOrWhiteSpace(serverUrl))
            {
                return;
            }

            serverUrl = PrepareUri(serverUrl);

            var noCapabilities = serverUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) < 0;
            var noService      = serverUrl.IndexOf("SERVICE=WMS", StringComparison.OrdinalIgnoreCase) < 0;

            if (noCapabilities)
            {
                serverUrl += "Request=GetCapabilities&";
            }

            if (noService)
            {
                serverUrl += "SERVICE=WMS&";
            }

            WmsCapabilities capabilities;

            try
            {
                var myRequest = WebRequest.Create(serverUrl);
                myRequest.Credentials = GetUserCredentials();
                using (var myResponse = myRequest.GetResponse())
                    using (var stream = myResponse.GetResponseStream())
                        capabilities = new WmsCapabilities(stream);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(Resources.UnableToReadCapabilities, ex.Message));
                return;
            }

            WmsInfo          = null;
            _wmsCapabilities = capabilities;

            ShowServerDetails(capabilities);
            InitLayers(capabilities);
        }
        public WMSServerParameters(WmsInfo data)
        {
            InitializeComponent();
            
            WmsInfo = data;
            if (WmsInfo == null) return;

            _wmsCapabilities = WmsInfo.WmsCapabilities;
            tbServerUrl.Text = WmsInfo.ServerUrl;
            if (WmsInfo.Credentials != null)
            {
                tbLogin.Text = WmsInfo.Credentials.UserName;
                tbPassword.Text = WmsInfo.Credentials.Password;
            }

            ShowServerDetails(_wmsCapabilities);
            InitLayers(_wmsCapabilities);

            // Select layer
            tvLayers.SelectedNode = FindNodeByLayer(tvLayers.Nodes, WmsInfo.Layer);
            if (tvLayers.SelectedNode != null) tvLayers.SelectedNode.EnsureVisible();
            tvLayers.Select();
            tvLayers.Focus();

            // Select CRS
            lbCRS.SelectedItem = WmsInfo.CRS;

            // Select Style
            for (int i = 0; i < lbStyles.Items.Count; i++)
            {
                var style = (StyleWrapper) lbStyles.Items[i];
                if (style.Style.Name == WmsInfo.Style)
                {
                    lbStyles.SelectedIndex = i;
                    break;
                }
            }

            // Show custom parameters
            if (WmsInfo.CustomParameters != null)
            {
                tbCustomParameters.Text = string.Join(Environment.NewLine,
                    WmsInfo.CustomParameters.Select(d => string.Format("{0}={1}", d.Key, d.Value)));
            }
        }
Exemple #7
0
        public static WmsTileSource Create(WmsInfo info)
        {
            var schema = new TileSchema
            {
                Format = "image/png",
                Srs = info.CRS,
                Height = 256,
                Width = 256,
            };

            var onlineResource = info.WmsCapabilities.Capability.Request.GetCapabilities.DCPType[0].Http.Get.OnlineResource.Href;
            return new WmsTileSource(new WebTileProvider(new WmsRequest(new Uri(onlineResource), 
                schema,
                new List<string>{info.Layer.Name},
                info.Style == null? null : new List<string>{info.Style},
                info.CustomParameters, info.WmsCapabilities.Version.VersionString),
                fetchTile: d => RequestHelper.FetchImage(d, info.Credentials)
                ),
                schema);
        }
        public WmsServiceProvider(string name) : 
            base(name, null, new MemoryCache<byte[]>())
        {
            Configure = delegate
            {
                using (var wmsDialog = new WMSServerParameters(_data))
                {
                    if (wmsDialog.ShowDialog() != DialogResult.OK) return false;

                    _data = wmsDialog.WmsInfo;
                    if (_data != null)
                    {

                        TileSource = WmsTileSource.Create(_data);
                        TileCache = new MemoryCache<byte[]>();
                        return true;
                    }
                    return false;
                }
            };
        }
Exemple #9
0
        private void btnGetCapabilities_Click(object sender, EventArgs e)
        {
            var serverUrl = tbServerUrl.Text;

            if (String.IsNullOrWhiteSpace(serverUrl))
            {
                return;
            }

            if (serverUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) < 0)
            {
                serverUrl = serverUrl + "&Request=GetCapabilities";
            }
            if (serverUrl.IndexOf("SERVICE=WMS", StringComparison.OrdinalIgnoreCase) < 0)
            {
                serverUrl = serverUrl + "&SERVICE=WMS";
            }

            WmsCapabilities capabilities;

            try
            {
                var myRequest = WebRequest.Create(serverUrl);
                myRequest.Credentials = GetUserCredentials();
                using (var myResponse = myRequest.GetResponse())
                    using (var stream = myResponse.GetResponseStream())
                        capabilities = new WmsCapabilities(stream);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to read capabilities: " + ex.Message);
                return;
            }

            WmsInfo          = null;
            _wmsCapabilities = capabilities;

            ShowServerDetails(capabilities);
            InitLayers(capabilities);
        }
Exemple #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WmsServiceProvider"/> class.
        /// </summary>
        /// <param name="name">Name of the service provider.</param>
        public WmsServiceProvider(string name)
            : base(name, null, new MemoryCache <byte[]>())
        {
            Configure = () =>
            {
                using var wmsDialog = new WmsServerParameters(_data);
                if (wmsDialog.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                _data = wmsDialog.WmsInfo;
                if (_data != null)
                {
                    TileSource = WmsTileSource.Create(_data);
                    TileCache  = new MemoryCache <byte[]>();
                    return(true);
                }

                return(false);
            };
        }
        private void btnGetCapabilities_Click(object sender, EventArgs e)
        {
            var serverUrl = tbServerUrl.Text;
            if (String.IsNullOrWhiteSpace(serverUrl)) return;

            if (serverUrl.IndexOf("Request=GetCapabilities", StringComparison.OrdinalIgnoreCase) < 0)
            {
                serverUrl = serverUrl + "&Request=GetCapabilities";
            }
            if (serverUrl.IndexOf("SERVICE=WMS", StringComparison.OrdinalIgnoreCase) < 0)
            {
                serverUrl = serverUrl + "&SERVICE=WMS";
            }

            WmsCapabilities capabilities;
            try
            {
                var myRequest = WebRequest.Create(serverUrl);
                myRequest.Credentials = GetUserCredentials();
                using (var myResponse = myRequest.GetResponse())
                using (var stream = myResponse.GetResponseStream())
                    capabilities = new WmsCapabilities(stream);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to read capabilities: " + ex.Message);
                return;
            }

            WmsInfo = null;
            _wmsCapabilities = capabilities;

            ShowServerDetails(capabilities);
            InitLayers(capabilities);
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (_wmsCapabilities == null)
            {
                MessageBox.Show("Select server to view.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (tvLayers.SelectedNode == null)
            {
                MessageBox.Show("Select layer to view.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (lbCRS.SelectedItem == null)
            {
                MessageBox.Show("Select CRS to view.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ProjectionInfo projectionInfo;
            try
            {
                var crs = (string) lbCRS.SelectedItem;
                if (string.Equals(crs, "CRS:84", StringComparison.OrdinalIgnoreCase))
                {
                    crs = "EPSG:4326";
                }
                var epsgCode = Convert.ToInt32(crs.Replace("EPSG:", ""));
                switch (epsgCode)
                {
                   case 3857:
                        projectionInfo = KnownCoordinateSystems.Projected.World.WebMercator;
                        break;
                    case 4326:
                        projectionInfo = KnownCoordinateSystems.Geographic.World.WGS1984;
                        break;
                    default:
                        projectionInfo = ProjectionInfo.FromEpsgCode(epsgCode);
                        break;
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("Unsupported CRS. Select another CRS.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Parse custom parameters
            var cs = string.IsNullOrWhiteSpace(tbCustomParameters.Text)
                ? new Dictionary<string, string>()
                : tbCustomParameters.Text.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                    .Select(d => d.Split('=')).ToDictionary(d => d[0], d => d[1]);

            WmsInfo = new WmsInfo(tbServerUrl.Text, 
                _wmsCapabilities,
                (Layer) tvLayers.SelectedNode.Tag, cs, (string) lbCRS.SelectedItem, projectionInfo,
                lbStyles.SelectedItem == null? null :
                ((StyleWrapper)lbStyles.SelectedItem).Style.Name, GetUserCredentials());
            DialogResult = DialogResult.OK;
        }