Added MetricGrid

This commit is contained in:
ClemensFischer 2026-02-07 22:10:06 +01:00
parent 7857118712
commit 80252cbfd0
7 changed files with 292 additions and 184 deletions

View file

@ -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<MapGraticule>(ForegroundProperty);
AffectsRender<MapGrid>(ForegroundProperty);
}
public static readonly StyledProperty<IBrush> ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, IBrush>(TextElement.ForegroundProperty);
DependencyPropertyHelper.AddOwner<MapGrid, IBrush>(TextElement.ForegroundProperty);
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
DependencyPropertyHelper.AddOwner<MapGrid, FontFamily>(TextElement.FontFamilyProperty);
public static readonly StyledProperty<double> FontSizeProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, double>(TextElement.FontSizeProperty, 12d);
DependencyPropertyHelper.AddOwner<MapGrid, double>(TextElement.FontSizeProperty, 12d);
/// <summary>
/// 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));
}
}
}
}

View file

@ -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
{
/// <summary>
/// Draws a graticule overlay.
/// Draws a map graticule, i.e. a lat/lon grid overlay.
/// </summary>
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<MapGraticule, double>(nameof(MinLineDistance), 150d);
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(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];
/// <summary>
/// Minimum graticule line distance in pixels. The default value is 150.
/// </summary>
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<Label> labels)
protected override void DrawGrid(PathFigureCollection figures, List<Label> labels)
{
if (ParentMap.MapProjection.IsNormalCylindrical)
{
@ -87,10 +29,36 @@ namespace MapControl
}
else
{
DrawNonNormalGraticule(figures, labels);
DrawGraticule(figures, labels);
}
}
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];
private static string GetLabelFormat(double lineDistance)
{
return lineDistance < 1d / 60d ? "{0} {1}°{2:00}'{3:00}\"" :
lineDistance < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";
}
private double GetLineDistance(bool scaleByLatitude)
{
var minDistance = MinLineDistance / (ParentMap.ViewTransform.Scale * MapProjection.Wgs84MeterPerDegree);
if (scaleByLatitude)
{
minDistance /= Math.Cos(ParentMap.Center.Latitude * Math.PI / 180d);
}
minDistance = Math.Max(minDistance, lineDistances.First());
minDistance = Math.Min(minDistance, lineDistances.Last());
return lineDistances.First(d => d >= minDistance);
}
private void DrawNormalGraticule(PathFigureCollection figures, List<Label> labels)
{
var lineDistance = GetLineDistance(false);
@ -121,7 +89,7 @@ namespace MapControl
}
}
private void DrawNonNormalGraticule(PathFigureCollection figures, List<Label> labels)
private void DrawGraticule(PathFigureCollection figures, List<Label> labels)
{
var lineDistance = GetLineDistance(true);
var labelFormat = GetLabelFormat(lineDistance);
@ -241,74 +209,27 @@ namespace MapControl
rotation -= 180d;
}
labels.Add(new Label(
GetLabelText(lat, labelFormat, "NS"),
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
position.X, position.Y, rotation));
}
}
var text = GetLabelText(lat, labelFormat, "NS") +
"\n" + GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW");
private double GetLineDistance(bool scaleByLatitude)
{
var minDistance = MinLineDistance / (ParentMap.ViewTransform.Scale * MapProjection.Wgs84MeterPerDegree);
if (scaleByLatitude)
{
minDistance /= Math.Cos(ParentMap.Center.Latitude * Math.PI / 180d);
labels.Add(new Label(text, position.X, position.Y, rotation));
}
minDistance = Math.Max(minDistance, lineDistances.First());
minDistance = Math.Min(minDistance, lineDistances.Last());
return lineDistances.First(d => d >= minDistance);
}
private static string GetLabelFormat(double lineDistance)
{
return lineDistance < 1d / 60d ? "{0} {1}°{2:00}'{3:00}\"" :
lineDistance < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";
}
private static string GetLabelText(double value, string labelFormat, string hemispheres)
{
var hemisphere = hemispheres[0];
if (value < -1e-8) // ~1 mm
static string GetLabelText(double value, string labelFormat, string hemispheres)
{
value = -value;
hemisphere = hemispheres[1];
var hemisphere = hemispheres[0];
if (value < -1e-8) // ~1 mm
{
value = -value;
hemisphere = hemispheres[1];
}
var seconds = (int)Math.Round(value * 3600d);
return string.Format(CultureInfo.InvariantCulture,
labelFormat, hemisphere, seconds / 3600, seconds / 60 % 60, seconds % 60);
}
var seconds = (int)Math.Round(value * 3600d);
return string.Format(CultureInfo.InvariantCulture,
labelFormat, hemisphere, seconds / 3600, seconds / 60 % 60, seconds % 60);
}
private static PathFigure CreateLineFigure(Point p1, Point p2)
{
var figure = new PathFigure
{
StartPoint = p1,
IsClosed = false,
IsFilled = false
};
figure.Segments.Add(new LineSegment { Point = p2 });
return figure;
}
private static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
{
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = false,
IsFilled = false
};
figure.Segments.Add(CreatePolyLineSegment(points.Skip(1)));
return figure;
}
}
}

