XAML-Map-Control/MapControl/WPF/MapItemsImageLayer.WPF.cs

95 lines
3.1 KiB
C#
Raw Normal View History

2020-06-20 17:58:04 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2023-01-03 15:12:53 +01:00
// Copyright © 2023 Clemens Fischer
2020-06-20 17:58:04 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2020-06-20 17:58:04 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
public interface IMapDrawingItem
{
IEnumerable<Location> Locations { get; }
Drawing GetDrawing(IList<Point> points, double scale, double rotation);
2020-06-20 17:58:04 +02:00
}
public class MapItemsImageLayer : MapImageLayer
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
nameof(ItemsSource), typeof(IEnumerable<IMapDrawingItem>), typeof(MapItemsImageLayer));
public IEnumerable<IMapDrawingItem> ItemsSource
{
2022-08-06 10:40:59 +02:00
get => (IEnumerable<IMapDrawingItem>)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
2020-06-20 17:58:04 +02:00
}
2022-11-30 17:59:38 +01:00
protected override async Task<ImageSource> GetImageAsync(BoundingBox boundingBox, IProgress<double> progress)
2020-06-20 17:58:04 +02:00
{
ImageSource image = null;
var projection = ParentMap?.MapProjection;
var items = ItemsSource;
if (projection != null && items != null)
{
2022-12-14 18:02:19 +01:00
var mapRect = projection.BoundingBoxToMapRect(boundingBox);
if (mapRect != null)
{
image = await Task.Run(() => GetImage(projection, mapRect, items));
}
2020-06-20 17:58:04 +02:00
}
return image;
}
2022-12-14 18:02:19 +01:00
private DrawingImage GetImage(MapProjection projection, MapRect mapRect, IEnumerable<IMapDrawingItem> items)
2020-06-20 17:58:04 +02:00
{
var scale = ParentMap.ViewTransform.Scale;
var rotation = ParentMap.ViewTransform.Rotation;
var drawings = new DrawingGroup();
foreach (var item in items)
{
var points = item.Locations
.Select(location => projection.LocationToMap(location))
.Where(point => point.HasValue)
.Select(point => point.Value)
.ToList();
2020-06-20 17:58:04 +02:00
if (points.Any(point => mapRect.Contains(point)))
2020-06-20 17:58:04 +02:00
{
for (int i = 0; i < points.Count; i++)
2020-06-20 17:58:04 +02:00
{
points[i] = new Point(
2022-12-07 17:00:25 +01:00
scale * (points[i].X - mapRect.XMin),
scale * (mapRect.YMax - points[i].Y));
2020-06-20 17:58:04 +02:00
}
drawings.Children.Add(item.GetDrawing(points, scale, rotation));
2020-06-20 17:58:04 +02:00
}
}
2021-01-26 00:25:57 +01:00
var drawingBrush = new DrawingBrush
2020-06-20 17:58:04 +02:00
{
2021-01-26 00:25:57 +01:00
Drawing = drawings,
2020-06-20 17:58:04 +02:00
ViewboxUnits = BrushMappingMode.Absolute,
2021-01-24 22:12:46 +01:00
Viewbox = new Rect(0, 0, scale * mapRect.Width, scale * mapRect.Height),
2020-06-20 17:58:04 +02:00
};
2021-01-24 22:12:46 +01:00
var drawing = new GeometryDrawing(
drawingBrush, null, new RectangleGeometry(drawingBrush.Viewbox));
2020-06-20 17:58:04 +02:00
var image = new DrawingImage(drawing);
image.Freeze();
return image;
}
}
}