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

314 lines
11 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);
}
2026-02-07 00:01:04 +01:00
private void DrawGraticule(PathFigureCollection figures, List<Label> labels)
2022-04-27 18:00:40 +02:00
{
2026-01-24 17:42:00 +01:00
if (ParentMap.MapProjection.IsNormalCylindrical)
2022-04-27 18:00:40 +02:00
{
2026-02-07 00:01:04 +01:00
DrawNormalGraticule(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-07 00:01:04 +01:00
DrawNonNormalGraticule(figures, labels);
2022-04-27 18:00:40 +02:00
}
}
2026-02-07 00:01:04 +01:00
private void DrawNormalGraticule(PathFigureCollection figures, List<Label> labels)
2022-04-27 18:00:40 +02:00
{
2026-02-07 12:50:54 +01:00
var lineDistance = GetLineDistance(false);
var labelFormat = GetLabelFormat(lineDistance);
2026-02-07 13:47:33 +01:00
var mapRect = ParentMap.ViewTransform.ViewToMapBounds(new Rect(0d, 0d, ParentMap.ActualWidth, ParentMap.ActualHeight));
var southWest = ParentMap.MapProjection.MapToLocation(mapRect.X, mapRect.Y);
var northEast = ParentMap.MapProjection.MapToLocation(mapRect.X + mapRect.Width, mapRect.Y + mapRect.Height);
2026-01-30 22:26:51 +01:00
var latLabelStart = Math.Ceiling(southWest.Latitude / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(southWest.Longitude / 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-02-07 13:47:33 +01:00
AddLabel(labels, labelFormat, lat, lon, ParentMap.LocationToView(lat, lon), 0d);
2022-04-27 18:00:40 +02:00
}
}
}
2026-02-07 00:01:04 +01:00
private void DrawNonNormalGraticule(PathFigureCollection figures, List<Label> labels)
2022-04-27 18:00:40 +02:00
{
2026-02-06 11:59:57 +01:00
var lineDistance = GetLineDistance(true);
var labelFormat = GetLabelFormat(lineDistance);
2026-02-07 00:01:04 +01:00
var pointDistance = Math.Min(lineDistance, 1d);
var interpolationCount = (int)(lineDistance / pointDistance);
2026-02-07 12:50:54 +01:00
var center = Math.Round(ParentMap.Center.Longitude / lineDistance) * lineDistance;
var minLat = Math.Round(ParentMap.Center.Latitude / pointDistance) * pointDistance;
var maxLat = minLat;
var minLon = center;
var maxLon = center;
for (var lon = center;
lon >= center - 180d && DrawMeridian(figures, lon, pointDistance, ref minLat, ref maxLat);
2026-02-07 00:01:04 +01:00
lon -= lineDistance)
{
minLon = lon;
}
2026-02-01 01:42:24 +01:00
2026-02-07 12:50:54 +01:00
for (var lon = center + lineDistance;
lon < center + 180d && DrawMeridian(figures, lon, pointDistance, ref minLat, ref maxLat);
2026-02-07 00:01:04 +01:00
lon += lineDistance)
2026-02-01 01:42:24 +01:00
{
2026-02-07 00:01:04 +01:00
maxLon = lon;
2026-02-01 01:42:24 +01:00
}
2026-02-07 00:01:04 +01:00
if (minLon + 360d > maxLon)
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-07 00:01:04 +01:00
if (maxLon - 360d < minLon)
2026-02-01 01:42:24 +01:00
{
maxLon += lineDistance;
2022-04-27 18:00:40 +02:00
}
2026-02-07 12:50:54 +01:00
minLat = Math.Ceiling(minLat / lineDistance) * lineDistance;
maxLat = Math.Floor(maxLat / lineDistance) * lineDistance;
2022-04-27 18:00:40 +02:00
2026-02-07 00:01:04 +01:00
for (var lat = minLat; lat <= maxLat; lat += lineDistance)
2022-04-27 18:00:40 +02:00
{
var points = new List<Point>();
2026-02-07 12:50:54 +01:00
for (var lon = minLon; lon <= maxLon; lon += lineDistance)
2022-04-27 18:00:40 +02:00
{
2026-02-07 00:01:04 +01:00
var p = ParentMap.LocationToView(lat, lon);
points.Add(p);
2026-02-07 13:47:33 +01:00
AddLabel(labels, labelFormat, lat, lon, p, -ParentMap.MapProjection.GridConvergence(lat, lon));
2026-02-07 00:01:04 +01:00
for (int j = 1; j < interpolationCount; j++)
2022-04-27 18:00:40 +02:00
{
2026-02-07 00:01:04 +01:00
points.Add(ParentMap.LocationToView(lat, lon + j * pointDistance));
2022-04-27 18:00:40 +02:00
}
}
if (points.Count >= 2)
{
figures.Add(CreatePolylineFigure(points));
}
}
}
2026-02-07 00:01:04 +01:00
private bool DrawMeridian(PathFigureCollection figures, double lon,
double latStep, ref double minLat, ref double maxLat)
2022-04-27 18:00:40 +02:00
{
var points = new List<Point>();
var visible = false;
2026-02-07 00:01:04 +01:00
for (var lat = minLat + latStep; lat < maxLat; lat += latStep)
2022-04-27 18:00:40 +02:00
{
2026-02-07 00:01:04 +01:00
var p = ParentMap.LocationToView(lat, lon);
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-02-07 00:01:04 +01:00
for (var lat = minLat; lat >= -90d; lat -= latStep)
2026-01-30 22:26:51 +01:00
{
2026-02-07 00:01:04 +01:00
var p = ParentMap.LocationToView(lat, lon);
points.Insert(0, p);
2026-02-07 12:50:54 +01:00
2026-02-07 00:01:04 +01:00
if (!ParentMap.InsideViewBounds(p)) break;
2026-02-07 12:50:54 +01:00
2026-02-07 00:01:04 +01:00
minLat = lat;
visible = true;
2026-01-30 22:26:51 +01:00
}
2026-02-07 00:01:04 +01:00
for (var lat = maxLat; lat <= 90d; lat += latStep)
2026-01-30 22:26:51 +01:00
{
2026-02-07 00:01:04 +01:00
var p = ParentMap.LocationToView(lat, lon);
points.Add(p);
2026-02-07 12:50:54 +01:00
2026-02-07 00:01:04 +01:00
if (!ParentMap.InsideViewBounds(p)) break;
2026-02-07 12:50:54 +01:00
2026-02-07 00:01:04 +01:00
maxLat = lat;
visible = true;
2026-01-30 22:26:51 +01:00
}
2026-02-07 00:01:04 +01:00
if (visible && points.Count >= 2)
2022-04-28 18:34:30 +02:00
{
2026-02-07 00:01:04 +01:00
figures.Add(CreatePolylineFigure(points));
2022-04-28 18:34:30 +02:00
}
2026-01-13 09:06:48 +01:00
2026-02-07 00:01:04 +01:00
return visible;
2026-01-30 22:26:51 +01:00
}
2026-02-07 13:47:33 +01:00
private void AddLabel(List<Label> labels, string labelFormat, double lat, double lon, Point position, double rotation)
2026-01-30 22:26:51 +01:00
{
2026-02-07 00:01:04 +01:00
if (lat > -90d && lat < 90d && ParentMap.InsideViewBounds(position))
2026-01-30 22:26:51 +01:00
{
2026-02-07 13:47:33 +01:00
rotation = ((rotation + ParentMap.ViewTransform.Rotation) % 360d + 540d) % 360d - 180d;
2026-01-30 22:26:51 +01:00
if (rotation < -90d)
{
rotation += 180d;
}
else if (rotation > 90d)
{
rotation -= 180d;
}
labels.Add(new Label(
2026-02-07 00:01:04 +01:00
GetLabelText(lat, labelFormat, "NS"),
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
2026-01-30 22:26:51 +01:00
position.X, position.Y, rotation));
}
}
2026-02-06 11:59:57 +01:00
private double GetLineDistance(bool scaleByLatitude)
2026-02-01 01:42:24 +01:00
{
2026-02-06 11:59:57 +01:00
var minDistance = MinLineDistance / (ParentMap.ViewTransform.Scale * MapProjection.Wgs84MeterPerDegree);
if (scaleByLatitude)
{
minDistance /= Math.Cos(ParentMap.Center.Latitude * Math.PI / 180d);
}
2026-02-07 00:01:04 +01:00
2026-02-01 01:42:24 +01:00
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
}
}