This is a migrated thread and some comments may be shown as answers.

WmsTiledProvider Format Problem

1 Answer 56 Views
Map
This is a migrated thread and some comments may be shown as answers.
MCanitez
Top achievements
Rank 1
MCanitez asked on 17 Jul 2020, 01:18 PM

Hi,

I'm using WmsTiledProvider. My problem is that the format value sent is PNG and I cannot change it. How can I solve this problem.








 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Vladimir Stoyanov
Telerik team
answered on 22 Jul 2020, 07:29 AM

Hello,

Thank you for the shared pictures. 

In order to change the format, you can implement a custom WmsTiledProvider and a custom tile source. 

public class CustomWmsTiledProvider : TiledProvider, ICloneable
    {
        /// <summary>
        /// Identifies the BaseUri dependency property.
        /// </summary>
        public static readonly DependencyProperty BaseUriProperty = DependencyProperty.Register(
            "BaseUri",
            typeof(string),
            typeof(WmsTiledProvider),
            new PropertyMetadata(null, ParametersChanged));

        /// <summary>
        /// Identifies the Layers dependency property.
        /// </summary>
        public static readonly DependencyProperty LayersProperty = DependencyProperty.Register(
            "Layers",
            typeof(string),
            typeof(WmsTiledProvider),
            new PropertyMetadata("Countries,Borders,Coastlines", ParametersChanged));

        /// <summary>
        /// Identifies the Projection dependency property.
        /// </summary>
        public static readonly DependencyProperty ProjectionProperty = DependencyProperty.Register(
            "Projection",
            typeof(string),
            typeof(WmsTiledProvider),
            new PropertyMetadata("EPSG:4326", ParametersChanged));

        /// <summary>
        /// Identifies the Version dependency property.
        /// </summary>
        public static readonly DependencyProperty VersionProperty = DependencyProperty.Register(
            "Version",
            typeof(string),
            typeof(WmsTiledProvider),
            new PropertyMetadata("1.3.0", ParametersChanged, VersionCoerced));

        private static readonly string[] SupportedVersions = new string[]
        {
            "1.3.0",
            "1.1.1",
            "1.1.0",
            "1.0.7",
            "1.0.0"
        };

        private MercatorProjection projection = new MercatorProjection();

        /// <summary>
        /// Initializes a new instance of the WmsTiledProvider class.
        /// </summary>
        public CustomWmsTiledProvider()
            : base()
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the WmsTiledProvider class.
        /// </summary>
        /// <param name="baseUri">Base URI of the WMS server.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "0#")]
        public CustomWmsTiledProvider(string baseUri)
        {
            this.BaseUri = baseUri;
           
        }

        public void Setup()
        {
            this.Layers = "osm";
            this.Version = "1.1.0";
            this.Projection = "EPSG:4326";


            var tileSource = new CustomWmsTileSource(this.BaseUri, 256, 256);
            this.InitializeTileSource(tileSource);


            this.MapSources.Add(tileSource.UniqueId, tileSource);
        }

        /// <summary>
        /// Gets or sets Base URI of the WMS server.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
        public string BaseUri
        {
            get
            {
                return (string)this.GetValue(BaseUriProperty);
            }

            set
            {
                this.SetValue(BaseUriProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets layers are requested from the WMS server.
        /// </summary>
        public string Layers
        {
            get
            {
                return (string)this.GetValue(LayersProperty);
            }

            set
            {
                this.SetValue(LayersProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets coordinate Reference System (CRS) or Spatial Reference System (SRS) identifier.
        /// Reserved for future use.
        /// </summary>
        public string Projection
        {
            get
            {
                return (string)this.GetValue(ProjectionProperty);
            }

            set
            {
                this.SetValue(ProjectionProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets the version of the requested service.
        /// </summary>
        public string Version
        {
            get
            {
                return (string)this.GetValue(VersionProperty);
            }

            set
            {
                this.SetValue(VersionProperty, value);
            }
        }

        /// <summary>
        /// Returns the SpatialReference for the map provider.
        /// </summary>
        public override ISpatialReference SpatialReference
        {
            get
            {
                return this.projection;
            }
        }

        /// <summary>
        /// Implements the ICloneable interface.
        /// </summary>
        /// <returns>Cloned object.</returns>
		public new object Clone()
        {
            CustomWmsTiledProvider clone = new CustomWmsTiledProvider(this.BaseUri);
            clone.Layers = this.Layers;
            this.InheritParameters(clone);

            return clone;
        }

        private static void ParametersChanged(DependencyObject source, DependencyPropertyChangedEventArgs eventArgs)
        {
            CustomWmsTiledProvider provider = source as CustomWmsTiledProvider;
            if (provider != null)
            {
                provider.OnParametersChanged();
            }
        }

        private static object VersionCoerced(DependencyObject source, object value)
        {
            string version = value as string;

            if (version != null)
            {
                if (!CustomWmsTiledProvider.SupportedVersions.Contains(version))
                {
                    version = "1.3.0";
                }
            }
            else
            {
                version = "1.3.0";
            }

            return version;
        }

        public void InitializeTileSource(CustomWmsTileSource source)
        {
            source.BaseUri = this.BaseUri;
            source.Layers = this.Layers;
            source.Projection = this.Projection;
            source.Version = this.Version;
        }

        private void OnParametersChanged()
        {
            if (this.ValidateParameters())
            {
                if (this.MapSources.Count > 0)
                {
                    CustomWmsTileSource source = this.MapSources[this.MapSources.Keys.FirstOrDefault()] as CustomWmsTileSource;
                    if (source != null)
                    {
                        this.InitializeTileSource(source);
                    }
                }
                else
                {
                    CustomWmsTileSource source = new CustomWmsTileSource(this.BaseUri, 256, 256);
                    this.InitializeTileSource(source);
                    this.MapSources.Add(source.UniqueId, source);
                }
            }
        }

        private bool ValidateParameters()
        {
            return !string.IsNullOrEmpty(this.BaseUri)
                && !string.IsNullOrEmpty(this.Layers);
        }
    }

    public class CustomWmsTileSource : WmsTileSource
    {
        public CustomWmsTileSource(string baseUri, int tileWidth, int tileHeight) : base(baseUri, tileWidth, tileHeight)
        {
        }

        protected override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            var uri = base.GetTile(tileLevel, tilePositionX, tilePositionY);

        
            return new Uri(uri.ToString().Replace("PNG", "image/png"), UriKind.Absolute);
        }
    }

This approach uses the default code for the WmsTiledProvider and replaces its tile source with a custom one. The imporant code is within the GetTile method of the CustomWmsTileSource class, which allows for replacing the "PNG" in the created Uri. 

On a side note, we also have a feature request: Map: Expose an option to set the "FORMAT" parameter of the url used in the WmsTileSource to allow for an easier way to achieve this behavior. 

I hope you find this information helpful.

Regards,
Vladimir Stoyanov
Progress Telerik

Tags
Map
Asked by
MCanitez
Top achievements
Rank 1
Answers by
Vladimir Stoyanov
Telerik team
Share this question
or