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

272 lines
9.3 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 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;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
2022-08-02 19:50:11 +02:00
using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
2021-11-17 23:17:11 +01:00
#elif 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;
#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 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));
public static readonly DependencyProperty LoadingProgressProperty = DependencyProperty.Register(
nameof(LoadingProgress), typeof(double), typeof(MapImageLayer), new PropertyMetadata(1d));
private readonly Progress<double> imageProgress;
2022-01-12 23:56:05 +01:00
private readonly DispatcherTimer updateTimer;
private bool updateInProgress;
public MapImageLayer()
{
imageProgress = new Progress<double>(p => LoadingProgress = p);
2022-01-12 23:56:05 +01:00
updateTimer = this.CreateTimer(UpdateInterval);
updateTimer.Tick += async (s, e) => await UpdateImageAsync();
}
/// <summary>
2021-11-28 23:50:13 +01:00
/// Description of the layer. Used to display copyright information on top of the map.
/// </summary>
public string Description
{
2022-08-06 10:40:59 +02:00
get => (string)GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, 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
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(RelativeImageSizeProperty);
set => SetValue(RelativeImageSizeProperty, value);
2013-05-15 15:58:07 +02:00
}
/// <summary>
/// Minimum time interval between images updates.
/// </summary>
public TimeSpan UpdateInterval
{
2022-08-06 10:40:59 +02:00
get => (TimeSpan)GetValue(UpdateIntervalProperty);
set => SetValue(UpdateIntervalProperty, value);
}
/// <summary>
/// Controls if images are updated while the viewport is still changing.
/// </summary>
public bool UpdateWhileViewportChanging
{
2022-08-06 10:40:59 +02:00
get => (bool)GetValue(UpdateWhileViewportChangingProperty);
set => SetValue(UpdateWhileViewportChangingProperty, value);
}
/// <summary>
2021-11-28 23:50:13 +01:00
/// Optional background brush. Sets MapBase.Background if not null and this layer is the base map layer.
/// </summary>
2021-11-28 23:50:13 +01:00
public Brush MapBackground
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(MapBackgroundProperty);
set => SetValue(MapBackgroundProperty, value);
}
/// <summary>
2021-11-28 23:50:13 +01:00
/// Optional foreground brush. Sets MapBase.Foreground if not null and this layer is the base map layer.
/// </summary>
2021-11-28 23:50:13 +01:00
public Brush MapForeground
{
2022-08-06 10:40:59 +02:00
get => (Brush)GetValue(MapForegroundProperty);
set => SetValue(MapForegroundProperty, value);
2016-02-23 20:07:30 +01:00
}
/// <summary>
/// Gets the progress of the ImageLoader as a double value between 0 and 1.
/// </summary>
public double LoadingProgress
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(LoadingProgressProperty);
private set => SetValue(LoadingProgressProperty, value);
}
/// <summary>
/// The current BoundingBox
/// </summary>
public BoundingBox BoundingBox { get; private set; }
2021-11-14 22:25:34 +01:00
protected override void SetParentMap(MapBase map)
{
2022-11-12 17:27:49 +01:00
if (map != null)
2021-11-14 22:25:34 +01:00
{
2022-11-12 17:27:49 +01:00
while (Children.Count < 2)
2022-11-12 11:08:10 +01:00
{
Children.Add(new Image
{
Opacity = 0d,
Stretch = Stretch.Fill,
IsHitTestVisible = false // avoid touch capture issues
});
}
2021-11-14 22:25:34 +01:00
}
2022-11-12 17:27:49 +01:00
else
{
updateTimer.Stop();
ClearImages();
Children.Clear();
}
2021-11-14 22:25:34 +01:00
base.SetParentMap(map);
}
2020-07-03 07:20:42 +02:00
protected override async void OnViewportChanged(ViewportChangedEventArgs e)
{
2022-11-25 19:05:48 +01:00
base.OnViewportChanged(e);
if (e.ProjectionChanged)
{
ClearImages();
await UpdateImageAsync(); // update immediately
}
else
{
2022-01-12 23:56:05 +01:00
updateTimer.Run(!UpdateWhileViewportChanging);
}
}
2020-10-25 16:09:09 +01:00
protected async Task UpdateImageAsync()
{
2022-11-25 19:05:48 +01:00
if (updateInProgress)
{
2022-11-25 19:05:48 +01:00
// update image on next tick, start timer if not running
//
updateTimer.Run();
}
else
{
updateTimer.Stop();
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)
{
try
{
image = await GetImageAsync(imageProgress);
}
catch (Exception ex)
{
Debug.WriteLine($"MapImageLayer: {ex.Message}");
}
}
SwapImages(image);
updateInProgress = false;
}
}
}
protected abstract Task<ImageSource> GetImageAsync(IProgress<double> progress);
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);
}
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;
2022-02-22 22:12:15 +01:00
SetBoundingBox(topImage, BoundingBox);
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
});
}
}
}
}