Added MapPolypoint base class

This commit is contained in:
ClemensFischer 2024-05-26 09:49:00 +02:00
parent aeb3fd047f
commit 84890090f8
11 changed files with 357 additions and 317 deletions

View file

@ -4,28 +4,23 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace MapControl
{
/// <summary>
/// A multi-polygon defined by a collection of collections of Locations.
/// Allows to draw polygons with holes.
/// Allows to draw filled polygons with holes.
///
/// A PolygonCollection (with ObservableCollection of Location elements) may be used
/// for the Polygons property if collection changes of the property itself and its
/// elements are both supposed to trigger UI updates.
/// </summary>
public class MapMultiPolygon : MapPath
public class MapMultiPolygon : MapPolypoint
{
public static readonly DependencyProperty PolygonsProperty =
DependencyPropertyHelper.Register<MapMultiPolygon, IEnumerable<IEnumerable<Location>>>(nameof(Polygons), null,
(polygon, oldValue, newValue) => polygon.DataCollectionPropertyChanged(oldValue, newValue));
public static readonly DependencyProperty FillRuleProperty =
DependencyPropertyHelper.Register<MapMultiPolygon, FillRule>(nameof(FillRule), FillRule.EvenOdd,
(polygon, oldValue, newValue) => ((PathGeometry)polygon.Data).FillRule = newValue);
/// <summary>
/// Gets or sets the Locations that define the multi-polygon points.
/// </summary>
@ -35,22 +30,9 @@ namespace MapControl
set => SetValue(PolygonsProperty, value);
}
public FillRule FillRule
{
get => (FillRule)GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
public MapMultiPolygon()
{
Data = new PathGeometry();
}
protected override void UpdateData()
{
var figures = ((PathGeometry)Data).Figures;
figures.Clear();
AddMultiPolygonPoints(figures, Polygons);
UpdateData(Polygons);
}
}
}

View file

@ -2,18 +2,13 @@
// 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;
using System.Windows.Shapes;
namespace MapControl
{
public partial class MapPath : Shape, IWeakEventListener
public partial class MapPath : Shape
{
public MapPath()
{
@ -60,56 +55,5 @@ namespace MapControl
Data.Transform = new MatrixTransform(matrix);
}
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
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();
}
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
UpdateData();
return true;
}
protected void SetPathFigures(PathFigureCollection pathFigures)
{
((PathGeometry)Data).Figures = pathFigures;
}
protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
{
if (locations.Count() >= 2)
{
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
IsFilled = true
};
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
pathFigures.Add(figure);
}
}
#endregion
}
}

View file

@ -0,0 +1,108 @@
// 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,
(polypoint, oldValue, newValue) => ((PathGeometry)polypoint.Data).FillRule = newValue);
public FillRule FillRule
{
get => (FillRule)GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
protected MapPolypoint()
{
Data = new PathGeometry();
}
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();
}
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
UpdateData();
return true;
}
protected void UpdateData(IEnumerable<Location> locations, bool closed)
{
var pathFigures = new PathFigureCollection();
if (ParentMap != null && locations != null)
{
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
}
((PathGeometry)Data).Figures = pathFigures;
}
protected void UpdateData(IEnumerable<IEnumerable<Location>> polygons)
{
var pathFigures = new PathFigureCollection();
if (ParentMap != null && polygons != null)
{
var longitudeOffset = GetLongitudeOffset(Location);
foreach (var polygon in polygons)
{
AddPolylinePoints(pathFigures, polygon, longitudeOffset, true);
}
}
((PathGeometry)Data).Figures = pathFigures;
}
protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, double longitudeOffset, bool closed)
{
if (locations.Count() >= 2)
{
var points = locations
.Select(location => LocationToView(location, longitudeOffset))
.Where(point => point.HasValue)
.Select(point => point.Value);
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
IsFilled = true
};
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
pathFigures.Add(figure);
}
}
}
}