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

103 lines
3.4 KiB
C#
Raw Normal View History

2024-05-26 09:49:00 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
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-05-26 12:39:40 +02:00
if (ParentMap != null && locations != null)
{
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);
foreach (var polygon in polygons)
{
AddPolylinePoints(context, polygon, longitudeOffset, true);
}
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
{
if (locations.Count() >= 2)
{
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
2024-05-26 12:39:40 +02:00
context.BeginFigure(points.First(), true, closed);
context.PolyLineTo(points.Skip(1).ToList(), true, true);
2024-05-26 09:49:00 +02:00
}
}
}
}