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

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