Reworked WmsImageLayer

This commit is contained in:
ClemensF 2018-06-11 21:37:36 +02:00
parent 8048eb123f
commit de6a586bae
4 changed files with 52 additions and 95 deletions

View file

@ -60,7 +60,7 @@ namespace MapControl
base.SetViewportTransform(projectionCenter, mapCenter, viewportCenter, zoomLevel, heading);
}
public override string WmsQueryParameters(BoundingBox boundingBox, bool useSrs)
public override string WmsQueryParameters(BoundingBox boundingBox)
{
if (string.IsNullOrEmpty(CrsId))
{
@ -72,9 +72,8 @@ namespace MapControl
var height = (int)Math.Round(ViewportScale * rect.Height);
return string.Format(CultureInfo.InvariantCulture,
"{0}={1},1,{2},{3}&BBOX={4},{5},{6},{7}&WIDTH={8}&HEIGHT={9}",
(useSrs ? "SRS" : "CRS"), CrsId,
ProjectionCenter.Longitude, ProjectionCenter.Latitude,
"CRS={0},1,{1},{2}&BBOX={3},{4},{5},{6}&WIDTH={7}&HEIGHT={8}",
CrsId, ProjectionCenter.Longitude, ProjectionCenter.Latitude,
rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height), width, height);
}

View file

