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

376 lines
13 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
2021-06-14 21:41:37 +02:00
#if WINUI
using Windows.Foundation;
2021-07-05 00:03:44 +02:00
using Microsoft.UI.Dispatching;
2021-06-14 21:41:37 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
#elif WINDOWS_UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
2021-11-14 22:25:34 +01:00
using System.Xml.Linq;
#endif
namespace MapControl
{
/// <summary>
/// Displays a single map image, e.g. from a Web Map Service (WMS).
/// The image must be provided by the abstract GetImageAsync() method.
/// </summary>
public abstract class MapImageLayer : MapPanel, IMapLayer
{
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
nameof(Description), typeof(string), typeof(MapImageLayer), new PropertyMetadata(null));
public static readonly DependencyProperty MinLatitudeProperty = DependencyProperty.Register(
nameof(MinLatitude), typeof(double), typeof(MapImageLayer), new PropertyMetadata(double.NaN));
2016-02-23 20:07:30 +01:00
public static readonly DependencyProperty MaxLatitudeProperty = DependencyProperty.Register(
nameof(MaxLatitude), typeof(double), typeof(MapImageLayer), new PropertyMetadata(double.NaN));
2016-02-23 20:07:30 +01:00
public static readonly DependencyProperty MinLongitudeProperty = DependencyProperty.Register(
nameof(MinLongitude), typeof(double), typeof(MapImageLayer), new PropertyMetadata(double.NaN));
2016-02-23 20:07:30 +01:00
public static readonly DependencyProperty MaxLongitudeProperty = DependencyProperty.Register(
nameof(MaxLongitude), typeof(double), typeof(MapImageLayer), new PropertyMetadata(double.NaN));
2016-02-23 20:07:30 +01:00
public static readonly DependencyProperty MaxBoundingBoxWidthProperty = DependencyProperty.Register(
nameof(MaxBoundingBoxWidth), typeof(double), typeof(MapImageLayer), new PropertyMetadata(double.NaN));
public static readonly DependencyProperty RelativeImageSizeProperty = DependencyProperty.Register(
nameof(RelativeImageSize), typeof(double), typeof(MapImageLayer), new PropertyMetadata(1d));
2013-05-15 15:58:07 +02:00
public static readonly DependencyProperty UpdateIntervalProperty = DependencyProperty.Register(
nameof(UpdateInterval), typeof(TimeSpan), typeof(MapImageLayer),
new PropertyMetadata(TimeSpan.FromSeconds(0.2), (o, e) => ((MapImageLayer)o).updateTimer.Interval = (TimeSpan)e.NewValue));
public static readonly DependencyProperty UpdateWhileViewportChangingProperty = DependencyProperty.Register(
nameof(UpdateWhileViewportChanging), typeof(bool), typeof(MapImageLayer), new PropertyMetadata(false));
public static readonly DependencyProperty MapBackgroundProperty = DependencyProperty.Register(
nameof(MapBackground), typeof(Brush), typeof(MapImageLayer), new PropertyMetadata(null));
public static readonly DependencyProperty MapForegroundProperty = DependencyProperty.Register(
nameof(MapForeground), typeof(Brush), typeof(MapImageLayer), new PropertyMetadata(null));
2021-07-05 00:03:44 +02:00
#if WINUI
private readonly DispatcherQueueTimer updateTimer;
#else
private readonly DispatcherTimer updateTimer = new DispatcherTimer();
#endif
private bool updateInProgress;
public MapImageLayer()
{
2021-07-05 00:03:44 +02:00
#if WINUI
updateTimer = DispatcherQueue.CreateTimer();
#endif
updateTimer.Interval = UpdateInterval;
updateTimer.Tick += async (s, e) => await UpdateImageAsync();
}
/// <summary>
/// Description of the MapImageLayer.
/// Used to display copyright information on top of the map.
/// </summary>
public string Description
{
get { return (string)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}
/// <summary>
/// Optional minimum latitude value. Default is NaN.
/// </summary>
public double MinLatitude
{
get { return (double)GetValue(MinLatitudeProperty); }
set { SetValue(MinLatitudeProperty, value); }
}
/// <summary>
/// Optional maximum latitude value. Default is NaN.
/// </summary>
public double MaxLatitude
{
get { return (double)GetValue(MaxLatitudeProperty); }
set { SetValue(MaxLatitudeProperty, value); }
}
2016-02-23 20:07:30 +01:00
/// <summary>
/// Optional minimum longitude value. Default is NaN.
/// </summary>
public double MinLongitude
{
get { return (double)GetValue(MinLongitudeProperty); }
set { SetValue(MinLongitudeProperty, value); }
}
/// <summary>
/// Optional maximum longitude value. Default is NaN.
/// </summary>
public double MaxLongitude
{
get { return (double)GetValue(MaxLongitudeProperty); }
set { SetValue(MaxLongitudeProperty, value); }
}
2013-05-15 16:28:57 +02:00
/// <summary>
/// Optional maximum width of the map image's bounding box. Default is NaN.
/// </summary>
public double MaxBoundingBoxWidth
{
get { return (double)GetValue(MaxBoundingBoxWidthProperty); }
set { SetValue(MaxBoundingBoxWidthProperty, value); }
}
/// <summary>
/// Relative size of the map image in relation to the current view size.
2017-07-17 21:31:09 +02:00
/// Setting a value greater than one will let MapImageLayer request images that
/// are larger than the view, in order to support smooth panning.
2013-05-15 16:28:57 +02:00
/// </summary>
2013-05-15 15:58:07 +02:00
public double RelativeImageSize
{
get { return (double)GetValue(RelativeImageSizeProperty); }
set { SetValue(RelativeImageSizeProperty, value); }
}
/// <summary>
/// Minimum time interval between images updates.
/// </summary>
public TimeSpan UpdateInterval
{
get { return (TimeSpan)GetValue(UpdateIntervalProperty); }
set { SetValue(UpdateIntervalProperty, value); }
}
/// <summary>
/// Controls if images are updated while the viewport is still changing.
/// </summary>
public bool UpdateWhileViewportChanging
{
get { return (bool)GetValue(UpdateWhileViewportChangingProperty); }
set { SetValue(UpdateWhileViewportChangingProperty, value); }
}
/// <summary>
/// Optional foreground brush.
2017-07-17 21:31:09 +02:00
/// Sets MapBase.Foreground if not null and the MapImageLayer is the base map layer.
/// </summary>
public Brush MapForeground
{
get { return (Brush)GetValue(MapForegroundProperty); }
set { SetValue(MapForegroundProperty, value); }
}
/// <summary>
/// Optional background brush.
2017-07-17 21:31:09 +02:00
/// Sets MapBase.Background if not null and the MapImageLayer is the base map layer.
/// </summary>
public Brush MapBackground
{
get { return (Brush)GetValue(MapBackgroundProperty); }
set { SetValue(MapBackgroundProperty, value); }
2016-02-23 20:07:30 +01:00
}
/// <summary>
/// The current BoundingBox
/// </summary>
public BoundingBox BoundingBox { get; private set; }
protected abstract Task<ImageSource> GetImageAsync();
2021-11-14 22:25:34 +01:00
protected override void SetParentMap(MapBase map)
{
if (map == null)
{
updateTimer.Stop();
ClearImages();
Children.Clear();
}
else if (Children.Count == 0)
{
Children.Add(new Image { Opacity = 0d, Stretch = Stretch.Fill });
Children.Add(new Image { Opacity = 0d, Stretch = Stretch.Fill });
}
base.SetParentMap(map);
}
2020-07-03 07:20:42 +02:00
protected override async void OnViewportChanged(ViewportChangedEventArgs e)
{
if (e.ProjectionChanged)
{
ClearImages();
base.OnViewportChanged(e);
2020-07-03 07:20:42 +02:00
await UpdateImageAsync();
}
else
{
AdjustBoundingBox(e.LongitudeOffset);
base.OnViewportChanged(e);
2020-10-27 23:34:14 +01:00
if (!UpdateWhileViewportChanging)
{
updateTimer.Stop(); // restart
}
2020-10-27 23:34:14 +01:00
updateTimer.Start();
}
}
2020-10-25 16:09:09 +01:00
protected async Task UpdateImageAsync()
{
updateTimer.Stop();
if (updateInProgress)
{
updateTimer.Start(); // update image on next timer tick
}
2016-02-23 20:07:30 +01:00
else if (ParentMap != null && ParentMap.RenderSize.Width > 0 && ParentMap.RenderSize.Height > 0)
{
updateInProgress = true;
2017-11-02 19:05:46 +01:00
UpdateBoundingBox();
ImageSource image = null;
if (BoundingBox != null)
2016-02-23 20:07:30 +01:00
{
try
{
image = await GetImageAsync();
}
catch (Exception ex)
{
Debug.WriteLine($"MapImageLayer: {ex.Message}");
}
}
SwapImages(image);
updateInProgress = false;
}
}
2017-11-02 19:05:46 +01:00
private void UpdateBoundingBox()
{
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);
2016-02-23 20:07:30 +01:00
BoundingBox = ParentMap.ViewRectToBoundingBox(rect);
2016-02-23 20:07:30 +01:00
if (BoundingBox != null)
{
if (!double.IsNaN(MinLatitude) && BoundingBox.South < MinLatitude)
2017-11-02 19:05:46 +01:00
{
BoundingBox.South = MinLatitude;
2017-11-02 19:05:46 +01:00
}
if (!double.IsNaN(MinLongitude) && BoundingBox.West < MinLongitude)
2017-11-02 19:05:46 +01:00
{
BoundingBox.West = MinLongitude;
2017-11-02 19:05:46 +01:00
}
if (!double.IsNaN(MaxLatitude) && BoundingBox.North > MaxLatitude)
2017-11-02 19:05:46 +01:00
{
BoundingBox.North = MaxLatitude;
2017-11-02 19:05:46 +01:00
}
if (!double.IsNaN(MaxLongitude) && BoundingBox.East > MaxLongitude)
2017-11-02 19:05:46 +01:00
{
BoundingBox.East = MaxLongitude;
2017-11-02 19:05:46 +01:00
}
if (!double.IsNaN(MaxBoundingBoxWidth) && BoundingBox.Width > MaxBoundingBoxWidth)
2017-11-02 19:05:46 +01:00
{
2021-11-14 22:25:34 +01:00
var margin = (BoundingBox.Width - MaxBoundingBoxWidth) / 2d;
BoundingBox.West += margin;
BoundingBox.East -= margin;
2017-11-02 19:05:46 +01:00
}
}
}
private void AdjustBoundingBox(double longitudeOffset)
{
if (Math.Abs(longitudeOffset) > 180d && BoundingBox != null)
{
var offset = 360d * Math.Sign(longitudeOffset);
BoundingBox.West += offset;
BoundingBox.East += offset;
2021-11-14 22:25:34 +01:00
foreach (var image in Children.OfType<Image>())
{
2021-11-14 22:25:34 +01:00
var bbox = GetBoundingBox(image);
2018-12-20 21:55:12 +01:00
if (bbox != null)
{
2021-11-14 22:25:34 +01:00
SetBoundingBox(image, new BoundingBox(bbox.South, bbox.West + offset, bbox.North, bbox.East + offset));
}
}
}
}
private void ClearImages()
{
2021-11-14 22:25:34 +01:00
foreach (var image in Children.OfType<Image>())
{
2021-11-14 22:25:34 +01:00
image.ClearValue(BoundingBoxProperty);
image.ClearValue(Image.SourceProperty);
}
}
private void SwapImages(ImageSource image)
{
2021-11-14 22:25:34 +01:00
if (Children.Count >= 2)
{
var topImage = (Image)Children[0];
var bottomImage = (Image)Children[1];
2021-11-14 22:25:34 +01:00
Children.RemoveAt(0);
Children.Insert(1, topImage);
2021-11-14 22:25:34 +01:00
topImage.Source = image;
SetBoundingBox(topImage, BoundingBox?.Clone());
2021-11-14 22:25:34 +01:00
topImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{
To = 1d,
Duration = MapBase.ImageFadeDuration
});
2021-11-14 22:25:34 +01:00
bottomImage.BeginAnimation(OpacityProperty, new DoubleAnimation
{
To = 0d,
BeginTime = MapBase.ImageFadeDuration,
Duration = TimeSpan.Zero
});
}
}
}
}