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

83 lines
2.6 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
2012-05-04 12:52:20 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2021-07-05 00:04:07 +02:00
using System.Globalization;
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml;
#else
2012-04-25 22:02:53 +02:00
using System.Windows;
#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
{
public static readonly DependencyProperty MinLineDistanceProperty = DependencyProperty.Register(
nameof(MinLineDistance), typeof(double), typeof(MapGraticule), new PropertyMetadata(150d));
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
{
get { return (double)GetValue(MinLineDistanceProperty); }
set { SetValue(MinLineDistanceProperty, value); }
}
2012-04-25 22:02:53 +02:00
private double GetLineDistance()
{
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-03-04 22:28:18 +01:00
return Math.Min(lineDistances[i] / scale, 30d);
}
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);
}
private static string GetLabelFormat(double lineDistance)
2012-04-25 22:02:53 +02:00
{
2021-07-05 00:04:07 +02:00
return lineDistance < 1d / 60d ? "{0} {1}°{2:00}'{3:00}\""
: lineDistance < 1d ? "{0} {1}°{2:00}'" : "{0} {1}°";
2012-04-25 22:02:53 +02:00
}
private static string GetLabelText(double value, string format, string hemispheres)
2012-04-25 22:02:53 +02:00
{
var hemisphere = hemispheres[0];
2012-04-25 22:02:53 +02:00
2022-03-04 22:28:18 +01:00
value = (value + 540d) % 360d - 180d;
2012-04-25 22:02:53 +02:00
if (value < -1e-8) // ~1mm
{
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,
format, hemisphere, seconds / 3600, seconds / 60 % 60, seconds % 60);
2012-04-25 22:02:53 +02:00
}
}
}