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

376 lines
13 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
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;
#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>
public partial class MapGraticule : MapOverlay
2012-04-25 22:02:53 +02:00
{
2022-04-27 18:00:40 +02:00
private class Label
{
2022-04-28 18:34:30 +02:00
public Label(string latText, string lonText, double x, double y, double rotation)
{
LatitudeText = latText;
LongitudeText = lonText;
X = x;
Y = y;
Rotation = rotation;
}
public string LatitudeText { get; }
public string LongitudeText { get; }
public double X { get; }
public double Y { get; }
public double Rotation { get; }
2022-04-27 18:00:40 +02:00
}
private const double LineInterpolationResolution = 2d;
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty MinLineDistanceProperty =
DependencyPropertyHelper.Register<MapGraticule, double>(nameof(MinLineDistance), 150d);
2022-04-27 18:00:40 +02:00
private double lineDistance;
private string labelFormat;
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
2022-04-27 18:00:40 +02:00
private void SetLineDistance()
{
2022-03-04 22:28:18 +01:00
var minDistance = MinLineDistance / PixelPerLongitudeDegree(ParentMap.Center);
var scale = minDistance < 1d / 60d ? 3600d : minDistance < 1d ? 60d : 1d;
minDistance *= scale;
var lineDistances = new double[] { 1d, 2d, 5d, 10d, 15d, 30d, 60d };
var i = 0;
while (i < lineDistances.Length - 1 && lineDistances[i] < minDistance)
{
i++;
}
2022-04-27 18:00:40 +02:00
lineDistance = Math.Min(lineDistances[i] / scale, 30d);
labelFormat = lineDistance < 1d / 60d ? "{0} {1}°{2:00}'{3:00}\""
: lineDistance < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";
2022-03-04 22:28:18 +01:00
}
private double PixelPerLongitudeDegree(Location location)
{
return Math.Max(1d, // a reasonable lower limit
ParentMap.GetScale(location).X *
Math.Cos(location.Latitude * Math.PI / 180d) * MapProjection.Wgs84MeterPerDegree);
}
2022-04-27 18:00:40 +02:00
private string GetLabelText(double value, string hemispheres)
2012-04-25 22:02:53 +02:00
{
var hemisphere = hemispheres[0];
2012-04-25 22:02:53 +02:00
2022-12-01 22:48:08 +01:00
if (value < -1e-8) // ~1 mm
2012-04-25 22:02:53 +02:00
{
value = -value;
hemisphere = hemispheres[1];
}
var seconds = (int)Math.Round(value * 3600d);
2012-04-25 22:02:53 +02:00
2021-07-05 00:04:07 +02:00
return string.Format(CultureInfo.InvariantCulture,
2022-04-27 18:00:40 +02:00
labelFormat, hemisphere, seconds / 3600, seconds / 60 % 60, seconds % 60);
}
2022-04-28 18:34:30 +02:00
private void AddLabel(ICollection<Label> labels, Location location, Point position, double? rotation = null)
2022-04-27 18:00:40 +02:00
{
if (position.X >= 0d && position.X <= ParentMap.RenderSize.Width &&
2022-04-27 18:00:40 +02:00
position.Y >= 0d && position.Y <= ParentMap.RenderSize.Height)
{
if (!rotation.HasValue)
{
2022-11-30 22:18:45 +01:00
// Get rotation from second location with same latitude.
2022-11-29 23:50:06 +01:00
//
var pos = ParentMap.LocationToView(
new Location(location.Latitude, location.Longitude + 10d / PixelPerLongitudeDegree(location)));
2022-04-27 18:00:40 +02:00
if (pos.HasValue)
2022-04-27 18:00:40 +02:00
{
rotation = Math.Atan2(pos.Value.Y - position.Y, pos.Value.X - position.X) * 180d / Math.PI;
2022-04-27 18:00:40 +02:00
}
}
if (rotation.HasValue)
{
2022-04-28 18:34:30 +02:00
labels.Add(new Label(
GetLabelText(location.Latitude, "NS"),
GetLabelText(Location.NormalizeLongitude(location.Longitude), "EW"),
position.X, position.Y, rotation.Value));
2022-04-27 18:00:40 +02:00
}
}
}
2022-04-28 18:34:30 +02:00
private ICollection<Label> DrawGraticule(PathFigureCollection figures)
2022-04-27 18:00:40 +02:00
{
2022-04-28 18:34:30 +02:00
var labels = new List<Label>();
2022-04-27 18:00:40 +02:00
2022-04-28 18:34:30 +02:00
figures.Clear();
2022-04-27 18:00:40 +02:00
2022-04-28 18:34:30 +02:00
SetLineDistance();
if (ParentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical)
2022-04-27 18:00:40 +02:00
{
2022-04-28 18:34:30 +02:00
DrawCylindricalGraticule(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
{
2022-04-28 18:34:30 +02: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
}
private void DrawCylindricalGraticule(PathFigureCollection figures, ICollection<Label> labels)
{
var bounds = ParentMap.ViewRectToBoundingBox(new Rect(0, 0, ParentMap.RenderSize.Width, ParentMap.RenderSize.Height));
var latLabelStart = Math.Ceiling(bounds.South / lineDistance) * lineDistance;
var lonLabelStart = Math.Ceiling(bounds.West / lineDistance) * lineDistance;
for (var lat = latLabelStart; lat <= bounds.North; lat += lineDistance)
{
var p1 = ParentMap.LocationToView(new Location(lat, bounds.West));
var p2 = ParentMap.LocationToView(new Location(lat, bounds.East));
if (p1.HasValue && p2.HasValue)
2022-04-27 18:00:40 +02:00
{
figures.Add(CreateLineFigure(p1.Value, p2.Value));
2022-04-27 18:00:40 +02:00
}
}
for (var lon = lonLabelStart; lon <= bounds.East; lon += lineDistance)
{
var p1 = ParentMap.LocationToView(new Location(bounds.South, lon));
var p2 = ParentMap.LocationToView(new Location(bounds.North, lon));
if (p1.HasValue && p2.HasValue)
2022-04-27 18:00:40 +02:00
{
figures.Add(CreateLineFigure(p1.Value, p2.Value));
2022-04-27 18:00:40 +02:00
}
2022-04-28 18:34:30 +02:00
for (var lat = latLabelStart; lat <= bounds.North; lat += lineDistance)
2022-04-27 18:00:40 +02:00
{
var location = new Location(lat, lon);
var position = ParentMap.LocationToView(location);
if (position.HasValue)
{
AddLabel(labels, location, position.Value, ParentMap.ViewTransform.Rotation);
}
2022-04-27 18:00:40 +02:00
}
}
}
private void DrawGraticule(PathFigureCollection figures, ICollection<Label> labels)
{
var minLat = 0d;
var maxLat = 0d;
GetLatitudeRange(lineDistance, ref minLat, ref maxLat);
var latSegments = (int)Math.Round(Math.Abs(maxLat - minLat) / lineDistance);
var interpolationCount = Math.Max(1, (int)Math.Ceiling(lineDistance / LineInterpolationResolution));
var interpolationDistance = lineDistance / interpolationCount;
var latPoints = latSegments * interpolationCount;
var centerLon = Math.Round(ParentMap.Center.Longitude / lineDistance) * lineDistance;
var westLimit = centerLon - 180d;
var eastLimit = centerLon + 180d;
if (ParentMap.MapProjection.Type == MapProjectionType.TransverseCylindrical)
{
westLimit = ParentMap.MapProjection.Center.Longitude - 15d;
eastLimit = ParentMap.MapProjection.Center.Longitude + 15d;
westLimit = Math.Floor(westLimit / lineDistance) * lineDistance;
eastLimit = Math.Ceiling(eastLimit / lineDistance) * lineDistance;
}
var minLon = centerLon - lineDistance;
var maxLon = centerLon + lineDistance;
if (DrawMeridian(figures, centerLon, minLat, interpolationDistance, latPoints))
{
while (DrawMeridian(figures, minLon, minLat, interpolationDistance, latPoints) &&
minLon > westLimit)
{
minLon -= lineDistance;
}
while (DrawMeridian(figures, maxLon, minLat, interpolationDistance, latPoints) &&
maxLon < eastLimit)
{
maxLon += lineDistance;
}
}
var lonSegments = (int)Math.Round(Math.Abs(maxLon - minLon) / lineDistance);
for (var i = 1; i < latSegments; i++)
{
var lat = minLat + i * lineDistance;
var lon = minLon;
var location = new Location(lat, lon);
var points = new List<Point>();
var position = ParentMap.LocationToView(location);
if (position.HasValue)
2022-04-27 18:00:40 +02:00
{
points.Add(position.Value);
AddLabel(labels, location, position.Value);
2022-04-27 18:00:40 +02:00
}
for (int j = 0; j < lonSegments; j++)
{
for (int k = 1; k <= interpolationCount; k++)
{
lon = minLon + j * lineDistance + k * interpolationDistance;
location = new Location(lat, lon);
position = ParentMap.LocationToView(location);
if (position.HasValue)
2022-04-27 18:00:40 +02:00
{
points.Add(position.Value);
2022-04-27 18:00:40 +02:00
}
}
if (position.HasValue)
{
AddLabel(labels, location, position.Value);
}
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(new Location(startLatitude + i * deltaLatitude, longitude));
if (p.HasValue)
2022-04-27 18:00:40 +02:00
{
visible = visible ||
p.Value.X >= 0d && p.Value.X <= ParentMap.RenderSize.Width &&
p.Value.Y >= 0d && p.Value.Y <= ParentMap.RenderSize.Height;
2022-04-27 18:00:40 +02:00
points.Add(p.Value);
2022-04-27 18:00:40 +02:00
}
}
if (points.Count >= 2)
{
figures.Add(CreatePolylineFigure(points));
}
return visible;
}
2022-04-28 18:34:30 +02:00
private void GetLatitudeRange(double lineDistance, ref double minLatitude, ref double maxLatitude)
{
var width = ParentMap.RenderSize.Width;
var height = ParentMap.RenderSize.Height;
var northPole = ParentMap.LocationToView(new Location(90d, 0d));
var southPole = ParentMap.LocationToView(new Location(-90d, 0d));
if (northPole.HasValue &&
northPole.Value.X >= 0d && northPole.Value.X <= width &&
northPole.Value.Y >= 0d && northPole.Value.Y <= height)
2022-04-28 18:34:30 +02:00
{
maxLatitude = 90d;
}
if (southPole.HasValue &&
southPole.Value.X >= 0d && southPole.Value.X <= width &&
southPole.Value.Y >= 0d && southPole.Value.Y <= height)
2022-04-28 18:34:30 +02:00
{
minLatitude = -90d;
}
if (minLatitude > -90d || maxLatitude < 90d)
{
var locations = new Location[]
{
ParentMap.ViewToLocation(new Point(0d, 0d)),
ParentMap.ViewToLocation(new Point(width / 2d, 0d)),
ParentMap.ViewToLocation(new Point(width, 0d)),
ParentMap.ViewToLocation(new Point(width, height / 2d)),
ParentMap.ViewToLocation(new Point(width, height)),
ParentMap.ViewToLocation(new Point(width / 2d, height)),
ParentMap.ViewToLocation(new Point(0d, height)),
ParentMap.ViewToLocation(new Point(0d, height / 2)),
};
var latitudes = locations.Where(loc => loc != null).Select(loc => loc.Latitude);
var south = -90d;
var north = 90d;
if (latitudes.Distinct().Count() >= 2)
{
south = latitudes.Min();
north = latitudes.Max();
}
if (minLatitude > -90d)
{
minLatitude = Math.Max(Math.Floor(south / lineDistance) * lineDistance, -90d);
}
if (maxLatitude < 90d)
{
maxLatitude = Math.Min(Math.Ceiling(north / lineDistance) * lineDistance, 90d);
}
}
}
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
}
}
}