2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2016-04-19 19:36:03 +02:00
|
|
|
|
// 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-04-05 19:13:58 +02:00
|
|
|
|
using System.Globalization;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Data.Xml.Dom;
|
2019-06-04 22:03:25 +02:00
|
|
|
|
using Windows.Foundation;
|
2016-04-19 19:36:03 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
2017-10-08 17:35:07 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2016-04-19 19:36:03 +02:00
|
|
|
|
#else
|
2017-08-04 21:38:58 +02:00
|
|
|
|
using System.Xml;
|
2017-10-08 17:35:07 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
2016-04-19 19:36:03 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
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),
|
2018-02-09 17:43:47 +01:00
|
|
|
|
new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2016-04-19 19:36:03 +02:00
|
|
|
|
public static readonly DependencyProperty LayersProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(Layers), typeof(string), typeof(WmsImageLayer),
|
2018-02-09 17:43:47 +01:00
|
|
|
|
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Styles), typeof(string), typeof(WmsImageLayer),
|
2018-02-09 17:43:47 +01:00
|
|
|
|
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty FormatProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Format), typeof(string), typeof(WmsImageLayer),
|
2018-02-09 17:43:47 +01:00
|
|
|
|
new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
public static readonly DependencyProperty CrsIdMapProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(CrsIdMap), typeof(string), typeof(WmsImageLayer),
|
|
|
|
|
|
new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).CrsIdMapPropertyChanged((string)e.NewValue)));
|
|
|
|
|
|
|
2018-06-09 00:11:44 +02:00
|
|
|
|
public Uri ServiceUri
|
2016-04-19 19:36:03 +02:00
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
get { return (Uri)GetValue(ServiceUriProperty); }
|
|
|
|
|
|
set { SetValue(ServiceUriProperty, value); }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 19:36:03 +02:00
|
|
|
|
public string Layers
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(LayersProperty); }
|
|
|
|
|
|
set { SetValue(LayersProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public string Styles
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(StylesProperty); }
|
|
|
|
|
|
set { SetValue(StylesProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Format
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(FormatProperty); }
|
|
|
|
|
|
set { SetValue(FormatProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
public string CrsIdMap
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(CrsIdMapProperty); }
|
|
|
|
|
|
set { SetValue(CrsIdMapProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> crsIdMap;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a list of all layer names returned by a GetCapabilities response.
|
|
|
|
|
|
/// </summary>
|
2017-08-04 21:38:58 +02:00
|
|
|
|
public async Task<IList<string>> GetLayerNamesAsync()
|
|
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
IList<string> layerNames = null;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2018-06-09 00:11:44 +02:00
|
|
|
|
if (ServiceUri != null)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2018-06-11 21:37:36 +02:00
|
|
|
|
var uri = GetRequestUri("GetCapabilities");
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2018-06-09 00:11:44 +02:00
|
|
|
|
try
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
var document = await XmlDocument.LoadFromUriAsync(new Uri(uri.Replace(" ", "%20")));
|
|
|
|
|
|
layerNames = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
var capability = ChildElements(document.DocumentElement, "Capability").FirstOrDefault();
|
|
|
|
|
|
if (capability != null)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
var rootLayer = ChildElements(capability, "Layer").FirstOrDefault();
|
|
|
|
|
|
if (rootLayer != null)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
foreach (var layer in ChildElements(rootLayer, "Layer"))
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
var name = ChildElements(layer, "Name").FirstOrDefault();
|
|
|
|
|
|
if (name != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
layerNames.Add(name.InnerText);
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-06-09 00:11:44 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return layerNames;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
protected override async Task<ImageSource> GetImageAsync()
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
var uri = GetImageUri();
|
2018-06-11 21:37:36 +02:00
|
|
|
|
|
2019-06-15 01:39:07 +02:00
|
|
|
|
return uri != null ? await ImageLoader.LoadImageAsync(uri) : null;
|
2019-04-05 19:13:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a GetMap request URL for the current BoundingBox.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected virtual Uri GetImageUri()
|
|
|
|
|
|
{
|
|
|
|
|
|
Uri imageUri = null;
|
|
|
|
|
|
var projection = ParentMap?.MapProjection;
|
|
|
|
|
|
|
|
|
|
|
|
if (ServiceUri != null && projection != null && !string.IsNullOrEmpty(projection.CrsId))
|
2018-06-09 00:11:44 +02:00
|
|
|
|
{
|
2019-04-05 19:13:58 +02:00
|
|
|
|
var 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 22:47:22 +02:00
|
|
|
|
var crs = GetCrsValue();
|
2019-04-05 19:13:58 +02:00
|
|
|
|
var rect = projection.BoundingBoxToRect(BoundingBox);
|
|
|
|
|
|
|
2019-04-05 22:47:22 +02:00
|
|
|
|
uri += "&" + GetBboxParameters(crs, rect);
|
2019-04-05 19:13:58 +02:00
|
|
|
|
uri += "&WIDTH=" + (int)Math.Round(projection.ViewportScale * rect.Width);
|
|
|
|
|
|
uri += "&HEIGHT=" + (int)Math.Round(projection.ViewportScale * rect.Height);
|
|
|
|
|
|
|
|
|
|
|
|
imageUri = new Uri(uri.Replace(" ", "%20"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return imageUri;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the effective value of the CRS query parameter.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected virtual string GetCrsValue()
|
|
|
|
|
|
{
|
|
|
|
|
|
var projection = ParentMap.MapProjection;
|
|
|
|
|
|
var crsId = projection.CrsId;
|
|
|
|
|
|
|
|
|
|
|
|
if (crsIdMap != null && !crsIdMap.TryGetValue(crsId, out crsId))
|
|
|
|
|
|
{
|
|
|
|
|
|
crsId = projection.CrsId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (crsId.StartsWith("AUTO2:") || crsId.StartsWith("AUTO:"))
|
|
|
|
|
|
{
|
|
|
|
|
|
crsId = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}",
|
|
|
|
|
|
crsId, projection.ProjectionCenter.Longitude, projection.ProjectionCenter.Latitude);
|
2018-06-09 00:11:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
return crsId;
|
2018-06-11 21:37:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-04-05 22:47:22 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a query substring for the projected bounding box, which contains the CRS and BBOX or equivalent parameters.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected virtual string GetBboxParameters(string crs, Rect bbox)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format(CultureInfo.InvariantCulture,
|
|
|
|
|
|
crs == "EPSG:4326" ? "CRS={0}&BBOX={2},{1},{4},{3}" : "CRS={0}&BBOX={1},{2},{3},{4}",
|
|
|
|
|
|
crs, bbox.X, bbox.Y, (bbox.X + bbox.Width), (bbox.Y + bbox.Height));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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();
|
2017-08-02 20:45:41 +02:00
|
|
|
|
|
|
|
|
|
|
if (!uri.EndsWith("?") && !uri.EndsWith("&"))
|
|
|
|
|
|
{
|
2018-06-09 00:11:44 +02:00
|
|
|
|
uri += !uri.Contains("?") ? "?" : "&";
|
2017-08-02 20:45:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2018-06-11 21:37:36 +02:00
|
|
|
|
return uri + "REQUEST=" + request;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2019-04-05 19:13:58 +02:00
|
|
|
|
private Task CrsIdMapPropertyChanged(string crsIdMapString)
|
|
|
|
|
|
{
|
|
|
|
|
|
crsIdMap = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(crsIdMapString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var entries = crsIdMapString.Split(new char[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
|
|
if (entries.Length >= 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
crsIdMap = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < entries.Length - 1; i += 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
crsIdMap[entries[i]] = entries[i + 1];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return UpdateImageAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
private static IEnumerable<XmlElement> ChildElements(XmlElement element, string name)
|
|
|
|
|
|
{
|
2019-06-04 22:03:25 +02:00
|
|
|
|
return element.ChildNodes.OfType<XmlElement>().Where(e => (string)e.LocalName == name);
|
2016-04-19 19:36:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|