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,10 +4,6 @@
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace MapControl
{
@ -43,57 +39,5 @@ namespace MapControl
InvalidateVisual();
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
protected void SetPathFigures(PathFigures pathFigures)
{
((PathGeometry)Data).Figures = pathFigures;
InvalidateGeometry();
}
protected void AddPolylinePoints(PathFigures 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)));
pathFigures.Add(figure);
}
}
#endregion
}
}

View file

@ -0,0 +1,90 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using Avalonia.Media;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace MapControl
{
/// <summary>
/// Base class of MapPolyline and MapPolygon.
/// </summary>
public class MapPolypoint : MapPath
{
public static readonly StyledProperty<FillRule> FillRuleProperty =
DependencyPropertyHelper.Register<MapPolygon, FillRule>(nameof(FillRule), FillRule.EvenOdd,
(polypoint, oldValue, newValue) => ((PathGeometry)polypoint.Data).FillRule = newValue);
public FillRule FillRule
{
get => GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
protected MapPolypoint()
{
Data = new PathGeometry();
}
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
protected void UpdateData(IEnumerable<Location> locations, bool closed)
{
var pathFigures = new PathFigures();
if (ParentMap != null && locations != null)
{
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
}
((PathGeometry)Data).Figures = pathFigures;
InvalidateGeometry();
}
protected void AddPolylinePoints(PathFigures 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)));
pathFigures.Add(figure);
}
}
}
}

View file

@ -2,8 +2,6 @@
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections.Generic;
using System.Linq;
#if WPF
using System.Windows;
using System.Windows.Media;
@ -84,8 +82,6 @@ namespace MapControl
MapPanel.SetLocation(this, Location);
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
protected Point? LocationToMap(Location location, double longitudeOffset)
{
if (longitudeOffset != 0d)
@ -140,34 +136,5 @@ namespace MapControl
return longitudeOffset;
}
protected PathFigureCollection GetPathFigures(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);
}
return pathFigures;
}
protected void AddMultiPolygonPoints(PathFigureCollection pathFigures, IEnumerable<IEnumerable<Location>> polygons)
{
if (parentMap != null && polygons != null)
{
var longitudeOffset = GetLongitudeOffset(Location);
foreach (var polygon in polygons)
{
AddPolylinePoints(pathFigures, polygon, longitudeOffset, true);
}
}
}
#endregion
}
}

View file

@ -5,15 +5,11 @@
using System.Collections.Generic;
#if WPF
using System.Windows;
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia.Media;
using DependencyProperty = Avalonia.AvaloniaProperty;
#endif
@ -22,16 +18,12 @@ namespace MapControl
/// <summary>
/// A polygon defined by a collection of Locations.
/// </summary>
public class MapPolygon : MapPath
public class MapPolygon : MapPolypoint
{
public static readonly DependencyProperty LocationsProperty =
DependencyPropertyHelper.Register<MapPolygon, IEnumerable<Location>>(nameof(Locations), null,
(polygon, oldValue, newValue) => polygon.DataCollectionPropertyChanged(oldValue, newValue));
public static readonly DependencyProperty FillRuleProperty =
DependencyPropertyHelper.Register<MapPolygon, FillRule>(nameof(FillRule), FillRule.EvenOdd,
(polygon, oldValue, newValue) => ((PathGeometry)polygon.Data).FillRule = newValue);
/// <summary>
/// Gets or sets the Locations that define the polygon points.
/// </summary>
@ -44,20 +36,9 @@ namespace MapControl
set => SetValue(LocationsProperty, value);
}
public FillRule FillRule
{
get => (FillRule)GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
public MapPolygon()
{
Data = new PathGeometry();
}
protected override void UpdateData()
{
SetPathFigures(GetPathFigures(Locations, true));
UpdateData(Locations, true);
}
}
}

View file