@ -149,31 +149,19 @@ namespace MapControl
/// <summary>
/// Gets a WMS query parameter string from the specified bounding box, e.g. "CRS=...&BBOX=...&WIDTH=...&HEIGHT=..."
/// </summary>
public virtual string WmsQueryParameters(BoundingBox boundingBox, bool useSrs = false)
public virtual string WmsQueryParameters(BoundingBox boundingBox)
{
if (string.IsNullOrEmpty(CrsId) || !boundingBox.HasValidBounds)
{
return null;
}
string format;
if (useSrs)
{
format = "SRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}";
}
else if (CrsId == "EPSG:4326")
{
format = "CRS={0}&BBOX={2},{1},{4},{3}&WIDTH={5}&HEIGHT={6}";
}
else
{
format = "CRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}";
}
var rect = BoundingBoxToRect(boundingBox);
var width = (int)Math.Round(ViewportScale * rect.Width);
var height = (int)Math.Round(ViewportScale * rect.Height);
var format = CrsId == "EPSG:4326"
? "CRS={0}&BBOX={2},{1},{4},{3}&WIDTH={5}&HEIGHT={6}"
: "CRS={0}&BBOX={1},{2},{3},{4}&WIDTH={5}&HEIGHT={6}";
return string.Format(CultureInfo.InvariantCulture, format, CrsId,
rect.X, rect.Y, (rect.X + rect.Width), (rect.Y + rect.Height), width, height);

View file

@ -21,16 +21,10 @@ namespace MapControl
{
public partial class WmsImageLayer : MapImageLayer
{
private const string DefaultVersion = "1.3.0";
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 VersionProperty = DependencyProperty.Register(
nameof(Version), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata(DefaultVersion, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
public static readonly DependencyProperty LayersProperty = DependencyProperty.Register(
nameof(Layers), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata(string.Empty, async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
@ -43,20 +37,12 @@ namespace MapControl
nameof(Format), typeof(string), typeof(WmsImageLayer),
new PropertyMetadata("image/png", async (o, e) => await ((WmsImageLayer)o).UpdateImageAsync()));
private string layers = string.Empty;
public Uri ServiceUri
{
get { return (Uri)GetValue(ServiceUriProperty); }
set { SetValue(ServiceUriProperty, value); }
}
public string Version
{
get { return (string)GetValue(VersionProperty); }
set { SetValue(VersionProperty, value); }
}
public string Layers
{
get { return (string)GetValue(LayersProperty); }
@ -75,57 +61,13 @@ namespace MapControl
set { SetValue(FormatProperty, value); }
}
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox)
{
ImageSource imageSource = null;
if (ServiceUri != null)
{
var version = Version;
var uri = GetRequestUri(ref version) + "REQUEST=GetMap&";
var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox, version.StartsWith("1.1."));
if (!string.IsNullOrEmpty(projectionParameters))
{
if (uri.IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0)
{
uri += "LAYERS=" + (Layers ?? "") + "&";
}
if (uri.IndexOf("STYLES=", StringComparison.OrdinalIgnoreCase) < 0)
{
uri += "STYLES=" + (Styles ?? "") + "&";
}
if (uri.IndexOf("FORMAT=", StringComparison.OrdinalIgnoreCase) < 0)
{
uri += "FORMAT=" + (Format ?? "") + "&";
}
uri += projectionParameters;
try
{
imageSource = await ImageLoader.LoadImageAsync(new Uri(uri.Replace(" ", "%20")), false);
}
catch (Exception ex)
{
Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message);
}
}
}
return imageSource;
}
public async Task<IList<string>> GetLayerNamesAsync()
{
IList<string> layerNames = null;
if (ServiceUri != null)
{
var version = Version;
var uri = GetRequestUri(ref version) + "REQUEST=GetCapabilities";
var uri = GetRequestUri("GetCapabilities");
try
{
@ -158,13 +100,45 @@ namespace MapControl
return layerNames;
}
private string GetRequestUri(ref string version)
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox)
{
if (version == null)
ImageSource imageSource = null;
var projectionParameters = ParentMap.MapProjection.WmsQueryParameters(boundingBox);
if (ServiceUri != null && !string.IsNullOrEmpty(projectionParameters))
{
version = DefaultVersion;
var uri = GetRequestUri("GetMap&" + projectionParameters);
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;
}
try
{
imageSource = await ImageLoader.LoadImageAsync(new Uri(uri.Replace(" ", "%20")), false);
}
catch (Exception ex)
{
Debug.WriteLine("WmsImageLayer: {0}: {1}", uri, ex.Message);
}
}
return imageSource;
}
private string GetRequestUri(string request)
{
var uri = ServiceUri.ToString();
if (!uri.EndsWith("?") && !uri.EndsWith("&"))
@ -177,19 +151,12 @@ namespace MapControl
uri += "SERVICE=WMS&";
}
int versionStart = uri.IndexOf("VERSION=", StringComparison.OrdinalIgnoreCase);
int versionEnd;
if (versionStart < 0)
if (uri.IndexOf("VERSION=", StringComparison.OrdinalIgnoreCase) < 0)
{
uri += "VERSION=" + version + "&";
}
else if ((versionEnd = uri.IndexOf("&", versionStart + 8)) >= versionStart + 8)
{
version = uri.Substring(versionStart, versionEnd - versionStart);
uri += "VERSION=1.3.0&";
}
return uri;
return uri + "REQUEST=" + request;
}
private static IEnumerable<XmlElement> ChildElements(XmlElement element, string name)

View file

@ -99,7 +99,8 @@ namespace ViewModel
new WmsImageLayer
{
Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/)\nData © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)",
ServiceUri = new Uri("http://ows.terrestris.de/osm/service?SERVICE=WMS&VERSION=1.3.0&LAYERS=OSM-WMS&STYLES=&FORMAT=image/png")
ServiceUri = new Uri("http://ows.terrestris.de/osm/service"),
Layers = "OSM-WMS"
}
},
{
@ -107,7 +108,8 @@ namespace ViewModel
new WmsImageLayer
{
Description = "© [terrestris GmbH & Co. KG](http://ows.terrestris.de/)\nData © [OpenStreetMap contributors](http://www.openstreetmap.org/copyright)",
ServiceUri = new Uri("http://ows.terrestris.de/osm/service?SERVICE=WMS&VERSION=1.3.0&LAYERS=TOPO-OSM-WMS&STYLES=&FORMAT=image/png")
ServiceUri = new Uri("http://ows.terrestris.de/osm/service"),
Layers = "TOPO-OSM-WMS"
}
},
{
@ -115,7 +117,8 @@ namespace ViewModel
new WmsImageLayer
{
Description = "© [SevenCs GmbH](http://www.sevencs.com)",
ServiceUri = new Uri("http://chartserver4.sevencs.com:8080?SERVICE=WMS&VERSION=1.3.0&LAYERS=ENC&STYLES=&FORMAT=image/png"),
ServiceUri = new Uri("http://chartserver4.sevencs.com:8080"),
Layers = "ENC",
MaxBoundingBoxWidth = 360
}
}