XAML-Map-Control/MapControl/Shared/WmsImageLayer.cs

199 lines
6.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2020 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2017-08-04 21:38:58 +02:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
2019-11-07 22:34:25 +01:00
using System.Xml.Linq;
2017-08-04 21:38:58 +02:00
#if WINDOWS_UWP
using Windows.Foundation;
using Windows.UI.Xaml;
2017-10-08 17:35:07 +02:00
using Windows.UI.Xaml.Media;
#else
2017-10-08 17:35:07 +02:00
using System.Windows;
using System.Windows.Media;
#endif
namespace MapControl
{
2020-04-19 12:47:39 +02:00
/// <summary>
/// Displays a single map image from a Web Map Service (WMS).
/// </summary>
public partial class WmsImageLayer : MapImageLayer
{
2018-06-09 00:11:44 +02:00
public static readonly DependencyProperty ServiceUriProperty = DependencyProperty.Register(
nameof(ServiceUri), typeof(Uri), typeof(WmsImageLayer),
new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
public static readonly DependencyProperty LayersProperty = DependencyProperty.Register(
nameof(Layers), typeof(string), typeof(WmsImageLayer),
2020-04-19 12:47:39 +02:00
new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
public static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
nameof(Styles), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
public static readonly DependencyProperty FormatProperty = DependencyProperty.Register(
nameof(Format), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
2020-04-19 12:47:39 +02:00
/// <summary>
/// The base request URL.
/// </summary>
2018-06-09 00:11:44 +02:00
public Uri ServiceUri
{
2018-06-09 00:11:44 +02:00
get { return (Uri)GetValue(ServiceUriProperty); }
set { SetValue(ServiceUriProperty, value); }
}
2020-04-19 12:47:39 +02:00
/// <summary>
/// Comma-separated list of Layer names to be displayed. If not set, the first Layer is displayed.
/// </summary>
public string Layers
{
get { return (string)GetValue(LayersProperty); }
set { SetValue(LayersProperty, value); }
}
2020-04-19 12:47:39 +02:00
/// <summary>
/// Comma-separated list of requested styles. Default is an empty string.
/// </summary>
public string Styles
{
get { return (string)GetValue(StylesProperty); }
set { SetValue(StylesProperty, value); }
}
2020-04-19 12:47:39 +02:00
/// <summary>
/// Requested image format. Default is image/png.
/// </summary>
public string Format
{
get { return (string)GetValue(FormatProperty); }
set { SetValue(FormatProperty, value); }
}
/// <summary>
/// Gets a list of all layer names returned by a GetCapabilities response.
/// </summary>
2020-04-19 12:47:39 +02:00
public async Task<IEnumerable<string>> GetLayerNamesAsync()
2017-08-04 21:38:58 +02:00
{
2020-04-19 12:47:39 +02:00
IEnumerable<string> layerNames = null;
2017-08-04 21:38:58 +02:00
2020-04-26 12:20:20 +02:00
if (ServiceUri != null)
2017-08-04 21:38:58 +02:00
{
2020-04-26 12:20:20 +02:00
var uri = GetRequestUri("GetCapabilities").Replace(" ", "%20");
2017-08-04 21:38:58 +02:00
2020-04-26 12:20:20 +02:00
try
{
using (var stream = await ImageLoader.HttpClient.GetStreamAsync(uri))
{
var capabilities = XDocument.Load(stream).Root;
var ns = capabilities.Name.Namespace;
layerNames = capabilities
.Descendants(ns + "Layer")
.Select(e => e.Element(ns + "Name")?.Value)
.Where(n => !string.IsNullOrEmpty(n));
}
}
catch (Exception ex)
{
Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message);
}
2017-08-04 21:38:58 +02:00
}
return layerNames;
}
/// <summary>
/// Calls GetImageUri() and asynchronously loads an ImageSource from the returned GetMap URL.
/// </summary>
protected override async Task<ImageSource> GetImageAsync()
2017-08-04 21:38:58 +02:00
{
2020-04-26 12:20:20 +02:00
ImageSource image = null;
if (ServiceUri != null)
2020-04-19 12:47:39 +02:00
{
2020-04-26 12:20:20 +02:00
if (Layers == null &&
ServiceUri.ToString().IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0)
{
Layers = (await GetLayerNamesAsync())?.FirstOrDefault() ?? ""; // get first Layer from Capabilities
}
var uri = GetImageUri();
2020-04-19 12:47:39 +02:00
2020-04-26 12:20:20 +02:00
if (uri != null)
{
image = await ImageLoader.LoadImageAsync(new Uri(uri.Replace(" ", "%20")));
}
}
2018-06-11 21:37:36 +02:00
2020-04-26 12:20:20 +02:00
return image;
}
/// <summary>
/// Returns a GetMap request URL string.
/// </summary>
protected virtual string GetImageUri()
{
string uri = null;
var projection = ParentMap?.MapProjection;
2020-04-26 12:20:20 +02:00
if (projection != null && !string.IsNullOrEmpty(projection.CrsId))
2018-06-09 00:11:44 +02:00
{
uri = GetRequestUri("GetMap");
2018-06-11 21:37:36 +02:00
if (uri.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0 && Layers != null)
{
uri += "&LAYERS=" + Layers;
}
if (uri.IndexOf("STYLES=", StringComparison.OrdinalIgnoreCase) < 0 && Styles != null)
{
uri += "&STYLES=" + Styles;
}
if (uri.IndexOf("FORMAT=", StringComparison.OrdinalIgnoreCase) < 0 && Format != null)
{
uri += "&FORMAT=" + Format;
}
var rect = projection.BoundingBoxToRect(BoundingBox);
uri += "&CRS=" + projection.GetCrsValue();
uri += "&BBOX=" + projection.GetBboxValue(rect);
uri += "&WIDTH=" + (int)Math.Round(ParentMap.ViewTransform.Scale * rect.Width);
uri += "&HEIGHT=" + (int)Math.Round(ParentMap.ViewTransform.Scale * rect.Height);
}
return uri;
}
2018-06-11 21:37:36 +02:00
private string GetRequestUri(string request)
{
2018-06-09 00:11:44 +02:00
var uri = ServiceUri.ToString();
if (!uri.EndsWith("?") && !uri.EndsWith("&"))
{
2018-06-09 00:11:44 +02:00
uri += !uri.Contains("?") ? "?" : "&";
}
2018-06-09 00:11:44 +02:00
if (uri.IndexOf("SERVICE=", StringComparison.OrdinalIgnoreCase) < 0)
{
uri += "SERVICE=WMS&";
}
2018-06-11 21:37:36 +02:00
if (uri.IndexOf("VERSION=", StringComparison.OrdinalIgnoreCase) < 0)
2018-06-09 00:11:44 +02:00
{
2018-06-11 21:37:36 +02:00
uri += "VERSION=1.3.0&";
2018-06-09 00:11:44 +02:00
}
2018-06-11 21:37:36 +02:00
return uri + "REQUEST=" + request;
2017-08-04 21:38:58 +02:00
}
}
}