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

404 lines
14 KiB
C#
Raw Normal View History

2025-03-31 21:33:52 +02:00
using Microsoft.Extensions.Logging;
using System;
2017-08-04 21:38:58 +02:00
using System.Collections.Generic;
2024-09-08 14:03:55 +02:00
using System.Globalization;
2017-08-04 21:38:58 +02:00
using System.Linq;
using System.Threading.Tasks;
2019-11-07 22:34:25 +01:00
using System.Xml.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml;
2017-10-08 17:35:07 +02:00
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
2025-09-19 18:49:12 +02:00
using Avalonia.Interactivity;
2025-11-13 13:36:28 +01:00
using ImageSource = Avalonia.Media.IImage;
#endif
namespace MapControl
{
2020-04-19 12:47:39 +02:00
/// <summary>
/// Displays a single map image from a Web Map Service (WMS).
/// </summary>
2025-11-14 23:59:24 +01:00
public class WmsImageLayer : MapImageLayer
{
2025-03-31 21:33:52 +02:00
private static ILogger logger;
2025-09-19 18:49:12 +02:00
private static ILogger Logger => logger ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(WmsImageLayer));
2025-03-31 21:33:52 +02:00
2024-05-21 23:18:00 +02:00
public static readonly DependencyProperty ServiceUriProperty =
2024-05-23 18:22:52 +02:00
DependencyPropertyHelper.Register<WmsImageLayer, Uri>(nameof(ServiceUri), null,
async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
2025-09-19 18:49:12 +02:00
public static readonly DependencyProperty RequestStylesProperty =
DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(RequestStyles), "",
2024-05-21 23:18:00 +02:00
async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
2025-09-19 18:49:12 +02:00
public static readonly DependencyProperty RequestLayersProperty =
DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(RequestLayers), null,
async (layer, oldValue, newValue) => await layer.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
{
2022-08-06 10:40:59 +02:00
get => (Uri)GetValue(ServiceUriProperty);
set => SetValue(ServiceUriProperty, value);
}
2020-04-19 12:47:39 +02:00
/// <summary>
2024-05-23 18:22:52 +02:00
/// Comma-separated sequence of requested WMS Styles. Default is an empty string.
2020-04-19 12:47:39 +02:00
/// </summary>
2025-09-19 18:49:12 +02:00
public string RequestStyles
{
2025-09-19 18:49:12 +02:00
get => (string)GetValue(RequestStylesProperty);
set => SetValue(RequestStylesProperty, value);
}
2020-04-19 12:47:39 +02:00
/// <summary>
2025-09-19 18:49:12 +02:00
/// Comma-separated sequence of WMS Layer names to be displayed. If not set, the default Layer is displayed.
2020-04-19 12:47:39 +02:00
/// </summary>
2025-09-19 18:49:12 +02:00
public string RequestLayers
{
2025-09-19 18:49:12 +02:00
get => (string)GetValue(RequestLayersProperty);
set => SetValue(RequestLayersProperty, value);
}
/// <summary>
2025-09-19 18:49:12 +02:00
/// Gets a collection of all Layer names available in a WMS.
2020-07-15 16:40:33 +02:00
/// </summary>
2025-09-19 18:49:12 +02:00
public IReadOnlyCollection<string> AvailableLayers { get; private set; }
2020-07-15 16:40:33 +02:00
2025-09-22 09:17:26 +02:00
private bool HasLayer =>
RequestLayers != null ||
AvailableLayers?.Count > 0 ||
ServiceUri.Query?.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) > 0;
public WmsImageLayer()
{
Loaded += OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
Loaded -= OnLoaded;
if (ServiceUri != null && !HasLayer)
{
await InitializeAsync();
if (AvailableLayers != null && AvailableLayers.Count > 0)
{
await UpdateImageAsync();
}
}
}
/// <summary>
/// Initializes the AvailableLayers and SupportedCrsIds properties.
/// Calling this method is only necessary when no layer name is known in advance.
/// It is called internally in a Loaded event handler when the RequestLayers and AvailableLayers
/// properties are null and the ServiceUri.Query part does not contain a LAYERS parameter.
/// </summary>
public async Task InitializeAsync()
{
var capabilities = await GetCapabilitiesAsync();
if (capabilities != null)
{
var ns = capabilities.Name.Namespace;
var capability = capabilities.Element(ns + "Capability");
2025-10-29 22:20:54 +01:00
SupportedCrsIds = capability
2025-09-22 09:17:26 +02:00
.Descendants(ns + "Layer")
.Descendants(ns + "CRS")
.Select(e => e.Value)
.ToList();
2025-10-29 22:20:54 +01:00
AvailableLayers = capability
2025-09-22 09:17:26 +02:00
.Descendants(ns + "Layer")
.Select(e => e.Element(ns + "Name")?.Value)
.Where(n => !string.IsNullOrEmpty(n))
.ToList();
}
}
2020-07-15 16:40:33 +02:00
/// <summary>
/// Loads an XElement from the URL returned by GetCapabilitiesRequestUri().
/// </summary>
2020-07-13 17:48:06 +02:00
public async Task<XElement> GetCapabilitiesAsync()
2017-08-04 21:38:58 +02:00
{
2020-07-13 17:48:06 +02:00
XElement element = 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-07-13 17:48:06 +02:00
var uri = GetCapabilitiesRequestUri();
2017-08-04 21:38:58 +02:00
2025-09-22 09:17:26 +02:00
if (uri != null)
2020-04-26 12:20:20 +02:00
{
2020-09-09 22:45:36 +02:00
try
2020-04-26 12:20:20 +02:00
{
2025-09-14 21:02:21 +02:00
using var stream = await ImageLoader.HttpClient.GetStreamAsync(uri);
2025-09-19 11:53:25 +02:00
element = await XDocument.LoadRootElementAsync(stream);
2020-09-09 22:45:36 +02:00
}
catch (Exception ex)
{
2025-04-01 16:11:53 +02:00
Logger?.LogError(ex, "Failed reading capabilities from {uri}", uri);
2020-07-13 17:48:06 +02:00
}
}
}
return element;
}
/// <summary>
2020-09-09 22:45:36 +02:00
/// Gets a response string from the URL returned by GetFeatureInfoRequestUri().
2020-07-13 17:48:06 +02:00
/// </summary>
2022-11-30 17:59:38 +01:00
public async Task<string> GetFeatureInfoAsync(Point position, string format = "text/plain")
2020-07-13 17:48:06 +02:00
{
2020-09-09 22:45:36 +02:00
string response = null;
2020-07-13 17:48:06 +02:00
2022-11-30 17:59:38 +01:00
if (ServiceUri != null &&
2025-09-22 09:17:26 +02:00
HasLayer &&
2022-11-30 17:59:38 +01:00
ParentMap?.MapProjection != null &&
ParentMap.ActualWidth > 0d &&
ParentMap.ActualHeight > 0d)
2020-07-13 17:48:06 +02:00
{
2024-09-08 15:56:48 +02:00
var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(0d, 0d, ParentMap.ActualWidth, ParentMap.ActualHeight));
var uri = GetFeatureInfoRequestUri(boundingBox, position, format);
2020-04-26 12:20:20 +02:00
2025-09-22 09:17:26 +02:00
if (uri != null)
2020-07-13 17:48:06 +02:00
{
2020-09-09 22:45:36 +02:00
try
2020-07-13 17:48:06 +02:00
{
2020-09-09 22:45:36 +02:00
response = await ImageLoader.HttpClient.GetStringAsync(uri);
}
catch (Exception ex)
{
2025-04-01 16:11:53 +02:00
Logger?.LogError(ex, "Failed reading feature info from {uri}", uri);
2020-04-26 12:20:20 +02:00
}
}
2017-08-04 21:38:58 +02:00
}
2020-09-09 22:45:36 +02:00
return response;
2020-07-13 17:48:06 +02:00
}
/// <summary>
2020-07-15 16:40:33 +02:00
/// Loads an ImageSource from the URL returned by GetMapRequestUri().
/// </summary>
2025-08-21 22:40:51 +02:00
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress)
2017-08-04 21:38:58 +02:00
{
2020-04-26 12:20:20 +02:00
ImageSource image = null;
2025-09-22 09:17:26 +02:00
if (ServiceUri != null && HasLayer)
2020-04-19 12:47:39 +02:00
{
2025-09-19 18:49:12 +02:00
if (boundingBox.West >= -180d && boundingBox.East <= 180d ||
ParentMap.MapProjection.Type > MapProjectionType.NormalCylindrical)
2020-04-26 12:20:20 +02:00
{
2025-09-19 18:49:12 +02:00
var uri = GetMapRequestUri(boundingBox);
if (uri != null)
2022-11-30 17:59:38 +01:00
{
2025-09-22 09:17:26 +02:00
try
{
image = await ImageLoader.LoadImageAsync(new Uri(uri), progress);
}
catch (Exception ex)
{
Logger?.LogError(ex, "ImageLoader.LoadImageAsync");
}
2022-11-30 17:59:38 +01:00
}
2025-09-19 18:49:12 +02:00
}
else
{
BoundingBox bbox1, bbox2;
2022-11-30 17:59:38 +01:00
2025-09-19 18:49:12 +02:00
if (boundingBox.West < -180d)
2022-11-30 17:59:38 +01:00
{
2025-09-19 18:49:12 +02:00
bbox1 = new BoundingBox(boundingBox.South, boundingBox.West + 360, boundingBox.North, 180d);
bbox2 = new BoundingBox(boundingBox.South, -180d, boundingBox.North, boundingBox.East);
2022-11-30 17:59:38 +01:00
}
else
{
2025-09-19 18:49:12 +02:00
bbox1 = new BoundingBox(boundingBox.South, boundingBox.West, boundingBox.North, 180d);
bbox2 = new BoundingBox(boundingBox.South, -180d, boundingBox.North, boundingBox.East - 360d);
}
var uri1 = GetMapRequestUri(bbox1);
var uri2 = GetMapRequestUri(bbox2);
if (uri1 != null && uri2 != null)
{
2025-09-22 09:17:26 +02:00
try
{
image = await ImageLoader.LoadMergedImageAsync(new Uri(uri1), new Uri(uri2), progress);
}
catch (Exception ex)
{
Logger?.LogError(ex, "ImageLoader.LoadMergedImageAsync");
}
2022-11-30 17:59:38 +01:00
}
2020-04-26 12:20:20 +02:00
}
}
2018-06-11 21:37:36 +02:00
2020-04-26 12:20:20 +02:00
return image;
}
2020-07-13 17:48:06 +02:00
/// <summary>
/// Returns a GetCapabilities request URL string.
/// </summary>
protected virtual string GetCapabilitiesRequestUri()
{
2022-08-22 21:13:45 +02:00
return GetRequestUri(new Dictionary<string, string>
{
{ "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetCapabilities" }
});
2020-07-13 17:48:06 +02:00
}
/// <summary>
/// Returns a GetMap request URL string.
/// </summary>
2022-11-30 17:59:38 +01:00
protected virtual string GetMapRequestUri(BoundingBox boundingBox)
{
2024-09-08 15:56:48 +02:00
string uri = null;
2024-09-09 16:44:45 +02:00
var bbox = ParentMap.MapProjection.BoundingBoxToMap(boundingBox);
2022-12-14 18:02:19 +01:00
2024-09-09 16:44:45 +02:00
if (bbox.HasValue)
2022-12-14 18:02:19 +01:00
{
2024-09-09 16:44:45 +02:00
var width = ParentMap.ViewTransform.Scale * bbox.Value.Width;
var height = ParentMap.ViewTransform.Scale * bbox.Value.Height;
2022-12-14 18:02:19 +01:00
2024-09-08 15:56:48 +02:00
uri = GetRequestUri(new Dictionary<string, string>
{
{ "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetMap" },
2025-09-22 09:17:26 +02:00
{ "LAYERS", RequestLayers ?? AvailableLayers?.FirstOrDefault() ?? "" },
2025-09-19 18:49:12 +02:00
{ "STYLES", RequestStyles ?? "" },
2024-09-08 15:56:48 +02:00
{ "FORMAT", "image/png" },
{ "CRS", GetCrsValue() },
2024-09-09 16:44:45 +02:00
{ "BBOX", GetBboxValue(boundingBox, bbox.Value) },
2024-09-08 15:56:48 +02:00
{ "WIDTH", Math.Round(width).ToString("F0") },
{ "HEIGHT", Math.Round(height).ToString("F0") }
});
}
2022-08-22 21:13:45 +02:00
2024-09-08 15:56:48 +02:00
return uri;
2020-07-13 17:48:06 +02:00
}
/// <summary>
/// Returns a GetFeatureInfo request URL string.
/// </summary>
2024-09-08 15:56:48 +02:00
protected virtual string GetFeatureInfoRequestUri(BoundingBox boundingBox, Point position, string format)
2020-07-13 17:48:06 +02:00
{
2024-09-08 15:56:48 +02:00
string uri = null;
2024-09-09 16:44:45 +02:00
var bbox = ParentMap.MapProjection.BoundingBoxToMap(boundingBox);
2022-12-14 18:02:19 +01:00
2024-09-09 16:44:45 +02:00
if (bbox.HasValue)
2022-12-14 18:02:19 +01:00
{
2024-09-09 16:44:45 +02:00
var width = ParentMap.ViewTransform.Scale * bbox.Value.Width;
var height = ParentMap.ViewTransform.Scale * bbox.Value.Height;
2022-12-14 18:02:19 +01:00
2024-09-08 15:56:48 +02:00
var transform = ViewTransform.CreateTransformMatrix(
-ParentMap.ActualWidth / 2d, -ParentMap.ActualWidth / 2d,
-ParentMap.ViewTransform.Rotation,
width / 2d, height / 2d);
2024-05-22 09:42:21 +02:00
2024-09-08 15:56:48 +02:00
var imagePos = transform.Transform(position);
2024-05-22 09:42:21 +02:00
2024-09-08 15:56:48 +02:00
var queryParameters = new Dictionary<string, string>
{
{ "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetFeatureInfo" },
2025-09-22 09:17:26 +02:00
{ "LAYERS", RequestLayers ?? AvailableLayers?.FirstOrDefault() ?? "" },
2025-09-19 18:49:12 +02:00
{ "STYLES", RequestStyles ?? "" },
2024-09-08 15:56:48 +02:00
{ "FORMAT", "image/png" },
{ "INFO_FORMAT", format },
{ "CRS", GetCrsValue() },
2024-09-09 16:44:45 +02:00
{ "BBOX", GetBboxValue(boundingBox, bbox.Value) },
2024-09-08 15:56:48 +02:00
{ "WIDTH", Math.Round(width).ToString("F0") },
{ "HEIGHT", Math.Round(height).ToString("F0") },
{ "I", Math.Round(imagePos.X).ToString("F0") },
{ "J", Math.Round(imagePos.Y).ToString("F0") }
};
// GetRequestUri may modify queryParameters["LAYERS"]
//
uri = GetRequestUri(queryParameters) + "&QUERY_LAYERS=" + queryParameters["LAYERS"];
}
2020-07-13 17:48:06 +02:00
2024-09-08 15:56:48 +02:00
return uri;
}
2022-11-28 17:29:32 +01:00
protected virtual string GetCrsValue()
2020-09-10 20:04:19 +02:00
{
2024-09-08 14:03:55 +02:00
var projection = ParentMap.MapProjection;
2025-09-19 18:49:12 +02:00
var crs = projection.CrsId;
2024-09-08 14:03:55 +02:00
2025-09-19 18:49:12 +02:00
if (crs.StartsWith("AUTO2:") || crs.StartsWith("AUTO:"))
2024-09-08 14:03:55 +02:00
{
2025-09-19 18:49:12 +02:00
crs = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", crs, projection.Center.Longitude, projection.Center.Latitude);
2024-09-08 14:03:55 +02:00
}
2025-09-19 18:49:12 +02:00
return crs;
2020-09-10 20:04:19 +02:00
}
2024-09-09 16:44:45 +02:00
protected virtual string GetBboxValue(BoundingBox boundingBox, Rect mapBoundingBox)
2020-09-10 20:04:19 +02:00
{
2025-09-19 18:49:12 +02:00
var crs = ParentMap.MapProjection.CrsId;
2024-09-08 15:56:48 +02:00
string format;
double x1, y1, x2, y2;
2024-09-08 14:03:55 +02:00
2025-09-19 18:49:12 +02:00
if (crs == "CRS:84" || crs == "EPSG:4326")
2024-09-08 14:03:55 +02:00
{
2025-09-19 18:49:12 +02:00
format = crs == "CRS:84" ? "{0:F8},{1:F8},{2:F8},{3:F8}" : "{1:F8},{0:F8},{3:F8},{2:F8}";
2024-09-08 15:56:48 +02:00
x1 = boundingBox.West;
y1 = boundingBox.South;
x2 = boundingBox.East;
y2 = boundingBox.North;
}
else
{
format = "{0:F2},{1:F2},{2:F2},{3:F2}";
2024-09-09 16:44:45 +02:00
x1 = mapBoundingBox.X;
y1 = mapBoundingBox.Y;
x2 = mapBoundingBox.X + mapBoundingBox.Width;
y2 = mapBoundingBox.Y + mapBoundingBox.Height;
2024-09-08 14:03:55 +02:00
}
return string.Format(CultureInfo.InvariantCulture, format, x1, y1, x2, y2);
2020-09-10 20:04:19 +02:00
}
2022-08-22 21:13:45 +02:00
protected string GetRequestUri(IDictionary<string, string> queryParameters)
2018-06-11 21:37:36 +02:00
{
2022-08-22 21:13:45 +02:00
var query = ServiceUri.Query;
2022-08-22 21:13:45 +02:00
if (!string.IsNullOrEmpty(query))
{
2025-09-22 09:17:26 +02:00
// Parameters from ServiceUri.Query take higher precedence than queryParameters.
//
2022-08-22 21:13:45 +02:00
foreach (var param in query.Substring(1).Split('&'))
{
var pair = param.Split('=');
queryParameters[pair[0].ToUpper()] = pair.Length > 1 ? pair[1] : "";
}
}
2022-08-22 21:13:45 +02:00
var uri = ServiceUri.GetLeftPart(UriPartial.Path) + "?"
+ string.Join("&", queryParameters.Select(kv => kv.Key + "=" + kv.Value));
2018-06-09 00:11:44 +02:00
2022-08-22 21:13:45 +02:00
return uri.Replace(" ", "%20");
2017-08-04 21:38:58 +02:00
}
}
}