From 80252cbfd0ee115010319811c83e0a3834bc991c Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Sat, 7 Feb 2026 22:10:06 +0100 Subject: [PATCH] Added MetricGrid --- ...ticule.Avalonia.cs => MapGrid.Avalonia.cs} | 48 ++--- MapControl/Shared/MapGraticule.cs | 173 +++++------------- MapControl/Shared/MapGrid.cs | 101 ++++++++++ MapControl/Shared/MetricGrid.cs | 84 +++++++++ MapControl/Shared/UtmProjections.cs | 19 +- .../{MapGraticule.WPF.cs => MapGrid.WPF.cs} | 38 ++-- ...MapGraticule.WinUI.cs => MapGrid.WinUI.cs} | 13 +- 7 files changed, 292 insertions(+), 184 deletions(-) rename MapControl/Avalonia/{MapGraticule.Avalonia.cs => MapGrid.Avalonia.cs} (60%) create mode 100644 MapControl/Shared/MapGrid.cs create mode 100644 MapControl/Shared/MetricGrid.cs rename MapControl/WPF/{MapGraticule.WPF.cs => MapGrid.WPF.cs} (65%) rename MapControl/WinUI/{MapGraticule.WinUI.cs => MapGrid.WinUI.cs} (89%) diff --git a/MapControl/Avalonia/MapGraticule.Avalonia.cs b/MapControl/Avalonia/MapGrid.Avalonia.cs similarity index 60% rename from MapControl/Avalonia/MapGraticule.Avalonia.cs rename to MapControl/Avalonia/MapGrid.Avalonia.cs index a5277e40..06b882c6 100644 --- a/MapControl/Avalonia/MapGraticule.Avalonia.cs +++ b/MapControl/Avalonia/MapGrid.Avalonia.cs @@ -2,26 +2,27 @@ using Avalonia.Controls; using Avalonia.Controls.Documents; using Avalonia.Media; +using System; using System.Collections.Generic; using System.Globalization; namespace MapControl { - public partial class MapGraticule : Control, IMapElement + public partial class MapGrid : Control, IMapElement { - static MapGraticule() + static MapGrid() { - AffectsRender(ForegroundProperty); + AffectsRender(ForegroundProperty); } public static readonly StyledProperty ForegroundProperty = - DependencyPropertyHelper.AddOwner(TextElement.ForegroundProperty); + DependencyPropertyHelper.AddOwner(TextElement.ForegroundProperty); public static readonly StyledProperty FontFamilyProperty = - DependencyPropertyHelper.AddOwner(TextElement.FontFamilyProperty); + DependencyPropertyHelper.AddOwner(TextElement.FontFamilyProperty); public static readonly StyledProperty FontSizeProperty = - DependencyPropertyHelper.AddOwner(TextElement.FontSizeProperty, 12d); + DependencyPropertyHelper.AddOwner(TextElement.FontSizeProperty, 12d); /// /// Implements IMapElement.ParentMap. @@ -46,6 +47,11 @@ namespace MapControl } private void OnViewportChanged(object sender, ViewportChangedEventArgs e) + { + OnViewportChanged(e); + } + + protected virtual void OnViewportChanged(ViewportChangedEventArgs e) { InvalidateVisual(); } @@ -62,7 +68,7 @@ namespace MapControl Thickness = StrokeThickness, }; - DrawGraticule(pathGeometry.Figures, labels); + DrawGrid(pathGeometry.Figures, labels); drawingContext.DrawGeometry(null, pen, pathGeometry); @@ -72,24 +78,24 @@ namespace MapControl foreach (var label in labels) { - var latText = new FormattedText(label.LatitudeText, + var text = new FormattedText(label.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground); + var x = label.X + StrokeThickness / 2d + 2d; + var y = label.Y - text.Height / 2d; - var lonText = new FormattedText(label.LongitudeText, - CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground); + if (label.Rotation != 0d) + { + var transform = Avalonia.Matrix.CreateRotation( + label.Rotation * Math.PI / 180d, new Point(label.X, label.Y)); - var x = StrokeThickness / 2d + 2d; - var y1 = -StrokeThickness / 2d - latText.Height; - var y2 = StrokeThickness / 2d; + using var pushedState = drawingContext.PushTransform(transform); - var transform = new Matrix(1d, 0d, 0d, 1d, 0d, 0d); - transform.Rotate(label.Rotation); - transform.Translate(label.X, label.Y); - - using var pushState = drawingContext.PushTransform(transform); - - drawingContext.DrawText(latText, new Point(x, y1)); - drawingContext.DrawText(lonText, new Point(x, y2)); + drawingContext.DrawText(text, new Point(x, y)); + } + else + { + drawingContext.DrawText(text, new Point(x, y)); + } } } } diff --git a/MapControl/Shared/MapGraticule.cs b/MapControl/Shared/MapGraticule.cs index f0734027..c5e32a04 100644 --- a/MapControl/Shared/MapGraticule.cs +++ b/MapControl/Shared/MapGraticule.cs @@ -6,80 +6,22 @@ using System.Linq; 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; -using Avalonia.Media; -using Brush = Avalonia.Media.IBrush; using PathFigureCollection = Avalonia.Media.PathFigures; #endif namespace MapControl { /// - /// Draws a graticule overlay. + /// Draws a map graticule, i.e. a lat/lon grid overlay. /// - public partial class MapGraticule + public partial class MapGraticule : MapGrid { - private class Label(string latText, string lonText, double x, double y, double rotation) - { - public string LatitudeText => latText; - public string LongitudeText => lonText; - public double X => x; - public double Y => y; - public double Rotation => rotation; - } - - public static readonly DependencyProperty MinLineDistanceProperty = - DependencyPropertyHelper.Register(nameof(MinLineDistance), 150d); - - public static readonly DependencyProperty StrokeThicknessProperty = - DependencyPropertyHelper.Register(nameof(StrokeThickness), 0.5); - - private static readonly double[] lineDistances = [ - 1d/3600d, 1d/1800d, 1d/720d, 1d/360d, 1d/240d, 1d/120d, - 1d/60d, 1d/30d, 1d/12d, 1d/6d, 1d/4d, 1d/2d, - 1d, 2d, 5d, 10d, 15d, 30d]; - - - /// - /// Minimum graticule line distance in pixels. The default value is 150. - /// - public double MinLineDistance - { - get => (double)GetValue(MinLineDistanceProperty); - set => SetValue(MinLineDistanceProperty, value); - } - - public double StrokeThickness - { - get => (double)GetValue(StrokeThicknessProperty); - set => SetValue(StrokeThicknessProperty, value); - } - - public Brush Foreground - { - get => (Brush)GetValue(ForegroundProperty); - set => SetValue(ForegroundProperty, value); - } - - public FontFamily FontFamily - { - get => (FontFamily)GetValue(FontFamilyProperty); - set => SetValue(FontFamilyProperty, value); - } - - public double FontSize - { - get => (double)GetValue(FontSizeProperty); - set => SetValue(FontSizeProperty, value); - } - - private void DrawGraticule(PathFigureCollection figures, List