diff options
Diffstat (limited to 'PositionCalculator.cs')
| -rw-r--r-- | PositionCalculator.cs | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/PositionCalculator.cs b/PositionCalculator.cs index 4344e76..2140a88 100644 --- a/PositionCalculator.cs +++ b/PositionCalculator.cs @@ -1,16 +1,33 @@ using CoordinateSharp; -using PagerParser; -using System; using System.ComponentModel.DataAnnotations.Schema; +using System.Text.Json.Serialization; using System.Text.RegularExpressions; namespace PagerParser; public class GpsPosition { + [JsonIgnore] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int GpsPositionId { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } + + private const double EarthRadius = 6_371; + + /// <summary> + /// Get the distance from this point to the specified coordinates + /// </summary> + public double GetDistance(double latitude, double longitude) => + Math.Acos( + Math.Sin(Latitude) * Math.Sin(latitude) + + Math.Cos(Latitude) * Math.Cos(latitude) * + Math.Cos(longitude - Longitude)) * EarthRadius; + + /// <summary> + /// Get the distance from this point to another + /// </summary> + public double GetDistance(GpsPosition position) => + GetDistance(position.Latitude, position.Longitude); } public record MelwaysPage { @@ -56,13 +73,15 @@ public class PositionCalculator { }; public static GpsPosition? GetGpsPosition(ParsedPagerMessage message) { - if(message.MelwaysMapNo is null || message.MelwaysGrid is null) + if(message.MapType != MapType.Melways) + return null; + if(message.MapNo is null || message.MapGrid is null) return null; - var page = MelwaysPages.FirstOrDefault(p => p.PageNo == message.MelwaysMapNo); + var page = MelwaysPages.FirstOrDefault(p => p.PageNo == message.MapNo); if(page is null) return null; - var match = Regex.Match(message.MelwaysGrid.ToUpper(), "^([A-HJ-K])([0-9]+)$"); + var match = Regex.Match(message.MapGrid.ToUpper(), "^([A-HJ-K])([0-9]+)$"); if(!match.Success) return null; |
