private static double ToMeters(double fromValue, UnitOfLength fromUnit) { switch (fromUnit) { case UnitOfLength.Millimeter: return(fromValue * Math.Pow(10, -3)); case UnitOfLength.Kilometer: return(fromValue * Math.Pow(10, 3)); case UnitOfLength.ScandinavianMile: return(fromValue * Math.Pow(10, 4)); case UnitOfLength.InternationalMile: return(fromValue * 1609.344); case UnitOfLength.Inch: return(fromValue * 0.0254); //inch == 2.54 cm case UnitOfLength.Inch32Fraction: return(fromValue * (0.0254 / 32.0)); case UnitOfLength.Meter: return(fromValue); default: throw new Exception(fromUnit.ToString()); } }
private static double FromMeters(double meter, UnitOfLength toUnit) { switch (toUnit) { case UnitOfLength.Millimeter: return(meter / Math.Pow(10, -3)); case UnitOfLength.Kilometer: return(meter / Math.Pow(10, 3)); case UnitOfLength.ScandinavianMile: return(meter / Math.Pow(10, 4)); case UnitOfLength.InternationalMile: return(meter / 1609.344); case UnitOfLength.Inch: return(meter / 0.0254); //inch == 2.54 cm case UnitOfLength.Inch32Fraction: return(meter / (0.0254 / 32.0)); case UnitOfLength.Meter: return(meter); default: throw new Exception(toUnit.ToString()); } }
public static string GetEnglishAbbreviation(this UnitOfLength unit) { switch (unit) { case UnitOfLength.Inch: return("in"); case UnitOfLength.Inch32Fraction: return("32nd"); case UnitOfLength.Millimeter: return("mm"); case UnitOfLength.Kilometer: return("km"); case UnitOfLength.InternationalMile: return("mi"); case UnitOfLength.Meter: return("m"); default: throw new Exception(unit.ToString()); } }
public static string GetUSEnglishName(this UnitOfLength unit) { switch (unit) { case UnitOfLength.Millimeter: return("millimeter"); case UnitOfLength.Kilometer: return("kilometer"); case UnitOfLength.ScandinavianMile: return("scandinavian mile"); case UnitOfLength.InternationalMile: return("mile"); case UnitOfLength.Meter: return("meter"); case UnitOfLength.Inch: return("inch"); case UnitOfLength.Inch32Fraction: return("32nd of inch"); default: throw new Exception(unit.ToString()); } }
public static string GetUSEnglishNamePluralized(this UnitOfLength unit) { if (unit == UnitOfLength.Inch) { return("inches"); } return(GetUSEnglishName(unit) + "s"); }
/// <summary> /// Convert value to requested unit of length /// </summary> public static Distance ConvertTo(this Distance self, UnitOfLength toUnit) { if (self.Unit == toUnit) { return(new Distance(value: self.Value, unit: self.Unit)); } var meter = ToMeters(self.Value, self.Unit); return(new Distance(value: FromMeters(meter, toUnit), unit: toUnit)); }
public double DistanceTo(Coordinates i_TargetCoordinates, UnitOfLength i_UnitOfLength) { var baseRad = Math.PI * Latitude / 180; var targetRad = Math.PI * i_TargetCoordinates.Latitude / 180; var theta = Longitude - i_TargetCoordinates.Longitude; var thetaRad = Math.PI * theta / 180; double dist = (Math.Sin(baseRad) * Math.Sin(targetRad)) + (Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad)); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(i_UnitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Geographical distance http://en.wikipedia.org/wiki/Geographical_distance /// </summary> public static double DistanceToByGeo(this Coordinate src, Coordinate dest, UnitOfLength unitOfLength) { var radLatSrc = src.Latitude * CoordinateConst.DegreesToRadians; var radLatDest = dest.Latitude * CoordinateConst.DegreesToRadians; var dLat = radLatDest - radLatSrc; var dLon = (dest.Longitude - src.Longitude) * CoordinateConst.DegreesToRadians; var a = dLon * Math.Cos((radLatSrc + radLatDest) / 2); // central angle, aka arc segment angular distance var centralAngle = Math.Sqrt(a * a + dLat * dLat); // great-circle (orthodromic) distance on Earth between 2 points var dist = CoordinateConst.EarthRadiusMile * centralAngle; return(unitOfLength.ConvertFromMiles(dist)); }
private static decimal?ConvertToUnit(UnitOfLength unit, decimal?value) { if (!value.HasValue) { return(null); } switch (unit) { case UnitOfLength.Kilometers: return(value); case UnitOfLength.Miles: return(value * (decimal)OneMileInKilometers); default: throw new InvalidOperationException(); } }
/// <summary> /// By Spherical law of cosines http://en.wikipedia.org/wiki/Spherical_law_of_cosines /// </summary> public static double DistanceTo(this Coordinate src, Coordinate dest, UnitOfLength unitOfLength) { var theta = src.Longitude - dest.Longitude; var thetaRad = theta * CoordinateConst.DegreesToRadians; var targetRad = dest.Latitude * CoordinateConst.DegreesToRadians; var baseRad = src.Latitude * CoordinateConst.DegreesToRadians; var dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); // calculate to earth radius by miles dist = dist * CoordinateConst.EarthRadiusMile; return(unitOfLength.ConvertFromMiles(dist)); }
public static double DistanceTo(this LocationDto baseCoordinates, LocationDto targetCoordinates, UnitOfLength unitOfLength) { var baseRad = Math.PI * double.Parse(baseCoordinates.Latitude) / 180; var targetRad = Math.PI * double.Parse(targetCoordinates.Latitude) / 180; var theta = double.Parse(baseCoordinates.Longitude) - double.Parse(targetCoordinates.Longitude); var thetaRad = Math.PI * theta / 180; double dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Haversine https://en.wikipedia.org/wiki/Haversine_formula /// </summary> /// <returns></returns> public static double GetDistance(double srcLng, double srcLat, double destLng, double destLat, UnitOfLength unitOfLength) { var src = new Models.Coordinate(srcLng, srcLat); var dest = new Models.Coordinate(destLng, destLat); return(src.DistanceToByHaversine(dest, unitOfLength)); }
public void Should_render_GetEnglishAbbreviation(UnitOfLength unitOfLength) { Assert.NotEmpty(unitOfLength.GetEnglishAbbreviation()); }
public void Should_render_GetUSEnglishName(UnitOfLength unitOfLength) { Assert.NotEmpty(unitOfLength.GetUSEnglishName()); }
public double DistanceTo(GeoCoordinate baseCoordinates, GeoCoordinate targetCoordinates, UnitOfLength unitOfLength) { var baseRad = Math.PI * baseCoordinates.Latitude / 180; var targetRad = Math.PI * targetCoordinates.Latitude / 180; var theta = baseCoordinates.Longitude - targetCoordinates.Longitude; var thetaRad = Math.PI * theta / 180; double dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// By Haversine https://en.wikipedia.org/wiki/Haversine_formula /// </summary> /// <returns></returns> public static double DistanceToByHaversine(this Models.Coordinate src, Models.Coordinate dest, UnitOfLength unitOfLength) { var dLat = (dest.Latitude - src.Latitude) * CoordinateConst.DegreesToRadians; var dLon = (dest.Longitude - src.Longitude) * CoordinateConst.DegreesToRadians; var a = Math.Pow(Math.Sin(dLat / 2), 2) + Math.Cos(src.Latitude * CoordinateConst.DegreesToRadians) * Math.Cos(dest.Latitude * CoordinateConst.DegreesToRadians) * Math.Pow(Math.Sin(dLon / 2), 2); // central angle, aka arc segment angular distance var centralAngle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); var dist = CoordinateConst.EarthRadiusMile * centralAngle; return(unitOfLength.ConvertFromMiles(dist)); }
/// <summary> /// create a new instance of distance /// </summary> public Distance(UnitOfLength unit, double value) { Unit = unit; Value = value; }