Rework Map/WmsImageLayer

This commit is contained in:
ClemensFischer 2022-11-30 17:59:38 +01:00
parent c3e1ffd685
commit 9db1987ee5
7 changed files with 265 additions and 103 deletions

View file

@ -33,6 +33,8 @@ namespace MapControl
{
ImageSource image = null;
progress?.Report(0d);
try
{
if (!uri.IsAbsoluteUri || uri.IsFile)
@ -58,6 +60,8 @@ namespace MapControl
Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}");
}
progress?.Report(1d);
return image;
}
@ -77,8 +81,6 @@ namespace MapControl
{
HttpResponse response = null;
progress?.Report(0d);
try
{
using (var responseMessage = await HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
@ -108,8 +110,6 @@ namespace MapControl
Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}");
}
progress?.Report(1d);
return response;
}

View file

@ -262,12 +262,12 @@ namespace MapControl
var p3 = ViewTransform.ViewToMap(new Point(rect.X + rect.Width, rect.Y));
var p4 = ViewTransform.ViewToMap(new Point(rect.X + rect.Width, rect.Y + rect.Height));
rect.X = Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X)));
rect.Y = Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y)));
rect.Width = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X))) - rect.X;
rect.Height = Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y))) - rect.Y;
var x1 = Math.Min(p1.X, Math.Min(p2.X, Math.Min(p3.X, p4.X)));
var x2 = Math.Max(p1.X, Math.Max(p2.X, Math.Max(p3.X, p4.X)));
var y1 = Math.Min(p1.Y, Math.Min(p2.Y, Math.Min(p3.Y, p4.Y)));
var y2 = Math.Max(p1.Y, Math.Max(p2.Y, Math.Max(p3.Y, p4.Y)));
return MapProjection.RectToBoundingBox(rect);
return MapProjection.RectToBoundingBox(new Rect(x1, y1, x2 - x1, y2 - y1));
}
/// <summary>

View file

