Added MapProjection.Type

This commit is contained in:
Clemens 2022-03-05 18:40:57 +01:00
parent a07948be02
commit 16fb98ac86
15 changed files with 126 additions and 77 deletions

View file

@ -20,8 +20,8 @@ namespace MapControl
public AutoEquirectangularProjection()
{
Type = MapProjectionType.NormalCylindrical;
CrsId = DefaultCrsId;
IsNormalCylindrical = true;
}
public override Point LocationToMap(Location location)

View file

@ -15,6 +15,11 @@ namespace MapControl
/// </summary>
public class AzimuthalEquidistantProjection : AzimuthalProjection
{
public AzimuthalEquidistantProjection(string crsId)
{
CrsId = crsId;
}
public override Point LocationToMap(Location location)
{
if (location.Equals(Center))

View file

@ -16,6 +16,11 @@ namespace MapControl
/// </summary>
public abstract class AzimuthalProjection : MapProjection
{
protected AzimuthalProjection()
{
Type = MapProjectionType.Azimuthal;
}
public override Rect BoundingBoxToRect(BoundingBox boundingBox)
{
var center = LocationToMap(boundingBox.Center);

View file

@ -23,8 +23,8 @@ namespace MapControl
public EquirectangularProjection()
{
Type = MapProjectionType.NormalCylindrical;
CrsId = DefaultCrsId;
IsNormalCylindrical = true;
}
public override Vector GetRelativeScale(Location location)

View file

@ -44,7 +44,7 @@ namespace MapControl
public static readonly DependencyProperty MapProjectionProperty = DependencyProperty.Register(
nameof(MapProjection), typeof(MapProjection), typeof(MapBase),
new PropertyMetadata(new WebMercatorProjection(), (o, e) => ((MapBase)o).MapProjectionPropertyChanged()));
new PropertyMetadata(new WebMercatorProjection(), (o, e) => ((MapBase)o).MapProjectionPropertyChanged((MapProjection)e.NewValue)));
public static readonly DependencyProperty ProjectionCenterProperty = DependencyProperty.Register(
nameof(ProjectionCenter), typeof(Location), typeof(MapBase),
@ -72,6 +72,7 @@ namespace MapControl
private Location transformCenter;
private Point viewCenter;
private double centerLongitude;
private double maxLatitude = 90d;
private bool internalPropertyChange;
/// <summary>
@ -436,8 +437,23 @@ namespace MapControl
}
}
private void MapProjectionPropertyChanged()
private void MapProjectionPropertyChanged(MapProjection projection)
{
maxLatitude = 90d;
if (projection.Type <= MapProjectionType.NormalCylindrical)
{
var maxLocation = projection.MapToLocation(new Point(0d, 180d * MapProjection.Wgs84MeterPerDegree));
if (maxLocation != null && maxLocation.Latitude < 90d)
{
maxLatitude = maxLocation.Latitude;
var center = Center;
AdjustCenterProperty(CenterProperty, ref center);
}
}
ResetTransformCenter();
UpdateTransform(false, true);
}
@ -450,16 +466,23 @@ namespace MapControl
private void AdjustCenterProperty(DependencyProperty property, ref Location center)
{
if (center == null ||
center.Longitude < -180d || center.Longitude > 180d ||
center.Latitude < -MapProjection.MaxLatitude || center.Latitude > MapProjection.MaxLatitude)
{
center = (center == null)
? new Location()
: new Location(
Math.Min(Math.Max(center.Latitude, -MapProjection.MaxLatitude), MapProjection.MaxLatitude),
Location.NormalizeLongitude(center.Longitude));
var c = center;
if (center == null)
{
center = new Location();
}
else if (
center.Latitude < -maxLatitude || center.Latitude > maxLatitude ||
center.Longitude < -180d || center.Longitude > 180d)
{
center = new Location(
Math.Min(Math.Max(center.Latitude, -maxLatitude), maxLatitude),
Location.NormalizeLongitude(center.Longitude));
}
if (center != c)
{
SetValueInternal(property, center);
}
}
@ -727,9 +750,9 @@ namespace MapControl
{
center.Longitude = Location.NormalizeLongitude(center.Longitude);
if (center.Latitude < -projection.MaxLatitude || center.Latitude > projection.MaxLatitude)
if (center.Latitude < -maxLatitude || center.Latitude > maxLatitude)
{
center.Latitude = Math.Min(Math.Max(center.Latitude, -projection.MaxLatitude), projection.MaxLatitude);
center.Latitude = Math.Min(Math.Max(center.Latitude, -maxLatitude), maxLatitude);
resetTransformCenter = true;
}

View file

@ -118,7 +118,7 @@ namespace MapControl
var position = parentMap.LocationToView(location);
if (parentMap.MapProjection.IsNormalCylindrical && IsOutsideViewport(position))
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical && IsOutsideViewport(position))
{
location = new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude));
@ -144,7 +144,7 @@ namespace MapControl
var center = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d);
var position = parentMap.ViewTransform.MapToView(center);
if (parentMap.MapProjection.IsNormalCylindrical && IsOutsideViewport(position))
if (parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical && IsOutsideViewport(position))
{
var location = parentMap.MapProjection.MapToLocation(center);
if (location != null)

View file

@ -87,7 +87,7 @@ namespace MapControl
{
var longitudeOffset = 0d;
if (location != null && parentMap.MapProjection.IsNormalCylindrical)
if (location != null && parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical)
{
var pos = parentMap.LocationToView(location);

View file

@ -12,6 +12,15 @@ using System.Windows;
namespace MapControl
{
public enum MapProjectionType
{
WebMercator, // normal cylindrical projection compatible with MapTileLayer
NormalCylindrical,
TransverseCylindrical,
Azimuthal,
Other
}
/// <summary>
/// Defines a map projection between geographic coordinates and cartesian map coordinates.
/// </summary>
@ -25,30 +34,20 @@ namespace MapControl
public static MapProjectionFactory Factory { get; set; } = new MapProjectionFactory();
/// <summary>
/// Gets or sets the WMS 1.3.0 CRS identifier.
/// Gets the type of the projection.
/// </summary>
public string CrsId { get; set; } = string.Empty;
public MapProjectionType Type { get; protected set; } = MapProjectionType.Other;
/// <summary>
/// Gets the WMS 1.3.0 CRS identifier.
/// </summary>
public string CrsId { get; protected set; } = string.Empty;
/// <summary>
/// Gets or sets the projection center.
/// </summary>
public Location Center { get; set; } = new Location();
/// <summary>
/// Indicates if this is a normal cylindrical projection.
/// </summary>
public bool IsNormalCylindrical { get; protected set; }
/// <summary>
/// Indicates if this is a web mercator projection, i.e. compatible with MapTileLayer.
/// </summary>
public bool IsWebMercator { get; protected set; }
/// <summary>
/// Gets the absolute value of the minimum and maximum latitude that can be transformed.
/// </summary>
public double MaxLatitude { get; protected set; } = 90d;
/// <summary>
/// Gets the relative map scale at the specified Location.
/// </summary>

View file

@ -41,7 +41,7 @@ namespace MapControl
break;
case "EPSG:97003": // proprietary CRS ID
projection = new AzimuthalEquidistantProjection { CrsId = crsId };
projection = new AzimuthalEquidistantProjection(crsId);
break;
default:

View file

@ -120,7 +120,7 @@ namespace MapControl
{
var update = false;
if (ParentMap == null || !ParentMap.MapProjection.IsWebMercator)
if (ParentMap == null || ParentMap.MapProjection.Type != MapProjectionType.WebMercator)
{
update = TileMatrix != null;
TileMatrix = null;

View file

@ -19,10 +19,8 @@ namespace MapControl
public WebMercatorProjection()
{
Type = MapProjectionType.WebMercator;
CrsId = DefaultCrsId;
IsNormalCylindrical = true;
IsWebMercator = true;
MaxLatitude = YToLatitude(180d);
}
public override Vector GetRelativeScale(Location location)

View file

@ -22,9 +22,8 @@ namespace MapControl
public WorldMercatorProjection()
{
Type = MapProjectionType.NormalCylindrical;
CrsId = DefaultCrsId;
IsNormalCylindrical = true;
MaxLatitude = YToLatitude(180d);
}
public override Vector GetRelativeScale(Location location)

View file

@ -43,7 +43,7 @@ namespace MapControl
if (projection != null)
{
if (projection.IsNormalCylindrical)
if (projection.Type <= MapProjectionType.NormalCylindrical)
{
DrawCylindricalGraticule(drawingContext);
}
@ -126,9 +126,6 @@ namespace MapControl
var lineDistance = GetLineDistance();
var labelFormat = GetLabelFormat(lineDistance);
var centerLon = Math.Floor(ParentMap.Center.Longitude / lineDistance) * lineDistance;
var minLon = centerLon - lineDistance;
var maxLon = centerLon + lineDistance;
var minLat = 0d;
var maxLat = 0d;
@ -139,16 +136,21 @@ namespace MapControl
var interpolationDistance = lineDistance / interpolationCount;
var latPoints = latSegments * interpolationCount;
var centerLon = Math.Floor(ParentMap.Center.Longitude / lineDistance) * lineDistance;
var minLon = centerLon - lineDistance;
var maxLon = centerLon + lineDistance;
var lonRange = ParentMap.MapProjection.Type == MapProjectionType.TransverseCylindrical ? 15d : 180d;
if (DrawMeridian(path.Figures, centerLon, minLat, interpolationDistance, latPoints))
{
while (minLon > centerLon - 180d &&
DrawMeridian(path.Figures, minLon, minLat, interpolationDistance, latPoints))
while (DrawMeridian(path.Figures, minLon, minLat, interpolationDistance, latPoints) &&
minLon > centerLon - lonRange)
{
minLon -= lineDistance;
}
while (maxLon <= centerLon + 180d &&
DrawMeridian(path.Figures, maxLon, minLat, interpolationDistance, latPoints))
while (DrawMeridian(path.Figures, maxLon, minLat, interpolationDistance, latPoints) &&
maxLon <= centerLon + lonRange)
{
maxLon += lineDistance;
}
@ -160,12 +162,14 @@ namespace MapControl
{
var lat = minLat + s * lineDistance;
var lon = minLon;
var location = new Location(lat, lon);
var points = new List<Point>();
var p = ParentMap.LocationToView(new Location(lat, lon));
var p = ParentMap.LocationToView(location);
if (MapProjection.IsValid(p))
{
points.Add(p);
DrawLabel(drawingContext, typeface, pixelsPerDip, p, location, labelFormat);
}
for (int i = 0; i < lonSegments; i++)
@ -173,7 +177,8 @@ namespace MapControl
for (int j = 1; j <= interpolationCount; j++)
{
lon = minLon + i * lineDistance + j * interpolationDistance;
p = ParentMap.LocationToView(new Location(lat, lon));
location = new Location(lat, lon);
p = ParentMap.LocationToView(location);
if (MapProjection.IsValid(p))
{
@ -181,11 +186,7 @@ namespace MapControl
}
}
if (p.X >= 0d && p.X <= ParentMap.RenderSize.Width &&
p.Y >= 0d && p.Y <= ParentMap.RenderSize.Height)
{
DrawLabel(drawingContext, typeface, pixelsPerDip, p, new Location(lat, lon), labelFormat);
}
DrawLabel(drawingContext, typeface, pixelsPerDip, p, location, labelFormat);
}
if (points.Count >= 2)
@ -232,18 +233,22 @@ namespace MapControl
private void DrawLabel(DrawingContext drawingContext, Typeface typeface, double pixelsPerDip,
Point position, Location location, string labelFormat)
{
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))
if (position.X >= 0d && position.X <= ParentMap.RenderSize.Width &&
position.Y >= 0d && position.Y <= ParentMap.RenderSize.Height)
{
DrawLabel(drawingContext, latText, lonText, position, Vector.AngleBetween(new Vector(1d, 0d), p - position));
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));
}
}
}

View file

@ -30,7 +30,7 @@ namespace MapControl
var map = ParentMap;
var projection = map.MapProjection;
if (projection.IsNormalCylindrical)
if (projection.Type <= MapProjectionType.NormalCylindrical)
{
if (path == null)
{
@ -43,11 +43,14 @@ namespace MapControl
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.South / lineDistance) * lineDistance,
Math.Ceiling(bounds.West / lineDistance) * lineDistance);
var labelEnd = new Location(
@ -55,11 +58,11 @@ namespace MapControl
Math.Floor(bounds.East / lineDistance) * lineDistance);
var lineStart = new Location(
Math.Min(Math.Max(labelStart.Latitude - lineDistance, -projection.MaxLatitude), projection.MaxLatitude),
Math.Min(Math.Max(labelStart.Latitude - lineDistance, -maxLatitude), maxLatitude),
labelStart.Longitude - lineDistance);
var lineEnd = new Location(
Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -projection.MaxLatitude), projection.MaxLatitude),
Math.Min(Math.Max(labelEnd.Latitude + lineDistance, -maxLatitude), maxLatitude),
labelEnd.Longitude + lineDistance);
var geometry = (PathGeometry)path.Data;