2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A geographic bounding box with south and north latitude and west and east longitude values in degrees.
|
|
|
|
|
|
/// </summary>
|
2024-05-22 11:25:32 +02:00
|
|
|
|
#if UWP || WINUI
|
2024-04-11 15:41:05 +02:00
|
|
|
|
[Windows.Foundation.Metadata.CreateFromString(MethodName = "Parse")]
|
2024-04-11 14:57:54 +02:00
|
|
|
|
#else
|
2018-08-08 23:31:52 +02:00
|
|
|
|
[System.ComponentModel.TypeConverter(typeof(BoundingBoxConverter))]
|
|
|
|
|
|
#endif
|
|
|
|
|
|
public class BoundingBox
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2024-08-29 21:35:58 +02:00
|
|
|
|
protected BoundingBox()
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-09 16:44:45 +02:00
|
|
|
|
public BoundingBox(double latitude1, double longitude1, double latitude2, double longitude2)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2024-04-11 14:57:54 +02:00
|
|
|
|
South = Math.Min(Math.Max(Math.Min(latitude1, latitude2), -90d), 90d);
|
|
|
|
|
|
North = Math.Min(Math.Max(Math.Max(latitude1, latitude2), -90d), 90d);
|
2022-12-07 17:00:25 +01:00
|
|
|
|
West = Math.Min(longitude1, longitude2);
|
|
|
|
|
|
East = Math.Max(longitude1, longitude2);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-07 17:00:25 +01:00
|
|
|
|
public BoundingBox(Location location1, Location location2)
|
|
|
|
|
|
: this(location1.Latitude, location1.Longitude, location2.Latitude, location2.Longitude)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-29 23:56:29 +02:00
|
|
|
|
public double South { get; }
|
|
|
|
|
|
public double North { get; }
|
|
|
|
|
|
public double West { get; }
|
|
|
|
|
|
public double East { get; }
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2022-12-07 23:34:42 +01:00
|
|
|
|
public virtual double Width => East - West;
|
|
|
|
|
|
public virtual double Height => North - South;
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2025-09-19 18:48:05 +02:00
|
|
|
|
public virtual Location Center => new((South + North) / 2d, (West + East) / 2d);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2025-08-14 13:11:25 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, "{0},{1},{2},{3}", South, West, North, East);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-11 15:59:07 +02:00
|
|
|
|
/// <summary>
|
2024-09-09 16:44:45 +02:00
|
|
|
|
/// Creates a BoundingBox instance from a string containing a comma-separated sequence of four or five floating point numbers.
|
2024-04-11 15:59:07 +02:00
|
|
|
|
/// </summary>
|
2022-12-01 22:48:08 +01:00
|
|
|
|
public static BoundingBox Parse(string boundingBox)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2022-12-01 22:48:08 +01:00
|
|
|
|
string[] values = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(boundingBox))
|
|
|
|
|
|
{
|
|
|
|
|
|
values = boundingBox.Split(new char[] { ',' });
|
|
|
|
|
|
}
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
2024-09-05 21:27:04 +02:00
|
|
|
|
if (values == null || values.Length != 4 && values.Length != 5)
|
2017-06-25 23:05:48 +02:00
|
|
|
|
{
|
2024-09-12 15:48:20 +02:00
|
|
|
|
throw new FormatException($"{nameof(BoundingBox)} string must contain a comma-separated sequence of four or five floating point numbers.");
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-05 21:27:04 +02:00
|
|
|
|
var rotation = values.Length == 5
|
|
|
|
|
|
? double.Parse(values[4], NumberStyles.Float, CultureInfo.InvariantCulture)
|
|
|
|
|
|
: 0d;
|
|
|
|
|
|
|
2025-01-27 19:30:02 +01:00
|
|
|
|
// Always return a LatLonBox, i.e. a BoundingBox with optional rotation, as used by GeoImage and GroundOverlay.
|
2024-09-09 16:44:45 +02:00
|
|
|
|
//
|
|
|
|
|
|
return new LatLonBox(
|
2017-06-25 23:05:48 +02:00
|
|
|
|
double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture),
|
|
|
|
|
|
double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture),
|
|
|
|
|
|
double.Parse(values[2], NumberStyles.Float, CultureInfo.InvariantCulture),
|
2024-09-05 21:27:04 +02:00
|
|
|
|
double.Parse(values[3], NumberStyles.Float, CultureInfo.InvariantCulture),
|
|
|
|
|
|
rotation);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|