View file

@ -0,0 +1,101 @@
using System.Collections.Generic;
using System.Linq;
#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;
using Avalonia.Media;
using Brush = Avalonia.Media.IBrush;
using PathFigureCollection = Avalonia.Media.PathFigures;
#endif
namespace MapControl
{
/// <summary>
/// Base class of map grid or graticule overlays.
/// </summary>
public abstract partial class MapGrid
{
protected class Label(string text, double x, double y, double rotation)
{
public string Text => text;
public double X => x;
public double Y => y;
public double Rotation => rotation;
}
public static readonly DependencyProperty MinLineDistanceProperty =
DependencyPropertyHelper.Register<MapGrid, double>(nameof(MinLineDistance), 150d);
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyPropertyHelper.Register<MapGrid, double>(nameof(StrokeThickness), 0.5);
/// <summary>
/// Minimum graticule line distance in pixels. The default value is 150.
/// </summary>
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);
}
protected abstract void DrawGrid(PathFigureCollection figures, List<Label> labels);
protected static PathFigure CreateLineFigure(Point p1, Point p2)
{
var figure = new PathFigure
{
StartPoint = p1,
IsClosed = false,
IsFilled = false
};
figure.Segments.Add(new LineSegment { Point = p2 });
return figure;
}
protected static PathFigure CreatePolylineFigure(IEnumerable<Point> points)
{
var figure = new PathFigure
{
StartPoint = points.First(),
IsClosed = false,
IsFilled = false
};
figure.Segments.Add(CreatePolyLineSegment(points.Skip(1)));
return figure;
}
}
}

View file

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
#if WPF
using System.Windows;
using System.Windows.Media;
#elif UWP
using Windows.UI.Xaml.Media;
#elif WINUI
using Microsoft.UI.Xaml.Media;
#elif AVALONIA
using Avalonia;
using PathFigureCollection = Avalonia.Media.PathFigures;
#endif
namespace MapControl
{
/// <summary>
/// Draws a metric grid overlay.
/// </summary>
public partial class MetricGrid : MapGrid
{
protected override void DrawGrid(PathFigureCollection figures, List<Label> labels)
{
var minLineDistance = MinLineDistance / ParentMap.ViewTransform.Scale;
var lineDistance = Math.Pow(10d, Math.Ceiling(Math.Log10(minLineDistance)));
if (lineDistance * 0.5 >= minLineDistance)
{
lineDistance *= 0.5;
if (lineDistance * 0.4 >= minLineDistance)
{
lineDistance *= 0.4;
}
}
var mapRect = ParentMap.ViewTransform.ViewToMapBounds(new Rect(0d, 0d, ParentMap.ActualWidth, ParentMap.ActualHeight));
var minX = Math.Ceiling(mapRect.X / lineDistance) * lineDistance;
var minY = Math.Ceiling(mapRect.Y / lineDistance) * lineDistance;
for (var x = minX; x <= mapRect.X + mapRect.Width; x += lineDistance)
{
var p1 = ParentMap.ViewTransform.MapToView(new Point(x, mapRect.Y));
var p2 = ParentMap.ViewTransform.MapToView(new Point(x, mapRect.Y + mapRect.Height));
figures.Add(CreateLineFigure(p1, p2));
}
for (var y = minY; y <= mapRect.Y + mapRect.Height; y += lineDistance)
{
var p1 = ParentMap.ViewTransform.MapToView(new Point(mapRect.X, y));
var p2 = ParentMap.ViewTransform.MapToView(new Point(mapRect.X + mapRect.Width, y));
figures.Add(CreateLineFigure(p1, p2));
for (var x = minX; x <= mapRect.X + mapRect.Width; x += lineDistance)
{
AddLabel(labels, x, y);
}
}
}
private void AddLabel(List<Label> labels, double x, double y)
{
var position = ParentMap.ViewTransform.MapToView(new Point(x, y));
if (ParentMap.InsideViewBounds(position))
{
var rotation = ParentMap.ViewTransform.Rotation;
if (rotation < -90d)
{
rotation += 180d;
}
else if (rotation > 90d)
{
rotation -= 180d;
}
var text = string.Format("{0:F0}\n{1:F0}", y, x);
labels.Add(new Label(text, position.X, position.Y, rotation));
}
}
}
}

View file

@ -4,15 +4,18 @@ namespace MapControl
{
public class UtmProjection : TransverseMercatorProjection
{
public UtmProjection(string crsId, double equatorialRadius, double flattening, int utmZone, bool north = true)
public UtmProjection(string crsId, double equatorialRadius, double flattening, int zone, bool north = true)
: base(equatorialRadius, flattening)
{
CrsId = crsId;
ScaleFactor = 0.9996;
CentralMeridian = utmZone * 6d - 183d;
CentralMeridian = zone * 6 - 183;
FalseEasting = 5e5;
FalseNorthing = north ? 0d : 1e7;
Zone = zone;
}
public int Zone { get; }
}
/// <summary>
@ -28,8 +31,6 @@ namespace MapControl
public const int FirstZoneSouthEpsgCode = 32700 + FirstZone;
public const int LastZoneSouthEpsgCode = 32700 + LastZone;
public int Zone { get; }
public Wgs84UtmProjection(int zone, bool north)
: base($"EPSG:{(north ? 32600 : 32700) + zone}", Wgs84EquatorialRadius, Wgs84Flattening, zone, north)
{
@ -37,8 +38,6 @@ namespace MapControl
{
throw new ArgumentException($"Invalid WGS84 UTM zone {zone}.", nameof(zone));
}
Zone = zone;
}
}
@ -52,8 +51,6 @@ namespace MapControl
public const int FirstZoneEpsgCode = 25800 + FirstZone;
public const int LastZoneEpsgCode = 25800 + LastZone;
public int Zone { get; }
public Etrs89UtmProjection(int zone)
: base($"EPSG:{25800 + zone}", 6378137d, 1d / 298.257222101, zone) // GRS 1980
{
@ -61,8 +58,6 @@ namespace MapControl
{
throw new ArgumentException($"Invalid ETRS89 UTM zone {zone}.", nameof(zone));
}
Zone = zone;
}
}
@ -76,8 +71,6 @@ namespace MapControl
public const int FirstZoneEpsgCode = 26900 + FirstZone;
public const int LastZoneEpsgCode = 26900 + LastZone;
public int Zone { get; }
public Nad83UtmProjection(int zone)
: base($"EPSG:{26900 + zone}", 6378137d, 1d / 298.257222101, zone) // GRS 1980
{
@ -85,8 +78,6 @@ namespace MapControl
{
throw new ArgumentException($"Invalid NAD83 UTM zone {zone}.", nameof(zone));
}
Zone = zone;
}
}
}

View file

@ -6,16 +6,16 @@ using System.Windows.Media;
namespace MapControl
{
public partial class MapGraticule : FrameworkElement, IMapElement
public partial class MapGrid : FrameworkElement, IMapElement
{
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, Brush>(TextElement.ForegroundProperty);
DependencyPropertyHelper.AddOwner<MapGrid, Brush>(TextElement.ForegroundProperty);
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, FontFamily>(TextElement.FontFamilyProperty);
DependencyPropertyHelper.AddOwner<MapGrid, FontFamily>(TextElement.FontFamilyProperty);
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.AddOwner<MapGraticule, double>(TextElement.FontSizeProperty, 12d);
DependencyPropertyHelper.AddOwner<MapGrid, double>(TextElement.FontSizeProperty, 12d);
/// <summary>
/// Implements IMapElement.ParentMap.
@ -40,6 +40,11 @@ namespace MapControl
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
OnViewportChanged(e);
}
protected virtual void OnViewportChanged(ViewportChangedEventArgs e)
{
InvalidateVisual();
}
@ -56,7 +61,7 @@ namespace MapControl
Thickness = StrokeThickness,
};
DrawGraticule(pathGeometry.Figures, labels);
DrawGrid(pathGeometry.Figures, labels);
drawingContext.DrawGeometry(null, pen, pathGeometry);
@ -67,20 +72,21 @@ 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, pixelsPerDip);
var lonText = new FormattedText(label.LongitudeText,
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
var x = label.X + StrokeThickness / 2d + 2d;
var y1 = label.Y - StrokeThickness / 2d - latText.Height;
var y2 = label.Y + StrokeThickness / 2d;
var y = label.Y - text.Height / 2d;
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.X, label.Y));
drawingContext.DrawText(latText, new Point(x, y1));
drawingContext.DrawText(lonText, new Point(x, y2));
drawingContext.Pop();
if (label.Rotation != 0d)
{
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.X, label.Y));
drawingContext.DrawText(text, new Point(x, y));
drawingContext.Pop();
}
else
{
drawingContext.DrawText(text, new Point(x, y));
}
}
}
}

View file

@ -16,16 +16,16 @@ using Microsoft.UI.Xaml.Shapes;
namespace MapControl
{
public partial class MapGraticule : MapPanel
public partial class MapGrid : MapPanel
{
public static readonly DependencyProperty ForegroundProperty =
DependencyPropertyHelper.Register<MapGraticule, Brush>(nameof(Foreground));
DependencyPropertyHelper.Register<MapGrid, Brush>(nameof(Foreground));
public static readonly DependencyProperty FontFamilyProperty =
DependencyPropertyHelper.Register<MapGraticule, FontFamily>(nameof(FontFamily));
DependencyPropertyHelper.Register<MapGrid, FontFamily>(nameof(FontFamily));
public static readonly DependencyProperty FontSizeProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(FontSize), 12d);
DependencyPropertyHelper.Register<MapGrid, double>(nameof(FontSize), 12d);
protected override void SetParentMap(MapBase map)
{
@ -64,7 +64,7 @@ namespace MapControl
var figures = ((PathGeometry)path.Data).Figures;
figures.Clear();
DrawGraticule(figures, labels);
DrawGrid(figures, labels);
foreach (var label in labels)
{
@ -93,11 +93,10 @@ namespace MapControl
Children.Add(textBlock);
}
textBlock.Text = label.LatitudeText + "\n" + label.LongitudeText;
textBlock.Text = label.Text;
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var matrix = new Matrix(1, 0, 0, 1, 0, 0);
matrix.Translate(StrokeThickness / 2d + 2d, -textBlock.DesiredSize.Height / 2d);
matrix.Rotate(label.Rotation);
matrix.Translate(label.X, label.Y);