XAML-Map-Control/MapControl/Shared/MapGraticule.cs

337 lines
12 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2022-04-27 18:00:40 +02:00
using System.Collections.Generic;
2021-07-05 00:04:07 +02:00
using System.Globalization;
2022-04-27 18:00:40 +02:00
using System.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml;
2022-04-27 18:00:40 +02:00
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
2025-08-19 19:43:02 +02:00
#elif AVALONIA
using Avalonia;
using Avalonia.Media;
2025-11-14 16:56:11 +01:00
using Brush = Avalonia.Media.IBrush;
2025-08-19 19:43:02 +02:00
using PathFigureCollection = Avalonia.Media.PathFigures;
#endif
2012-04-25 22:02:53 +02:00
namespace MapControl
{
2012-05-04 12:52:20 +02:00
/// <summary>
2012-10-25 08:42:51 +02:00
/// Draws a graticule overlay.
2012-05-04 12:52:20 +02:00
/// </summary>
2024-05-26 20:32:29 +02:00
public partial class MapGraticule
2012-04-25 22:02:53 +02:00
{
2025-09-15 17:46:31 +02:00
private class Label(string latText, string lonText, double x, double y, double rotation)
2022-04-27 18:00:40 +02:00
{
2025-11-26 23:34:22 +01:00
public string LatitudeText => latText;
public string LongitudeText => lonText;
public double X => x;
public double Y => y;
public double Rotation => rotation;
2022-04-27 18:00:40 +02:00
}
2024-05-23 18:08:14 +02:00
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);
2022-04-27 18:00:40 +02:00
2026-02-01 01:42:24 +01:00
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];
2012-10-25 08:42:51 +02:00
/// <summary>
/// Minimum graticule line distance in pixels. The default value is 150.
2012-10-25 08:42:51 +02:00
/// </summary>
public double MinLineDistance
{
2022-08-06 10:40:59 +02:00
get => (double)GetValue(MinLineDistanceProperty);
set => SetValue(MinLineDistanceProperty, value);
}
2012-04-25 22:02:53 +02:00
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);
}
2025-06-10 09:13:48 +02:00
private List<Label> DrawGraticule(PathFigureCollection figures)
2022-04-27 18:00:40 +02:00
{
2026-01-30 22:26:51 +01:00
var labels = new List<Label>();
2026-02-01 01:42:24 +01:00
figures.Clear();
2022-04-28 18:34:30 +02:00
2026-01-24 17:42:00 +01:00
if (ParentMap.MapProjection.IsNormalCylindrical)
2022-04-27 18:00:40 +02:00
{
2026-02-01 01:42:24 +01:00
DrawNormalCylindrical(figures, labels);
2022-04-27 18:00:40 +02:00
}
2022-04-28 18:34:30 +02:00
else
2022-04-27 18:00:40 +02:00
{
2026-02-01 01:42:24 +01:00
DrawGraticule(figures, labels);
2022-04-27 18:00:40 +02:00
}
2022-04-28 18:34:30 +02:00
return labels;
2022-04-27 18:00:40 +02:00
}
2026-02-01 01:42:24 +01:00
private void DrawNormalCylindrical(PathFigureCollection figures, List<Label> labels)
2022-04-27 18:00:40 +02:00
{
2026-01-30 22:26:51 +01:00
var southWest = ParentMap.ViewToLocation(new Point(0d, ParentMap.ActualHeight));
var northEast = ParentMap.ViewToLocation(new Point(ParentMap.ActualWidth, 0d));
2026-02-01 01:42:24 +01:00
var lineDistance = GetLineDistance(MinLineDistance /
(ParentMap.ViewTransform.Scale * MapProjection.Wgs84MeterPerDegree));
2026-01-30 22:26:51 +01:00
var latLabelStart = Math.Ceiling(southWest.Latitude / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(southWest.Longitude / lineDistance) * lineDistance;
2026-02-01 01:42:24 +01:00
var labelFormat = GetLabelFormat(Math.Min(lineDistance, lineDistance));
2022-04-27 18:00:40 +02:00
2026-01-30 22:26:51 +01:00
for (var lat = latLabelStart; lat <= northEast.Latitude; lat += lineDistance)
2022-04-27 18:00:40 +02:00
{
2026-01-30 22:26:51 +01:00
var p1 = ParentMap.LocationToView(lat, southWest.Longitude);
var p2 = ParentMap.LocationToView(lat, northEast.Longitude);
figures.Add(CreateLineFigure(p1, p2));
2022-04-27 18:00:40 +02:00
}
2026-01-30 22:26:51 +01:00
for (var lon = lonLabelStart; lon <= northEast.Longitude; lon += lineDistance)
2022-04-27 18:00:40 +02:00
{
2026-01-30 22:26:51 +01:00
var p1 = ParentMap.LocationToView(southWest.Latitude, lon);
var p2 = ParentMap.LocationToView(northEast.Latitude, lon);
figures.Add(CreateLineFigure(p1, p2));
2022-04-27 18:00:40 +02:00
2026-01-30 22:26:51 +01:00
for (var lat = latLabelStart; lat <= northEast.Latitude; lat += lineDistance)
2022-04-27 18:00:40 +02:00
{
2026-01-30 22:26:51 +01:00
AddLabel(labels, labelFormat, lat, lon, ParentMap.LocationToView(lat, lon));
2022-04-27 18:00:40 +02:00
}
}
}
2026-02-01 01:42:24 +01:00
private void DrawGraticule(PathFigureCollection figures, List<Label> labels)
2022-04-27 18:00:40 +02:00
{
2026-02-01 01:42:24 +01:00
var lineDistance = GetLineDistance(MinLineDistance /
(ParentMap.ViewTransform.Scale * MapProjection.Wgs84MeterPerDegree * Math.Cos(ParentMap.Center.Latitude * Math.PI / 180d)));
var labelFormat = GetLabelFormat(Math.Min(lineDistance, lineDistance));
2026-01-13 09:06:48 +01:00
GetLatitudeRange(lineDistance, out double minLat, out double maxLat);
2022-04-27 18:00:40 +02:00
2026-02-01 01:42:24 +01:00
var latSegments = (int)Math.Ceiling(Math.Abs(maxLat - minLat) / lineDistance);
2026-01-30 22:26:51 +01:00
var interpolationCount = Math.Max(1, (int)Math.Ceiling(lineDistance));
2026-02-01 01:42:24 +01:00
var interpolationStep = lineDistance / interpolationCount;
var latStep = lineDistance;
var startLat = minLat;
if (Math.Abs(minLat) > Math.Abs(maxLat))
{
startLat = maxLat;
latStep = -lineDistance;
}
2022-04-27 18:00:40 +02:00
var centerLon = Math.Round(ParentMap.Center.Longitude / lineDistance) * lineDistance;
2026-02-01 01:42:24 +01:00
var minLon = centerLon;
2022-04-27 18:00:40 +02:00
var maxLon = centerLon + lineDistance;
2026-02-01 01:42:24 +01:00
while (DrawMeridian(figures, minLon, minLat, interpolationStep, latSegments * interpolationCount) &&
minLon > centerLon - 180d)
2022-04-27 18:00:40 +02:00
{
2026-02-01 01:42:24 +01:00
minLon -= lineDistance;
}
2022-04-27 18:00:40 +02:00
2026-02-01 01:42:24 +01:00
while (DrawMeridian(figures, maxLon, minLat, interpolationStep, latSegments * interpolationCount) &&
maxLon < centerLon + 180d)
{
maxLon += lineDistance;
2022-04-27 18:00:40 +02:00
}
var lonSegments = (int)Math.Round(Math.Abs(maxLon - minLon) / lineDistance);
for (var i = 1; i < latSegments; i++)
{
2026-02-01 01:42:24 +01:00
var lat = startLat + i * latStep;
2022-04-27 18:00:40 +02:00
var lon = minLon;
var points = new List<Point>();
2026-02-01 01:42:24 +01:00
var position = ParentMap.LocationToView(lat, lon);
var rotation = -ParentMap.MapProjection.GridConvergence(lat, lon);
2022-04-27 18:00:40 +02:00
points.Add(position);
2026-01-30 22:26:51 +01:00
AddLabel(labels, labelFormat, lat, lon, position, rotation);
2022-04-27 18:00:40 +02:00
for (int j = 0; j < lonSegments; j++)
{
for (int k = 1; k <= interpolationCount; k++)
{
2026-02-01 01:42:24 +01:00
lon = minLon + j * lineDistance + k * interpolationStep;
position = ParentMap.LocationToView(lat, lon);
points.Add(position);
2022-04-27 18:00:40 +02:00
}
2026-02-01 01:42:24 +01:00
rotation = -ParentMap.MapProjection.GridConvergence(lat, lon);
2026-01-30 22:26:51 +01:00
AddLabel(labels, labelFormat, lat, lon, position, rotation);
2022-04-27 18:00:40 +02:00
}
if (points.Count >= 2)
{
figures.Add(CreatePolylineFigure(points));
}
}
}
private bool DrawMeridian(PathFigureCollection figures,
double longitude, double startLatitude, double deltaLatitude, int numPoints)
{
var points = new List<Point>();
var visible = false;
for (int i = 0; i <= numPoints; i++)
{
var p = ParentMap.LocationToView(startLatitude + i * deltaLatitude, longitude);
points.Add(p);
2026-01-31 00:07:40 +01:00
visible = visible || ParentMap.InsideViewBounds(p);
2022-04-27 18:00:40 +02:00
}
2026-01-30 22:26:51 +01:00
if (visible && points.Count >= 2)
2022-04-27 18:00:40 +02:00
{
figures.Add(CreatePolylineFigure(points));
}
return visible;
}
2026-01-13 09:06:48 +01:00
private void GetLatitudeRange(double lineDistance, out double minLatitude, out double maxLatitude)
2022-04-28 18:34:30 +02:00
{
2026-01-31 00:07:40 +01:00
var minLat = 90d;
var maxLat = -90d;
if (ParentMap.InsideViewBounds(ParentMap.LocationToView(90d, 0d)))
2026-01-30 22:26:51 +01:00
{
2026-01-31 00:07:40 +01:00
maxLat = 90d;
2026-01-30 22:26:51 +01:00
}
2026-01-31 00:07:40 +01:00
if (ParentMap.InsideViewBounds(ParentMap.LocationToView(-90d, 0d)))
2026-01-30 22:26:51 +01:00
{
2026-01-31 00:07:40 +01:00
minLat = -90d;
2026-01-30 22:26:51 +01:00
}
2026-01-31 00:07:40 +01:00
if (minLat > -90d || maxLat < 90d)
2022-04-28 18:34:30 +02:00
{
2026-01-31 00:07:40 +01:00
var locations = new Location[]
{
ParentMap.ViewToLocation(new Point(0d, 0d)),
2026-02-01 01:42:24 +01:00
ParentMap.ViewToLocation(new Point(ParentMap.ActualWidth, 0d)),
ParentMap.ViewToLocation(new Point(0d, ParentMap.ActualHeight)),
ParentMap.ViewToLocation(new Point(ParentMap.ActualWidth, ParentMap.ActualHeight)),
ParentMap.ViewToLocation(new Point(ParentMap.ActualWidth / 2d, 0d)),
ParentMap.ViewToLocation(new Point(ParentMap.ActualWidth / 2d, ParentMap.ActualHeight)),
ParentMap.ViewToLocation(new Point(0d, ParentMap.ActualHeight / 2d)),
ParentMap.ViewToLocation(new Point(ParentMap.ActualWidth, ParentMap.ActualHeight / 2d)),
2026-01-31 00:07:40 +01:00
};
var latitudes = locations.Select(loc => loc.Latitude).Distinct();
minLat = Math.Min(minLat, latitudes.Min());
maxLat = Math.Max(maxLat, latitudes.Max());
2022-04-28 18:34:30 +02:00
}
2026-01-13 09:06:48 +01:00
2026-02-01 01:42:24 +01:00
minLatitude = Math.Max(Math.Floor(minLat / lineDistance) * lineDistance, -90d);
maxLatitude = Math.Min(Math.Ceiling(maxLat / lineDistance) * lineDistance, 90d);
2026-01-30 22:26:51 +01:00
}
private void AddLabel(List<Label> labels, string labelFormat, double latitude, double longitude, Point position, double rotation = 0d)
{
2026-01-31 00:07:40 +01:00
if (ParentMap.InsideViewBounds(position))
2026-01-30 22:26:51 +01:00
{
rotation = (rotation + ParentMap.ViewTransform.Rotation) % 360d;
if (rotation < -90d)
{
rotation += 180d;
}
else if (rotation > 90d)
{
rotation -= 180d;
}
labels.Add(new Label(
GetLabelText(latitude, labelFormat, "NS"),
GetLabelText(Location.NormalizeLongitude(longitude), labelFormat, "EW"),
position.X, position.Y, rotation));
}
}
2026-02-01 01:42:24 +01:00
private static double GetLineDistance(double minDistance)
{
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}°";
}
2026-01-30 22:26:51 +01:00
private static string GetLabelText(double value, string labelFormat, string hemispheres)
{
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);
2022-04-28 18:34:30 +02:00
}
2022-04-27 18:00:40 +02:00
private static PathFigure CreateLineFigure(Point p1, Point p2)
{
var figure = new PathFigure
{
StartPoint = p1,
IsFilled = false
};
figure.Segments.Add(new LineSegment { Point = p2 });
return figure;
2012-04-25 22:02:53 +02:00
}
2026-02-01 01:42:24 +01:00
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;
}
2012-04-25 22:02:53 +02:00
}
}