Simplify BoundingBox

This commit is contained in:
ClemensFischer 2022-12-07 23:34:42 +01:00
parent 142e86da31
commit d8c416d552
3 changed files with 39 additions and 41 deletions

View file

@ -3,9 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
#if !WINUI && !UWP
using System.Windows;
#endif
namespace MapControl namespace MapControl
{ {
@ -19,23 +16,6 @@ namespace MapControl
Type = MapProjectionType.Azimuthal; Type = MapProjectionType.Azimuthal;
} }
public override MapRect BoundingBoxToMapRect(BoundingBox boundingBox)
{
var center = LocationToMap(boundingBox.Center);
if (!center.HasValue)
{
return null;
}
var width = boundingBox.Width * Wgs84MeterPerDegree;
var height = boundingBox.Height * Wgs84MeterPerDegree;
var x = center.Value.X - width / 2d;
var y = center.Value.Y - height / 2d;
return new MapRect(x, y, x + width, y + height);
}
public override BoundingBox MapRectToBoundingBox(MapRect mapRect) public override BoundingBox MapRectToBoundingBox(MapRect mapRect)
{ {
var center = MapToLocation(mapRect.Center); var center = MapToLocation(mapRect.Center);

View file

@ -39,9 +39,6 @@ namespace MapControl
{ {
} }
public double West { get; set; }
public double East { get; set; }
public double South public double South
{ {
get => south; get => south;
@ -54,20 +51,16 @@ namespace MapControl
set => north = Math.Min(Math.Max(value, -90d), 90d); set => north = Math.Min(Math.Max(value, -90d), 90d);
} }
public virtual double Width public double West { get; set; }
{ public double East { get; set; }
get => East - West;
}
public virtual double Height public virtual double Width => East - West;
{ public virtual double Height => North - South;
get => North - South;
}
public virtual Location Center public virtual Location Center =>
{ double.IsNaN(South) || double.IsNaN(North) || double.IsNaN(West) || double.IsNaN(East)
get => new Location((South + North) / 2d, (West + East) / 2d); ? null
} : new Location((South + North) / 2d, (West + East) / 2d);
public static BoundingBox Parse(string boundingBox) public static BoundingBox Parse(string boundingBox)
{ {

View file

@ -72,10 +72,33 @@ namespace MapControl
/// </summary> /// </summary>
public virtual MapRect BoundingBoxToMapRect(BoundingBox boundingBox) public virtual MapRect BoundingBoxToMapRect(BoundingBox boundingBox)
{ {
var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West)); if (!double.IsNaN(boundingBox.South) && !double.IsNaN(boundingBox.West) &&
var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East)); !double.IsNaN(boundingBox.North) && !double.IsNaN(boundingBox.East))
{
var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West));
var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East));
return p1.HasValue && p2.HasValue ? new MapRect(p1.Value, p2.Value) : null; if (p1.HasValue && p2.HasValue)
{
return new MapRect(p1.Value, p2.Value);
}
}
else if (boundingBox.Center != null)
{
var center = LocationToMap(boundingBox.Center);
if (center.HasValue)
{
var width = boundingBox.Width * Wgs84MeterPerDegree;
var height = boundingBox.Height * Wgs84MeterPerDegree;
var x = center.Value.X - width / 2d;
var y = center.Value.Y - height / 2d;
return new MapRect(x, y, x + width, y + height);
}
}
return null;
} }
/// <summary> /// <summary>
@ -84,10 +107,12 @@ namespace MapControl
/// </summary> /// </summary>
public virtual BoundingBox MapRectToBoundingBox(MapRect mapRect) public virtual BoundingBox MapRectToBoundingBox(MapRect mapRect)
{ {
var sw = MapToLocation(new Point(mapRect.XMin, mapRect.YMin)); var southWest = MapToLocation(new Point(mapRect.XMin, mapRect.YMin));
var ne = MapToLocation(new Point(mapRect.XMax, mapRect.YMax)); var northEast = MapToLocation(new Point(mapRect.XMax, mapRect.YMax));
return sw != null && ne != null ? new BoundingBox(sw, ne) : null; return southWest != null && northEast != null
? new BoundingBox(southWest, northEast)
: null;
} }
/// <summary> /// <summary>