Minor improvements

This commit is contained in:
ClemensFischer 2022-12-01 22:48:08 +01:00
parent 754e185c5d
commit 7e6b187fd7
9 changed files with 44 additions and 46 deletions

View file

@ -15,8 +15,8 @@ namespace MapControl
#endif #endif
public class BoundingBox public class BoundingBox
{ {
private double south; private double south = double.NaN;
private double north; private double north = double.NaN;
public BoundingBox() public BoundingBox()
{ {
@ -30,9 +30,8 @@ namespace MapControl
East = east; East = east;
} }
public double West { get; set; } public double West { get; set; } = double.NaN;
public double East { get; set; } = double.NaN;
public double East { get; set; }
public double South public double South
{ {
@ -64,11 +63,16 @@ namespace MapControl
protected set { } protected set { }
} }
public static BoundingBox Parse(string s) public static BoundingBox Parse(string boundingBox)
{ {
var values = s.Split(new char[] { ',' }); string[] values = null;
if (values.Length != 4) if (!string.IsNullOrEmpty(boundingBox))
{
values = boundingBox.Split(new char[] { ',' });
}
if (values?.Length != 4)
{ {
throw new FormatException("BoundingBox string must be a comma-separated list of four floating point numbers."); throw new FormatException("BoundingBox string must be a comma-separated list of four floating point numbers.");
} }

View file

@ -242,11 +242,11 @@ namespace MapControl
private static LatLonBox ReadLatLonBox(XmlElement element) private static LatLonBox ReadLatLonBox(XmlElement element)
{ {
double north = double.NaN; var north = double.NaN;
double south = double.NaN; var south = double.NaN;
double east = double.NaN; var east = double.NaN;
double west = double.NaN; var west = double.NaN;
double rotation = 0d; var rotation = 0d;
foreach (var childElement in element.ChildNodes.OfType<XmlElement>()) foreach (var childElement in element.ChildNodes.OfType<XmlElement>())
{ {

View file

@ -57,42 +57,33 @@ namespace MapControl
return string.Format(CultureInfo.InvariantCulture, "{0:F5},{1:F5}", Latitude, Longitude); return string.Format(CultureInfo.InvariantCulture, "{0:F5},{1:F5}", Latitude, Longitude);
} }
public static Location Parse(string locationString) public static Location Parse(string location)
{ {
Location location = null; string[] values = null;
if (!string.IsNullOrEmpty(locationString)) if (!string.IsNullOrEmpty(location))
{ {
var values = locationString.Split(new char[] { ',' }); values = location.Split(new char[] { ',' });
if (values.Length != 2)
{
throw new FormatException("Location string must be a comma-separated pair of double values.");
}
location = new Location(
double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture),
double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture));
} }
return location; if (values?.Length != 2)
{
throw new FormatException("Location string must be a comma-separated pair of floating point numbers.");
}
return new Location(
double.Parse(values[0], NumberStyles.Float, CultureInfo.InvariantCulture),
double.Parse(values[1], NumberStyles.Float, CultureInfo.InvariantCulture));
} }
/// <summary> /// <summary>
/// Normalizes a longitude to a value in the interval [-180 .. 180]. /// Normalizes a longitude to a value in the interval [-180 .. 180).
/// </summary> /// </summary>
public static double NormalizeLongitude(double longitude) public static double NormalizeLongitude(double longitude)
{ {
if (longitude < -180d) var x = (longitude + 180d) % 360d;
{
longitude = ((longitude + 180d) % 360d) + 180d;
}
else if (longitude > 180d)
{
longitude = ((longitude - 180d) % 360d) - 180d;
}
return longitude; return x < 0d ? x + 180d : x - 180d;
} }
/// <summary> /// <summary>

View file

@ -50,9 +50,14 @@ namespace MapControl
Add(new Location(latitude, longitude)); Add(new Location(latitude, longitude));
} }
public static LocationCollection Parse(string s) public static LocationCollection Parse(string locations)
{ {
var strings = s.Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries); if (string.IsNullOrEmpty(locations))
{
return new LocationCollection();
}
var strings = locations.Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
return new LocationCollection(strings.Select(l => Location.Parse(l))); return new LocationCollection(strings.Select(l => Location.Parse(l)));
} }

View file

@ -390,11 +390,11 @@ namespace MapControl
if (offset > 180d) if (offset > 180d)
{ {
longitude = Center.Longitude - 360d + offset % 360d; longitude = Center.Longitude + (offset % 360d) - 360d;
} }
else if (offset < -180d) else if (offset < -180d)
{ {
longitude = Center.Longitude + 360d + offset % 360d; longitude = Center.Longitude + (offset % 360d) + 360d;
} }
return longitude; return longitude;

View file

@ -92,9 +92,7 @@ namespace MapControl
{ {
var hemisphere = hemispheres[0]; var hemisphere = hemispheres[0];
value = (value + 540d) % 360d - 180d; if (value < -1e-8) // ~1 mm
if (value < -1e-8) // ~1mm
{ {
value = -value; value = -value;
hemisphere = hemispheres[1]; hemisphere = hemispheres[1];

View file

@ -211,7 +211,6 @@ namespace MapControl
IsOutsideViewport(position)) IsOutsideViewport(position))
{ {
location = new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude)); location = new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude));
position = parentMap.LocationToView(location); position = parentMap.LocationToView(location);
} }

View file

@ -58,6 +58,7 @@ namespace MapControl
/// <summary> /// <summary>
/// Transforms a Location in geographic coordinates to a Point in projected map coordinates. /// Transforms a Location in geographic coordinates to a Point in projected map coordinates.
/// Returns new Point(double.NaN, double.NaN) when the Location can not be transformed.
/// </summary> /// </summary>
public abstract Point LocationToMap(Location location); public abstract Point LocationToMap(Location location);

View file

@ -7,7 +7,7 @@ namespace MapControl
/// <summary> /// <summary>
/// Rotated rectangle used to arrange and rotate an element with a BoundingBox. /// Rotated rectangle used to arrange and rotate an element with a BoundingBox.
/// </summary> /// </summary>
public struct ViewRect public class ViewRect
{ {
public ViewRect(double x, double y, double width, double height, double rotation) public ViewRect(double x, double y, double width, double height, double rotation)
{ {