@ -5,15 +5,11 @@
using System.Collections.Generic;
#if WPF
using System.Windows;
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia.Media;
using DependencyProperty = Avalonia.AvaloniaProperty;
#endif
@ -22,16 +18,12 @@ namespace MapControl
/// <summary>
/// A polyline defined by a collection of Locations.
/// </summary>
public class MapPolyline : MapPath
public class MapPolyline : MapPolypoint
{
public static readonly DependencyProperty LocationsProperty =
DependencyPropertyHelper.Register<MapPolyline, IEnumerable<Location>>(nameof(Locations), null,
(polyline, oldValue, newValue) => polyline.DataCollectionPropertyChanged(oldValue, newValue));
public static readonly DependencyProperty FillRuleProperty =
DependencyPropertyHelper.Register<MapPolyline, FillRule>(nameof(FillRule), FillRule.EvenOdd,
(polyline, oldValue, newValue) => ((PathGeometry)polyline.Data).FillRule = newValue);
/// <summary>
/// Gets or sets the Locations that define the polyline points.
/// </summary>
@ -44,20 +36,9 @@ namespace MapControl
set => SetValue(LocationsProperty, value);
}
public FillRule FillRule
{
get => (FillRule)GetValue(FillRuleProperty);
set => SetValue(FillRuleProperty, value);
}
public MapPolyline()
{
Data = new PathGeometry();
}
protected override void UpdateData()
{
SetPathFigures(GetPathFigures(Locations, true));
UpdateData(Locations, false);
}
}
}

View file

@ -260,6 +260,9 @@
<Compile Include="..\WinUI\MapPath.WinUI.cs">
<Link>MapPath.WinUI.cs</Link>
</Compile>
<Compile Include="..\WinUI\MapPolypoint.WinUI.cs">
<Link>MapPolypoint.WinUI.cs</Link>
</Compile>
<Compile Include="..\WinUI\Matrix.WinUI.cs">
<Link>Matrix.WinUI.cs</Link>
</Compile>

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);
}
}
}
}

View file

@ -2,10 +2,6 @@
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
#if UWP
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
@ -35,109 +31,5 @@ namespace MapControl
Data.Transform = new MatrixTransform { Matrix = matrix };
}
}
#region Methods used only by derived classes MapPolyline and MapPolygon
protected void DataCollectionPropertyChanged(IEnumerable oldValue, IEnumerable newValue)
{
if (oldValue is INotifyCollectionChanged oldCollection)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
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);
if (closed)
{
var segment = new PolyLineSegment();
foreach (var point in points.Skip(1))
{
segment.Points.Add(point);
}
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
IsFilled = true
};
figure.Segments.Add(segment);
pathFigures.Add(figure);
}
else
{
var pointList = points.ToList();
if (closed)
{
pointList.Add(pointList[0]);
}
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
PathFigure figure = null;
PolyLineSegment segment = null;
for (int i = 1; i < pointList.Count; i++)
{
var p1 = pointList[i - 1];
var p2 = pointList[i];
var inside = Intersections.GetIntersections(ref p1, ref p2, viewport);
if (inside)
{
if (figure == null)
{
figure = new PathFigure
{
StartPoint = p1,
IsClosed = false,
IsFilled = true
};
segment = new PolyLineSegment();
figure.Segments.Add(segment);
pathFigures.Add(figure);
}
segment.Points.Add(p2);
}
if (!inside || p2 != pointList[i])
{
figure = null;
}
}
}
}
}
#endregion
}
}

View file

@ -0,0 +1,148 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#endif
namespace MapControl
{
/// <summary>
/// Base class of MapPolyline and MapPolygon.
/// </summary>
public class MapPolypoint : MapPath
{
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)
{
oldCollection.CollectionChanged -= DataCollectionChanged;
}
if (newValue is INotifyCollectionChanged newCollection)
{
newCollection.CollectionChanged += DataCollectionChanged;
}
UpdateData();
}
protected void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateData();
}
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 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);
if (closed)
{
var segment = new PolyLineSegment();
foreach (var point in points.Skip(1))
{
segment.Points.Add(point);
}
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = closed,
IsFilled = true
};
figure.Segments.Add(segment);
pathFigures.Add(figure);
}
else
{
var pointList = points.ToList();
if (closed)
{
pointList.Add(pointList[0]);
}
var viewport = new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height);
PathFigure figure = null;
PolyLineSegment segment = null;
for (int i = 1; i < pointList.Count; i++)
{
var p1 = pointList[i - 1];
var p2 = pointList[i];
var inside = Intersections.GetIntersections(ref p1, ref p2, viewport);
if (inside)
{
if (figure == null)
{
figure = new PathFigure
{
StartPoint = p1,
IsClosed = false,
IsFilled = true
};
segment = new PolyLineSegment();
figure.Segments.Add(segment);
pathFigures.Add(figure);
}
segment.Points.Add(p2);
}
if (!inside || p2 != pointList[i])
{
figure = null;
}
}
}
}
}
}
}