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

118 lines
3.9 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2024-05-26 09:49:00 +02:00
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
/// <summary>
/// Base class of MapPolyline, MapPolygon and MapMultiPolygon.
/// </summary>
public class MapPolypoint : MapPath, IWeakEventListener
{
public static readonly DependencyProperty FillRuleProperty =
DependencyPropertyHelper.Register<MapPolygon, FillRule>(nameof(FillRule), FillRule.EvenOdd,
2024-05-26 12:39:40 +02:00
(polypoint, oldValue, newValue) => ((StreamGeometry)polypoint.Data).FillRule = newValue);
2024-05-26 09:49:00 +02:00
public FillRule FillRule
{
get => (FillRule)GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
protected MapPolypoint()
{
2024-05-26 12:39:40 +02:00
Data = new StreamGeometry();
2024-05-26 09:49:00 +02:00
}
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
CollectionChangedEventManager.RemoveListener(oldCollection, this);
}
if (newValue is INotifyCollectionChanged newCollection)
{
CollectionChangedEventManager.AddListener(newCollection, this);
}
UpdateData();
2024-05-26 12:39:40 +02:00
InvalidateVisual(); // necessary for StreamGeometry
2024-05-26 09:49:00 +02:00
}
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
UpdateData();
2024-05-26 12:39:40 +02:00
InvalidateVisual(); // necessary for StreamGeometry
2024-05-26 09:49:00 +02:00
return true;
}
protected void UpdateData(IEnumerable<Location> locations, bool closed)
{
2024-05-26 12:39:40 +02:00
using (var context = ((StreamGeometry)Data).Open())
2024-05-26 09:49:00 +02:00
{
2024-07-15 13:41:52 +02:00
if (ParentMap != null && locations != null)
2024-05-26 12:39:40 +02:00
{
2024-07-15 13:41:52 +02:00
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
2024-05-26 09:49:00 +02:00
2024-05-26 12:39:40 +02:00
AddPolylinePoints(context, locations, longitudeOffset, closed);
}
2024-05-26 09:49:00 +02:00
}
}
protected void UpdateData(IEnumerable<IEnumerable<Location>> polygons)
{
2024-05-26 12:39:40 +02:00
using (var context = ((StreamGeometry)Data).Open())
2024-05-26 09:49:00 +02:00
{
2024-05-26 12:39:40 +02:00
if (ParentMap != null && polygons != null)
2024-05-26 09:49:00 +02:00
{
2024-05-26 12:39:40 +02:00
var longitudeOffset = GetLongitudeOffset(Location);
2024-07-16 21:29:25 +02:00
foreach (var locations in polygons)
2024-05-26 12:39:40 +02:00
{
2024-07-16 21:29:25 +02:00
AddPolylinePoints(context, locations, longitudeOffset, true);
2024-05-26 12:39:40 +02:00
}
2024-05-26 09:49:00 +02:00
}
}
}
2024-05-26 12:39:40 +02:00
private void AddPolylinePoints(StreamGeometryContext context, IEnumerable<Location> locations, double longitudeOffset, bool closed)
2024-05-26 09:49:00 +02:00
{
2024-07-15 09:09:15 +02:00
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
2024-05-26 09:49:00 +02:00
2024-07-15 13:41:52 +02:00
if (points.Any())
{
2024-07-16 21:29:25 +02:00
var start = points.First();
var polyline = points.Skip(1).ToList();
var minX = start.X;
var maxX = start.X;
var minY = start.Y;
var maxY = start.Y;
foreach (var point in polyline)
{
minX = Math.Min(minX, point.X);
maxX = Math.Max(maxX, point.X);
minY = Math.Min(minY, point.Y);
maxY = Math.Max(maxY, point.Y);
}
if (maxX >= 0 && minX <= ParentMap.ActualWidth &&
maxY >= 0 && minY <= ParentMap.ActualHeight)
{
context.BeginFigure(start, true, closed);
context.PolyLineTo(polyline, true, true);
}
2024-07-15 13:41:52 +02:00
}
2024-05-26 09:49:00 +02:00
}
}
}