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

366 lines
12 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 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;
2024-05-21 23:18:00 +02:00
#if AVALONIA
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using DependencyProperty = Avalonia.AvaloniaProperty;
using FrameworkElement = Avalonia.Controls.Control;
using ImageSource = Avalonia.Media.IImage;
#elif WINUI
using Windows.Foundation;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2021-11-17 23:17:11 +01:00
#elif 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
{
2024-05-21 23:18:00 +02:00
public static readonly DependencyProperty ServiceUriProperty =
DependencyPropertyHelper.Register<WmsImageLayer, Uri>(nameof(ServiceUri), null, false,
async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
public static readonly DependencyProperty LayersProperty =
DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(Layers), null, false,
async (layer, oldValue, newValue) =>
{
2022-11-30 22:18:45 +01:00
// Ignore property change from GetImageAsync, when Layers was null.
//
2024-05-21 23:18:00 +02:00
if (oldValue != null)
{
2024-05-21 23:18:00 +02:00
await layer.UpdateImageAsync();
}
2024-05-21 23:18:00 +02:00
});
2024-05-21 23:18:00 +02:00
public static readonly DependencyProperty StylesProperty =
DependencyPropertyHelper.Register<WmsImageLayer, string>(nameof(Styles), string.Empty, false,
async (layer, oldValue, newValue) => await layer.UpdateImageAsync());
2020-07-15 16:40:33 +02:00
public WmsImageLayer()
{
foreach (FrameworkElement child in Children)
{
child.UseLayoutRounding = true;
}
}
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-04-11 15:59:07 +02:00
/// Comma-separated sequence of Layer names to be displayed. If not set, the first Layer is displayed.
2020-04-19 12:47:39 +02:00
/// </summary>
public string Layers
{
2022-08-06 10:40:59 +02:00
get => (string)GetValue(LayersProperty);
set => SetValue(LayersProperty, value);
}
2020-04-19 12:47:39 +02:00
/// <summary>
2024-04-11 15:59:07 +02:00
/// Comma-separated sequence of requested styles. Default is an empty string.
2020-04-19 12:47:39 +02:00
/// </summary>
2024-05-21 23:20:42 +02:00
public string Styles
{
2022-08-06 10:40:59 +02:00
get => (string)GetValue(StylesProperty);
set => SetValue(StylesProperty, value);
}
/// <summary>
2020-07-15 16:40:33 +02:00
/// Gets a list of all layer names returned by a GetCapabilities response.
/// </summary>
public async Task<IEnumerable<string>> GetLayerNamesAsync()
{
IEnumerable<string> layerNames = null;
var capabilities = await GetCapabilitiesAsync();
if (capabilities != null)
{
var ns = capabilities.Name.Namespace;
layerNames = capabilities
.Descendants(ns + "Layer")
.Select(e => e.Element(ns + "Name")?.Value)
.Where(n => !string.IsNullOrEmpty(n));
}
return layerNames;
}
/// <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
2020-09-09 22:45:36 +02:00
if (!string.IsNullOrEmpty(uri))
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
{
2020-09-09 22:45:36 +02:00
using (var stream = await ImageLoader.HttpClient.GetStreamAsync(uri))
{
element = XDocument.Load(stream).Root;
}
}
catch (Exception ex)
{
Debug.WriteLine($"WmsImageLayer: {uri}: {ex.Message}");
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 &&
ParentMap?.MapProjection != null &&
ParentMap.RenderSize.Width > 0d &&
ParentMap.RenderSize.Height > 0d)
2020-07-13 17:48:06 +02:00
{
2020-09-09 22:45:36 +02:00
var uri = GetFeatureInfoRequestUri(position, format);
2020-04-26 12:20:20 +02:00
2020-09-09 22:45:36 +02:00
if (!string.IsNullOrEmpty(uri))
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)
{
Debug.WriteLine($"WmsImageLayer: {uri}: {ex.Message}");
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>
2022-11-30 17:59:38 +01: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;
2022-11-30 17:59:38 +01:00
if (ServiceUri != null && ParentMap?.MapProjection != 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)
{
2022-11-30 22:18:45 +01:00
// Get first Layer from a GetCapabilities response.
//
Layers = (await GetLayerNamesAsync())?.FirstOrDefault() ?? "";
2020-04-26 12:20:20 +02:00
}
2022-11-30 17:59:38 +01:00
if (boundingBox.West >= -180d && boundingBox.East <= 180d ||
ParentMap.MapProjection.Type > MapProjectionType.NormalCylindrical)
{
var uri = CreateUri(GetMapRequestUri(boundingBox));
2020-04-19 12:47:39 +02:00
2022-11-30 17:59:38 +01:00
if (uri != null)
{
image = await ImageLoader.LoadImageAsync(uri, progress);
}
}
else
2020-04-26 12:20:20 +02:00
{
2022-11-30 17:59:38 +01:00
BoundingBox bbox1, bbox2;
if (boundingBox.West < -180d)
{
bbox1 = new BoundingBox(boundingBox.South, boundingBox.West + 360, boundingBox.North, 180d);
bbox2 = new BoundingBox(boundingBox.South, -180d, boundingBox.North, boundingBox.East);
}
else
{
bbox1 = new BoundingBox(boundingBox.South, boundingBox.West, boundingBox.North, 180d);
bbox2 = new BoundingBox(boundingBox.South, -180d, boundingBox.North, boundingBox.East - 360d);
}
var uri1 = CreateUri(GetMapRequestUri(bbox1));
var uri2 = CreateUri(GetMapRequestUri(bbox2));
if (uri1 != null && uri2 != null)
{
image = await ImageLoader.LoadMergedImageAsync(uri1, uri2, progress);
}
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)
{
var rect = ParentMap.MapProjection.BoundingBoxToMap(boundingBox);
2022-12-14 18:02:19 +01:00
if (!rect.HasValue)
2022-12-14 18:02:19 +01:00
{
return null;
}
2022-08-22 21:13:45 +02:00
var viewScale = ParentMap.ViewTransform.Scale;
return GetRequestUri(new Dictionary<string, string>
{
{ "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetMap" },
{ "LAYERS", Layers ?? "" },
{ "STYLES", Styles ?? "" },
{ "FORMAT", "image/png" },
2022-11-28 17:29:32 +01:00
{ "CRS", GetCrsValue() },
{ "BBOX", GetBboxValue(rect.Value) },
{ "WIDTH", Math.Round(viewScale * rect.Value.Width).ToString("F0") },
{ "HEIGHT", Math.Round(viewScale * rect.Value.Height).ToString("F0") }
2022-08-22 21:13:45 +02:00
});
2020-07-13 17:48:06 +02:00
}
/// <summary>
/// Returns a GetFeatureInfo request URL string.
/// </summary>
2020-07-16 16:32:22 +02:00
protected virtual string GetFeatureInfoRequestUri(Point position, string format)
2020-07-13 17:48:06 +02:00
{
2022-08-22 21:13:45 +02:00
var viewSize = ParentMap.RenderSize;
2022-11-30 17:59:38 +01:00
var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(0d, 0d, viewSize.Width, viewSize.Height));
var rect = ParentMap.MapProjection.BoundingBoxToMap(boundingBox);
2022-12-14 18:02:19 +01:00
if (!rect.HasValue)
2022-12-14 18:02:19 +01:00
{
return null;
}
var viewRect = GetViewRect(rect.Value);
2024-05-21 23:18:00 +02:00
#if AVALONIA
var transform
= Matrix.CreateTranslation(-viewSize.Width / 2d, -viewSize.Height / 2d)
* Matrix.CreateRotation(-viewRect.Rotation * Math.PI / 180d)
* Matrix.CreateTranslation(viewRect.Rect.Width / 2d, viewRect.Rect.Height / 2d);
#else
var transform = new Matrix(1d, 0d, 0d, 1d, -viewSize.Width / 2d, -viewSize.Height / 2d);
2022-08-22 21:13:45 +02:00
transform.Rotate(-viewRect.Rotation);
transform.Translate(viewRect.Rect.Width / 2d, viewRect.Rect.Height / 2d);
2024-05-21 23:18:00 +02:00
#endif
2022-08-22 21:13:45 +02:00
var imagePos = transform.Transform(position);
2020-07-13 17:48:06 +02:00
2022-08-22 21:13:45 +02:00
var queryParameters = new Dictionary<string, string>
{
{ "SERVICE", "WMS" },
{ "VERSION", "1.3.0" },
{ "REQUEST", "GetFeatureInfo" },
{ "LAYERS", Layers ?? "" },
{ "STYLES", Styles ?? "" },
{ "FORMAT", "image/png" },
{ "INFO_FORMAT", format },
2022-11-28 17:29:32 +01:00
{ "CRS", GetCrsValue() },
{ "BBOX", GetBboxValue(rect.Value) },
{ "WIDTH", Math.Round(viewRect.Rect.Width).ToString("F0") },
{ "HEIGHT", Math.Round(viewRect.Rect.Height).ToString("F0") },
2022-08-22 21:13:45 +02:00
{ "I", Math.Round(imagePos.X).ToString("F0") },
{ "J", Math.Round(imagePos.Y).ToString("F0") }
};
return GetRequestUri(queryParameters) + "&QUERY_LAYERS=" + queryParameters["LAYERS"];
}
2022-11-28 17:29:32 +01:00
protected virtual string GetCrsValue()
2020-09-10 20:04:19 +02:00
{
2022-11-28 17:29:32 +01:00
return ParentMap.MapProjection.GetCrsValue();
2020-09-10 20:04:19 +02:00
}
protected virtual string GetBboxValue(Rect rect)
2020-09-10 20:04:19 +02:00
{
return ParentMap.MapProjection.GetBboxValue(rect);
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))
{
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
}
2022-11-30 17:59:38 +01:00
private static Uri CreateUri(string uri)
{
if (!string.IsNullOrEmpty(uri))
{
try
{
return new Uri(uri, UriKind.RelativeOrAbsolute);
}
catch (Exception ex)
{
Debug.WriteLine($"WmsImageLayer: {uri}: {ex.Message}");
}
}
return null;
}
}
}