From e8e3c4cba8ffa0056e984c113cfbb75319e00022 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Fri, 20 Sep 2024 16:21:09 +1000 Subject: v0.4-rc1 --- PositionCalculator.cs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'PositionCalculator.cs') 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; + + /// + /// Get the distance from this point to the specified coordinates + /// + 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; + + /// + /// Get the distance from this point to another + /// + 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; -- cgit v1.3