@ -134,11 +134,6 @@ namespace MapControl
private set => SetValue(LoadingProgressProperty, value);
}
/// <summary>
/// The current BoundingBox
/// </summary>
public BoundingBox BoundingBox { get; private set; }
protected override void SetParentMap(MapBase map)
{
if (map != null)
@ -179,6 +174,8 @@ namespace MapControl
}
}
protected abstract Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress);
protected async Task UpdateImageAsync()
{
if (updateInProgress)
@ -190,45 +187,44 @@ namespace MapControl
else
{
updateTimer.Stop();
updateInProgress = true;
if (ParentMap != null && ParentMap.RenderSize.Width > 0 && ParentMap.RenderSize.Height > 0)
ImageSource image = null;
var boundingBox = GetImageBoundingBox();
if (boundingBox != null)
{
updateInProgress = true;
UpdateBoundingBox();
ImageSource image = null;
if (BoundingBox != null)
try
{
try
{
image = await GetImageAsync(imageProgress);
}
catch (Exception ex)
{
Debug.WriteLine($"MapImageLayer: {ex.Message}");
}
image = await GetImageAsync(boundingBox, imageProgress);
}
catch (Exception ex)
{
Debug.WriteLine($"MapImageLayer: {ex.Message}");
}
SwapImages(image);
updateInProgress = false;
}
SwapImages(image, boundingBox);
updateInProgress = false;
}
}
protected abstract Task<ImageSource> GetImageAsync(IProgress<double> progress);
private void UpdateBoundingBox()
protected BoundingBox GetImageBoundingBox()
{
var width = ParentMap.RenderSize.Width * RelativeImageSize;
var height = ParentMap.RenderSize.Height * RelativeImageSize;
var x = (ParentMap.RenderSize.Width - width) / 2d;
var y = (ParentMap.RenderSize.Height - height) / 2d;
var rect = new Rect(x, y, width, height);
BoundingBox boundingBox = null;
BoundingBox = ParentMap.ViewRectToBoundingBox(rect);
if (ParentMap != null && ParentMap.RenderSize.Width > 0d && ParentMap.RenderSize.Height > 0d)
{
var width = ParentMap.RenderSize.Width * RelativeImageSize;
var height = ParentMap.RenderSize.Height * RelativeImageSize;
var x = (ParentMap.RenderSize.Width - width) / 2d;
var y = (ParentMap.RenderSize.Height - height) / 2d;
boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(x, y, width, height));
}
return boundingBox;
}
private void ClearImages()
@ -240,7 +236,7 @@ namespace MapControl
}
}
private void SwapImages(ImageSource image)
private void SwapImages(ImageSource image, BoundingBox boundingBox)
{
if (Children.Count >= 2)
{
@ -251,7 +247,7 @@ namespace MapControl
Children.Insert(1, topImage);
topImage.Source = image;
SetBoundingBox(topImage, BoundingBox);
SetBoundingBox(topImage, boundingBox);
topImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{

View file

@ -134,44 +134,17 @@ namespace MapControl
return element;
}
/// <summary>
/// Loads an XElement from the URL returned by GetFeatureInfoRequestUri().
/// </summary>
public async Task<XElement> GetFeatureInfoAsync(Point position)
{
XElement element = null;
if (ServiceUri != null)
{
var uri = GetFeatureInfoRequestUri(position, "text/xml");
if (!string.IsNullOrEmpty(uri))
{
try
{
using (var stream = await ImageLoader.HttpClient.GetStreamAsync(uri))
{
element = XDocument.Load(stream).Root;
}
}
catch (Exception ex)
{
Debug.WriteLine($"WmsImageLayer: {uri}: {ex.Message}");
}
}
}
return element;
}
/// <summary>
/// Gets a response string from the URL returned by GetFeatureInfoRequestUri().
/// </summary>
public async Task<string> GetFeatureInfoTextAsync(Point position, string format = "text/plain")
public async Task<string> GetFeatureInfoAsync(Point position, string format = "text/plain")
{
string response = null;
if (ServiceUri != null)
if (ServiceUri != null &&
ParentMap?.MapProjection != null &&
ParentMap.RenderSize.Width > 0d &&
ParentMap.RenderSize.Height > 0d)
{
var uri = GetFeatureInfoRequestUri(position, format);
@ -194,11 +167,11 @@ namespace MapControl
/// <summary>
/// Loads an ImageSource from the URL returned by GetMapRequestUri().
/// </summary>
protected override async Task<ImageSource> GetImageAsync(IProgress<double> progress)
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress)
{
ImageSource image = null;
if (ServiceUri != null)
if (ServiceUri != null && ParentMap?.MapProjection != null)
{
if (Layers == null &&
ServiceUri.ToString().IndexOf("LAYERS=", StringComparison.OrdinalIgnoreCase) < 0)
@ -206,11 +179,38 @@ namespace MapControl
Layers = (await GetLayerNamesAsync())?.FirstOrDefault() ?? ""; // get first Layer from Capabilities
}
var uri = GetMapRequestUri();
if (!string.IsNullOrEmpty(uri))
if (boundingBox.West >= -180d && boundingBox.East <= 180d ||
ParentMap.MapProjection.Type > MapProjectionType.NormalCylindrical)
{
image = await ImageLoader.LoadImageAsync(new Uri(uri), progress);
var uri = CreateUri(GetMapRequestUri(boundingBox));
if (uri != null)
{
image = await ImageLoader.LoadImageAsync(uri, progress);
}
}
else
{
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);
}
}
}
@ -233,14 +233,9 @@ namespace MapControl
/// <summary>
/// Returns a GetMap request URL string.
/// </summary>
protected virtual string GetMapRequestUri()
protected virtual string GetMapRequestUri(BoundingBox boundingBox)
{
if (ParentMap?.MapProjection == null)
{
return null;
}
var mapRect = ParentMap.MapProjection.BoundingBoxToRect(BoundingBox);
var mapRect = ParentMap.MapProjection.BoundingBoxToRect(boundingBox);
var viewScale = ParentMap.ViewTransform.Scale;
return GetRequestUri(new Dictionary<string, string>
@ -263,14 +258,10 @@ namespace MapControl
/// </summary>
protected virtual string GetFeatureInfoRequestUri(Point position, string format)
{
if (ParentMap?.MapProjection == null)
{
return null;
}
var mapRect = ParentMap.MapProjection.BoundingBoxToRect(BoundingBox);
var viewRect = GetViewRect(mapRect);
var viewSize = ParentMap.RenderSize;
var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(0d, 0d, viewSize.Width, viewSize.Height));
var mapRect = ParentMap.MapProjection.BoundingBoxToRect(boundingBox);
var viewRect = GetViewRect(mapRect);
var transform = new Matrix(1, 0, 0, 1, -viewSize.Width / 2, -viewSize.Height / 2);
transform.Rotate(-viewRect.Rotation);
@ -326,5 +317,22 @@ namespace MapControl
return uri.Replace(" ", "%20");
}
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;
}
}
}