mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Reworked MapGraticule
This commit is contained in:
parent
7c5eb04c9e
commit
53a291662d
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
// © 2022 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
|
@ -13,25 +12,13 @@ namespace MapControl
|
|||
{
|
||||
public partial class MapGraticule
|
||||
{
|
||||
private const double LineInterpolationResolution = 2d;
|
||||
|
||||
private class Label
|
||||
{
|
||||
public readonly double Position;
|
||||
public readonly FormattedText Text;
|
||||
|
||||
public Label(double position, FormattedText text)
|
||||
{
|
||||
Position = position;
|
||||
Text = text;
|
||||
}
|
||||
}
|
||||
|
||||
static MapGraticule()
|
||||
{
|
||||
StrokeThicknessProperty.OverrideMetadata(typeof(MapGraticule), new FrameworkPropertyMetadata(0.5));
|
||||
}
|
||||
|
||||
private readonly PathGeometry pathGeometry = new PathGeometry();
|
||||
|
||||
protected override void OnViewportChanged(ViewportChangedEventArgs e)
|
||||
{
|
||||
InvalidateVisual();
|
||||
|
|
@ -43,289 +30,54 @@ namespace MapControl
|
|||
|
||||
if (projection != null)
|
||||
{
|
||||
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
||||
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
|
||||
var pathGeometry = new PathGeometry();
|
||||
var labels = new List<Label>();
|
||||
|
||||
SetLineDistance();
|
||||
|
||||
if (projection.Type <= MapProjectionType.NormalCylindrical)
|
||||
{
|
||||
DrawCylindricalGraticule(drawingContext);
|
||||
DrawCylindricalGraticule(pathGeometry.Figures, labels);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawGraticule(drawingContext);
|
||||
DrawGraticule(pathGeometry.Figures, labels);
|
||||
}
|
||||
|
||||
drawingContext.DrawGeometry(null, CreatePen(), pathGeometry);
|
||||
|
||||
foreach (var label in labels)
|
||||
{
|
||||
var latText = new FormattedText(label.LatitudeText,
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
|
||||
|
||||
var lonText = new FormattedText(label.LongitudeText,
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
|
||||
|
||||
var x = label.Position.X + StrokeThickness / 2d + 2d;
|
||||
var y1 = label.Position.Y - StrokeThickness / 2d - latText.Height;
|
||||
var y2 = label.Position.Y + StrokeThickness / 2d;
|
||||
|
||||
drawingContext.PushTransform(new RotateTransform(label.Rotation, label.Position.X, label.Position.Y));
|
||||
drawingContext.DrawText(latText, new Point(x, y1));
|
||||
drawingContext.DrawText(lonText, new Point(x, y2));
|
||||
drawingContext.Pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawCylindricalGraticule(DrawingContext drawingContext)
|
||||
private static PathFigure CreatePolylineFigure(ICollection<Point> points)
|
||||
{
|
||||
var path = new PathGeometry();
|
||||
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
||||
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
|
||||
var lineDistance = GetLineDistance();
|
||||
var labelFormat = GetLabelFormat(lineDistance);
|
||||
|
||||
var boundingBox = ParentMap.ViewRectToBoundingBox(new Rect(ParentMap.RenderSize));
|
||||
var latLabelStart = Math.Ceiling(boundingBox.South / lineDistance) * lineDistance;
|
||||
var lonLabelStart = Math.Ceiling(boundingBox.West / lineDistance) * lineDistance;
|
||||
var latLabels = new List<Label>((int)((boundingBox.North - latLabelStart) / lineDistance) + 1);
|
||||
var lonLabels = new List<Label>((int)((boundingBox.East - lonLabelStart) / lineDistance) + 1);
|
||||
|
||||
for (var lat = latLabelStart; lat <= boundingBox.North; lat += lineDistance)
|
||||
var figure = new PathFigure
|
||||
{
|
||||
latLabels.Add(new Label(lat, new FormattedText(
|
||||
GetLabelText(lat, labelFormat, "NS"),
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip)));
|
||||
StartPoint = points.First(),
|
||||
IsFilled = false
|
||||
};
|
||||
|
||||
var p1 = ParentMap.LocationToView(new Location(lat, boundingBox.West));
|
||||
var p2 = ParentMap.LocationToView(new Location(lat, boundingBox.East));
|
||||
|
||||
if (MapProjection.IsValid(p1) && MapProjection.IsValid(p2))
|
||||
{
|
||||
var figure = new PathFigure { StartPoint = p1 };
|
||||
figure.Segments.Add(new LineSegment(p2, true));
|
||||
path.Figures.Add(figure);
|
||||
}
|
||||
}
|
||||
|
||||
for (var lon = lonLabelStart; lon <= boundingBox.East; lon += lineDistance)
|
||||
{
|
||||
lonLabels.Add(new Label(lon, new FormattedText(
|
||||
GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW"),
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip)));
|
||||
|
||||
var p1 = ParentMap.LocationToView(new Location(boundingBox.South, lon));
|
||||
var p2 = ParentMap.LocationToView(new Location(boundingBox.North, lon));
|
||||
|
||||
if (MapProjection.IsValid(p1) && MapProjection.IsValid(p2))
|
||||
{
|
||||
var figure = new PathFigure { StartPoint = p1 };
|
||||
figure.Segments.Add(new LineSegment(p2, true));
|
||||
path.Figures.Add(figure);
|
||||
}
|
||||
}
|
||||
|
||||
drawingContext.DrawGeometry(null, CreatePen(), path);
|
||||
|
||||
foreach (var latLabel in latLabels)
|
||||
{
|
||||
foreach (var lonLabel in lonLabels)
|
||||
{
|
||||
var position = ParentMap.LocationToView(new Location(latLabel.Position, lonLabel.Position));
|
||||
|
||||
if (MapProjection.IsValid(position))
|
||||
{
|
||||
DrawLabel(drawingContext, latLabel.Text, lonLabel.Text, position, ParentMap.ViewTransform.Rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawGraticule(DrawingContext drawingContext)
|
||||
{
|
||||
var path = new PathGeometry();
|
||||
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
||||
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
|
||||
var lineDistance = GetLineDistance();
|
||||
var labelFormat = GetLabelFormat(lineDistance);
|
||||
|
||||
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(path.Figures, centerLon, minLat, interpolationDistance, latPoints))
|
||||
{
|
||||
while (DrawMeridian(path.Figures, minLon, minLat, interpolationDistance, latPoints) &&
|
||||
minLon > westLimit)
|
||||
{
|
||||
minLon -= lineDistance;
|
||||
}
|
||||
|
||||
while (DrawMeridian(path.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 p = ParentMap.LocationToView(location);
|
||||
|
||||
if (MapProjection.IsValid(p))
|
||||
{
|
||||
points.Add(p);
|
||||
DrawLabel(drawingContext, typeface, pixelsPerDip, p, location, labelFormat);
|
||||
}
|
||||
|
||||
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);
|
||||
p = ParentMap.LocationToView(location);
|
||||
|
||||
if (MapProjection.IsValid(p))
|
||||
{
|
||||
points.Add(p);
|
||||
}
|
||||
}
|
||||
|
||||
DrawLabel(drawingContext, typeface, pixelsPerDip, p, location, labelFormat);
|
||||
}
|
||||
|
||||
if (points.Count >= 2)
|
||||
{
|
||||
var figure = new PathFigure { StartPoint = points.First() };
|
||||
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
|
||||
path.Figures.Add(figure);
|
||||
}
|
||||
}
|
||||
|
||||
drawingContext.DrawGeometry(null, CreatePen(), path);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var figure = new PathFigure { StartPoint = points.First() };
|
||||
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
|
||||
figures.Add(figure);
|
||||
}
|
||||
|
||||
return visible;
|
||||
}
|
||||
|
||||
private void DrawLabel(DrawingContext drawingContext, Typeface typeface, double pixelsPerDip,
|
||||
Point position, Location location, string labelFormat)
|
||||
{
|
||||
if (position.X >= 0d && position.X <= ParentMap.RenderSize.Width &&
|
||||
position.Y >= 0d && position.Y <= ParentMap.RenderSize.Height)
|
||||
{
|
||||
var latText = new FormattedText(GetLabelText(location.Latitude, labelFormat, "NS"),
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
|
||||
var lonText = new FormattedText(GetLabelText(location.Longitude, labelFormat, "EW"),
|
||||
CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeface, FontSize, Foreground, pixelsPerDip);
|
||||
|
||||
location.Longitude += latText.Width / PixelPerLongitudeDegree(location);
|
||||
|
||||
var p = ParentMap.LocationToView(location);
|
||||
|
||||
if (MapProjection.IsValid(p))
|
||||
{
|
||||
DrawLabel(drawingContext, latText, lonText, position, Vector.AngleBetween(new Vector(1d, 0d), p - position));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawLabel(DrawingContext drawingContext,
|
||||
FormattedText latitudeLabel, FormattedText longitudeLabel, Point position, double rotation)
|
||||
{
|
||||
var x = position.X + StrokeThickness / 2d + 2d;
|
||||
var y1 = position.Y - StrokeThickness / 2d - latitudeLabel.Height;
|
||||
var y2 = position.Y + StrokeThickness / 2d;
|
||||
|
||||
drawingContext.PushTransform(new RotateTransform(rotation, position.X, position.Y));
|
||||
drawingContext.DrawText(latitudeLabel, new Point(x, y1));
|
||||
drawingContext.DrawText(longitudeLabel, new Point(x, y2));
|
||||
drawingContext.Pop();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
figure.Segments.Add(new PolyLineSegment(points.Skip(1), true));
|
||||
return figure;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
// © 2022 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using Windows.Foundation;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
#if WINUI
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
|
|
@ -18,7 +19,7 @@ namespace MapControl
|
|||
{
|
||||
public partial class MapGraticule
|
||||
{
|
||||
private Path path;
|
||||
private readonly Path path = new Path { Data = new PathGeometry() };
|
||||
|
||||
public MapGraticule()
|
||||
{
|
||||
|
|
@ -27,147 +28,99 @@ namespace MapControl
|
|||
|
||||
protected override void OnViewportChanged(ViewportChangedEventArgs e)
|
||||
{
|
||||
var map = ParentMap;
|
||||
var projection = map.MapProjection;
|
||||
var labels = new List<Label>();
|
||||
var pathFigures = ((PathGeometry)path.Data).Figures;
|
||||
|
||||
pathFigures.Clear();
|
||||
|
||||
if (projection.Type <= MapProjectionType.NormalCylindrical)
|
||||
SetLineDistance();
|
||||
|
||||
if (ParentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical)
|
||||
{
|
||||
if (path == null)
|
||||
{
|
||||
path = new Path { Data = new PathGeometry() };
|
||||
path.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke)));
|
||||
path.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness)));
|
||||
path.SetBinding(Shape.StrokeDashArrayProperty, this.GetOrCreateBinding(StrokeDashArrayProperty, nameof(StrokeDashArray)));
|
||||
path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetOrCreateBinding(StrokeDashOffsetProperty, nameof(StrokeDashOffset)));
|
||||
path.SetBinding(Shape.StrokeDashCapProperty, this.GetOrCreateBinding(StrokeDashCapProperty, nameof(StrokeDashCap)));
|
||||
Children.Add(path);
|
||||
}
|
||||
|
||||
var maxLocation = projection.MapToLocation(new Point(0d, 180d * MapProjection.Wgs84MeterPerDegree));
|
||||
var maxLatitude = maxLocation != null && maxLocation.Latitude < 90d ? maxLocation.Latitude : 90d;
|
||||
|
||||
var bounds = map.ViewRectToBoundingBox(new Rect(0d, 0d, map.RenderSize.Width, map.RenderSize.Height));
|
||||
var lineDistance = GetLineDistance();
|
||||
|
||||
var labelStart = new Location(
|
||||
Math.Ceiling(bounds.South / lineDistance) * lineDistance,
|
||||
Math.Ceiling(bounds.West / lineDistance) * lineDistance);
|
||||
|
||||
var labelEnd = new Location(
|
||||
Math.Floor(bounds.North / lineDistance) * lineDistance,
|
||||
Math.Floor(bounds.East / lineDistance) * lineDistance);
|
||||
|
||||
var lineStart = new Location(
|
||||
Math.Min(Math.Max(labelStart.Latitude - lineDistance, -maxLatitude), maxLatitude),
|
||||
labelStart.Longitude - lineDistance);
|
||||
|
||||
var lineEnd = new Location(
|
||||
Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -maxLatitude), maxLatitude),
|
||||
labelEnd.Longitude + lineDistance);
|
||||
|
||||
var geometry = (PathGeometry)path.Data;
|
||||
geometry.Figures.Clear();
|
||||
|
||||
for (var lat = labelStart.Latitude; lat <= bounds.North; lat += lineDistance)
|
||||
{
|
||||
var figure = new PathFigure
|
||||
{
|
||||
StartPoint = map.LocationToView(new Location(lat, lineStart.Longitude)),
|
||||
IsClosed = false,
|
||||
IsFilled = false
|
||||
};
|
||||
|
||||
figure.Segments.Add(new LineSegment
|
||||
{
|
||||
Point = map.LocationToView(new Location(lat, lineEnd.Longitude))
|
||||
});
|
||||
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
for (var lon = labelStart.Longitude; lon <= bounds.East; lon += lineDistance)
|
||||
{
|
||||
var figure = new PathFigure
|
||||
{
|
||||
StartPoint = map.LocationToView(new Location(lineStart.Latitude, lon)),
|
||||
IsClosed = false,
|
||||
IsFilled = false
|
||||
};
|
||||
|
||||
figure.Segments.Add(new LineSegment
|
||||
{
|
||||
Point = map.LocationToView(new Location(lineEnd.Latitude, lon))
|
||||
});
|
||||
|
||||
geometry.Figures.Add(figure);
|
||||
}
|
||||
|
||||
var labelFormat = GetLabelFormat(lineDistance);
|
||||
var childIndex = 1; // 0 for Path
|
||||
|
||||
for (var lat = labelStart.Latitude; lat <= bounds.North; lat += lineDistance)
|
||||
{
|
||||
for (var lon = labelStart.Longitude; lon <= bounds.East; lon += lineDistance)
|
||||
{
|
||||
TextBlock label;
|
||||
|
||||
if (childIndex < Children.Count)
|
||||
{
|
||||
label = (TextBlock)Children[childIndex];
|
||||
}
|
||||
else
|
||||
{
|
||||
label = new TextBlock { RenderTransform = new MatrixTransform() };
|
||||
label.SetBinding(TextBlock.FontSizeProperty, this.GetOrCreateBinding(FontSizeProperty, nameof(FontSize)));
|
||||
label.SetBinding(TextBlock.FontStyleProperty, this.GetOrCreateBinding(FontStyleProperty, nameof(FontStyle)));
|
||||
label.SetBinding(TextBlock.FontStretchProperty, this.GetOrCreateBinding(FontStretchProperty, nameof(FontStretch)));
|
||||
label.SetBinding(TextBlock.FontWeightProperty, this.GetOrCreateBinding(FontWeightProperty, nameof(FontWeight)));
|
||||
label.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground)));
|
||||
|
||||
if (FontFamily != null)
|
||||
{
|
||||
label.SetBinding(TextBlock.FontFamilyProperty, this.GetOrCreateBinding(FontFamilyProperty, nameof(FontFamily)));
|
||||
}
|
||||
|
||||
Children.Add(label);
|
||||
}
|
||||
|
||||
childIndex++;
|
||||
|
||||
label.Text = GetLabelText(lat, labelFormat, "NS") + "\n" + GetLabelText(Location.NormalizeLongitude(lon), labelFormat, "EW");
|
||||
label.Tag = new Location(lat, lon);
|
||||
label.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
}
|
||||
|
||||
while (Children.Count > childIndex)
|
||||
{
|
||||
Children.RemoveAt(Children.Count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// don't use MapPanel.Location because labels may be at more than 180° distance from map center
|
||||
|
||||
for (int i = 1; i < Children.Count; i++)
|
||||
{
|
||||
var label = (TextBlock)Children[i];
|
||||
var location = (Location)label.Tag;
|
||||
var viewPosition = map.LocationToView(location);
|
||||
var matrix = new Matrix(1, 0, 0, 1, 0, 0);
|
||||
|
||||
matrix.Translate(StrokeThickness / 2d + 2d, -label.DesiredSize.Height / 2d);
|
||||
matrix.Rotate(map.ViewTransform.Rotation);
|
||||
matrix.Translate(viewPosition.X, viewPosition.Y);
|
||||
|
||||
((MatrixTransform)label.RenderTransform).Matrix = matrix;
|
||||
}
|
||||
DrawCylindricalGraticule(pathFigures, labels);
|
||||
}
|
||||
else if (path != null)
|
||||
else
|
||||
{
|
||||
path = null;
|
||||
Children.Clear();
|
||||
DrawGraticule(pathFigures, labels);
|
||||
}
|
||||
|
||||
if (Children.Count == 0)
|
||||
{
|
||||
path.SetBinding(Shape.StrokeProperty, this.GetOrCreateBinding(StrokeProperty, nameof(Stroke)));
|
||||
path.SetBinding(Shape.StrokeThicknessProperty, this.GetOrCreateBinding(StrokeThicknessProperty, nameof(StrokeThickness)));
|
||||
path.SetBinding(Shape.StrokeDashArrayProperty, this.GetOrCreateBinding(StrokeDashArrayProperty, nameof(StrokeDashArray)));
|
||||
path.SetBinding(Shape.StrokeDashOffsetProperty, this.GetOrCreateBinding(StrokeDashOffsetProperty, nameof(StrokeDashOffset)));
|
||||
path.SetBinding(Shape.StrokeDashCapProperty, this.GetOrCreateBinding(StrokeDashCapProperty, nameof(StrokeDashCap)));
|
||||
|
||||
Children.Add(path);
|
||||
}
|
||||
|
||||
var childrenCount = 1;
|
||||
|
||||
foreach (var label in labels)
|
||||
{
|
||||
TextBlock textBlock;
|
||||
|
||||
if (childrenCount < Children.Count)
|
||||
{
|
||||
textBlock = (TextBlock)Children[childrenCount];
|
||||
}
|
||||
else
|
||||
{
|
||||
textBlock = new TextBlock { RenderTransform = new MatrixTransform() };
|
||||
textBlock.SetBinding(TextBlock.FontSizeProperty, this.GetOrCreateBinding(FontSizeProperty, nameof(FontSize)));
|
||||
textBlock.SetBinding(TextBlock.FontStyleProperty, this.GetOrCreateBinding(FontStyleProperty, nameof(FontStyle)));
|
||||
textBlock.SetBinding(TextBlock.FontStretchProperty, this.GetOrCreateBinding(FontStretchProperty, nameof(FontStretch)));
|
||||
textBlock.SetBinding(TextBlock.FontWeightProperty, this.GetOrCreateBinding(FontWeightProperty, nameof(FontWeight)));
|
||||
textBlock.SetBinding(TextBlock.ForegroundProperty, this.GetOrCreateBinding(ForegroundProperty, nameof(Foreground)));
|
||||
|
||||
if (FontFamily != null)
|
||||
{
|
||||
textBlock.SetBinding(TextBlock.FontFamilyProperty, this.GetOrCreateBinding(FontFamilyProperty, nameof(FontFamily)));
|
||||
}
|
||||
|
||||
Children.Add(textBlock);
|
||||
}
|
||||
|
||||
textBlock.Text = label.LatitudeText + "\n" + label.LongitudeText;
|
||||
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
|
||||
var matrix = new Matrix(1, 0, 0, 1, 0, 0);
|
||||
|
||||
matrix.Translate(StrokeThickness / 2d + 2d, -textBlock.DesiredSize.Height / 2d);
|
||||
matrix.Rotate(label.Rotation);
|
||||
matrix.Translate(label.Position.X, label.Position.Y);
|
||||
|
||||
((MatrixTransform)textBlock.RenderTransform).Matrix = matrix;
|
||||
|
||||
childrenCount++;
|
||||
}
|
||||
|
||||
while (Children.Count > childrenCount)
|
||||
{
|
||||
Children.RemoveAt(Children.Count - 1);
|
||||
}
|
||||
|
||||
base.OnViewportChanged(e);
|
||||
}
|
||||
|
||||
private static PathFigure CreatePolylineFigure(ICollection<Point> points)
|
||||
{
|
||||
var figure = new PathFigure
|
||||
{
|
||||
StartPoint = points.First(),
|
||||
IsFilled = false
|
||||
};
|
||||
|
||||
var polyline = new PolyLineSegment();
|
||||
|
||||
foreach (var p in points.Skip(1))
|
||||
{
|
||||
polyline.Points.Add(p);
|
||||
}
|
||||
|
||||
figure.Segments.Add(polyline);
|
||||
return figure;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue