2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 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;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Data.Xml.Dom;
|
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
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public static readonly DependencyProperty ServerUriProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(ServerUri), typeof(Uri), typeof(WmsImageLayer),
|
2017-10-27 17:15:18 +02:00
|
|
|
|
new PropertyMetadata(null, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty VersionProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Version), typeof(string), typeof(WmsImageLayer),
|
2017-10-27 17:15:18 +02:00
|
|
|
|
new PropertyMetadata("1.3.0", async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
|
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),
|
2017-10-27 17:15:18 +02:00
|
|
|
|
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Styles), typeof(string), typeof(WmsImageLayer),
|
2017-10-27 17:15:18 +02:00
|
|
|
|
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty FormatProperty = DependencyProperty.Register(
|
|
|
|
|
|
nameof(Format), typeof(string), typeof(WmsImageLayer),
|
2017-10-27 17:15:18 +02:00
|
|
|
|
new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty TransparentProperty = DependencyProperty.Register(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
nameof(Transparent), typeof(bool), typeof(WmsImageLayer),
|
2017-10-27 17:15:18 +02:00
|
|
|
|
new PropertyMetadata(false, async (o, e) => await ((WmsImageLayer)o).UpdateImage()));
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
private string layers = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
public Uri ServerUri
|
2016-04-19 19:36:03 +02:00
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
get { return (Uri)GetValue(ServerUriProperty); }
|
|
|
|
|
|
set { SetValue(ServerUriProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Version
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (string)GetValue(VersionProperty); }
|
|
|
|
|
|
set { SetValue(VersionProperty, value); }
|
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); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-04-19 19:36:03 +02:00
|
|
|
|
public bool Transparent
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (bool)GetValue(TransparentProperty); }
|
|
|
|
|
|
set { SetValue(TransparentProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
protected override async Task<ImageSource> GetImage(BoundingBox boundingBox)
|
2016-04-19 19:36:03 +02:00
|
|
|
|
{
|
2017-10-27 17:15:18 +02:00
|
|
|
|
ImageSource imageSource = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (ServerUri != null)
|
2016-04-19 19:36:03 +02:00
|
|
|
|
{
|
2017-10-27 17:15:18 +02:00
|
|
|
|
var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox, Version);
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(projectionParameters))
|
|
|
|
|
|
{
|
|
|
|
|
|
var uri = GetRequestUri("GetMap"
|
|
|
|
|
|
+ "&LAYERS=" + Layers + "&STYLES=" + Styles + "&FORMAT=" + Format
|
|
|
|
|
|
+ "&TRANSPARENT=" + (Transparent ? "TRUE" : "FALSE") + "&" + projectionParameters);
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSource = await ImageLoader.LoadImageAsync(uri, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-04-19 19:36:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-27 17:15:18 +02:00
|
|
|
|
return imageSource;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IList<string>> GetLayerNamesAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ServerUri == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var layerNames = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var document = await XmlDocument.LoadFromUriAsync(GetRequestUri("GetCapabilities"));
|
|
|
|
|
|
|
|
|
|
|
|
var capability = ChildElements(document.DocumentElement, "Capability").FirstOrDefault();
|
|
|
|
|
|
if (capability != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var rootLayer = ChildElements(capability, "Layer").FirstOrDefault();
|
|
|
|
|
|
if (rootLayer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var layer in ChildElements(rootLayer, "Layer"))
|
|
|
|
|
|
{
|
|
|
|
|
|
var name = ChildElements(layer, "Name").FirstOrDefault();
|
|
|
|
|
|
if (name != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
layerNames.Add(name.InnerText);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("WmsImageLayer: {0}: {1}", ServerUri, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return layerNames;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Uri GetRequestUri(string query)
|
|
|
|
|
|
{
|
2017-08-02 20:45:41 +02:00
|
|
|
|
var uri = ServerUri.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (!uri.EndsWith("?") && !uri.EndsWith("&"))
|
|
|
|
|
|
{
|
|
|
|
|
|
uri += "?";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
uri += "SERVICE=WMS&VERSION=" + Version + "&REQUEST=" + query;
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
return new Uri(uri.Replace(" ", "%20"));
|
|
|
|
|
|
}
|
2016-04-19 19:36:03 +02:00
|
|
|
|
|
2017-08-04 21:38:58 +02:00
|
|
|
|
private static IEnumerable<XmlElement> ChildElements(XmlElement element, string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return element.ChildNodes.OfType<XmlElement>().Where(e => (string)e.LocalName == name);
|
2016-04-19 19:36:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|