mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Reworked MapGraticule
This commit is contained in:
parent
7c5eb04c9e
commit
53a291662d
3 changed files with 399 additions and 432 deletions
|
|
@ -3,13 +3,20 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
#if WINUI
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Windows.Foundation;
|
||||
#elif UWP
|
||||
using Windows.Foundation;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Media;
|
||||
#else
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
|
|
@ -19,9 +26,22 @@ namespace MapControl
|
|||
/// </summary>
|
||||
public partial class MapGraticule : MapOverlay
|
||||
{
|
||||
private class Label
|
||||
{
|
||||
public string LatitudeText { get; set; }
|
||||
public string LongitudeText { get; set; }
|
||||
public Point Position { get; set; }
|
||||
public double Rotation { get; set; }
|
||||
}
|
||||
|
||||
private const double LineInterpolationResolution = 2d;
|
||||
|
||||
public static readonly DependencyProperty MinLineDistanceProperty = DependencyProperty.Register(
|
||||
nameof(MinLineDistance), typeof(double), typeof(MapGraticule), new PropertyMetadata(150d));
|
||||
|
||||
private double lineDistance;
|
||||
private string labelFormat;
|
||||
|
||||
/// <summary>
|
||||
/// Minimum graticule line distance in pixels. The default value is 150.
|
||||
/// </summary>
|
||||
|
|
@ -31,7 +51,7 @@ namespace MapControl
|
|||
set { SetValue(MinLineDistanceProperty, value); }
|
||||
}
|
||||
|
||||
private double GetLineDistance()
|
||||
private void SetLineDistance()
|
||||
{
|
||||
var minDistance = MinLineDistance / PixelPerLongitudeDegree(ParentMap.Center);
|
||||
var scale = minDistance < 1d / 60d ? 3600d : minDistance < 1d ? 60d : 1d;
|
||||
|
|
@ -45,7 +65,10 @@ namespace MapControl
|
|||
i++;
|
||||
}
|
||||
|
||||
return Math.Min(lineDistances[i] / scale, 30d);
|
||||
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}°";
|
||||
}
|
||||
|
||||
private double PixelPerLongitudeDegree(Location location)
|
||||
|
|
@ -55,13 +78,7 @@ namespace MapControl
|
|||
Math.Cos(location.Latitude * Math.PI / 180d) * MapProjection.Wgs84MeterPerDegree);
|
||||
}
|
||||
|
||||
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 format, string hemispheres)
|
||||
private string GetLabelText(double value, string hemispheres)
|
||||
{
|
||||
var hemisphere = hemispheres[0];
|
||||
|
||||
|
|
@ -76,7 +93,252 @@ namespace MapControl
|
|||
var seconds = (int)Math.Round(value * 3600d);
|
||||
|
||||
return string.Format(CultureInfo.InvariantCulture,
|
||||
format, hemisphere, seconds / 3600, seconds / 60 % 60, seconds % 60);
|
||||
labelFormat, hemisphere, seconds / 3600, seconds / 60 % 60, seconds % 60);
|
||||
}
|
||||
|
||||
private void AddLabel(ICollection<Label> labels, Point position, Location location, double? rotation = null)
|
||||
{
|
||||
if (MapProjection.IsValid(position) &&
|
||||
position.X >= 0d && position.X <= ParentMap.RenderSize.Width &&
|
||||
position.Y >= 0d && position.Y <= ParentMap.RenderSize.Height)
|
||||
{
|
||||
if (!rotation.HasValue)
|
||||
{
|
||||
var pos = ParentMap.LocationToView(new Location(
|
||||
location.Latitude, location.Longitude + 10d / PixelPerLongitudeDegree(location)));
|
||||
|
||||
if (MapProjection.IsValid(pos))
|
||||
{
|
||||
rotation = Math.Atan2(pos.Y - position.Y, pos.X - position.X) * 180d / Math.PI;
|
||||
}
|
||||
}
|
||||
|
||||
if (rotation.HasValue)
|
||||
{
|
||||
labels.Add(new Label
|
||||
{
|
||||
LatitudeText = GetLabelText(location.Latitude, "NS"),
|
||||
LongitudeText = GetLabelText(Location.NormalizeLongitude(location.Longitude), "EW"),
|
||||
Position = position,
|
||||
Rotation = rotation.Value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.X >= 0d && northPole.Y >= 0d && northPole.X <= width && northPole.Y <= height)
|
||||
{
|
||||
maxLatitude = 90d;
|
||||
}
|
||||
|
||||
if (southPole.X >= 0d && southPole.Y >= 0d && southPole.X <= width && southPole.Y <= height)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 (MapProjection.IsValid(p1) && MapProjection.IsValid(p2))
|
||||
{
|
||||
figures.Add(CreateLineFigure(p1, p2));
|
||||
}
|
||||
}
|
||||
|
||||
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 (MapProjection.IsValid(p1) && MapProjection.IsValid(p2))
|
||||
{
|
||||
figures.Add(CreateLineFigure(p1, p2));
|
||||
}
|
||||
}
|
||||
|
||||
for (var lat = latLabelStart; lat <= bounds.North; lat += lineDistance)
|
||||
{
|
||||
for (var lon = lonLabelStart; lon <= bounds.East; lon += lineDistance)
|
||||
{
|
||||
var location = new Location(lat, lon);
|
||||
var position = ParentMap.LocationToView(location);
|
||||
|
||||
AddLabel(labels, position, location, ParentMap.ViewTransform.Rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 (MapProjection.IsValid(position))
|
||||
{
|
||||
points.Add(position);
|
||||
AddLabel(labels, position, location);
|
||||
}
|
||||
|
||||
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 (MapProjection.IsValid(position))
|
||||
{
|
||||
points.Add(position);
|
||||
}
|
||||
}
|
||||
|
||||
AddLabel(labels, position, location);
|
||||
}
|
||||
|
||||
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 (MapProjection.IsValid(p))
|
||||
{
|
||||
visible = visible ||
|
||||
p.X >= 0d && p.X <= ParentMap.RenderSize.Width &&
|
||||
p.Y >= 0d && p.Y <= ParentMap.RenderSize.Height;
|
||||
|
||||
points.Add(p);
|
||||
}
|
||||
}
|
||||
|
||||
if (points.Count >= 2)
|
||||
{
|
||||
figures.Add(CreatePolylineFigure(points));
|
||||
}
|
||||
|
||||
return visible;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue