aboutsummaryrefslogtreecommitdiff
path: root/PositionCalculator.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2024-09-20 16:21:09 +1000
committerJake Mannens <jake@asger.xyz>2024-10-29 11:42:54 +1100
commite8e3c4cba8ffa0056e984c113cfbb75319e00022 (patch)
tree8336315e61f9e77207276d478b25fe5dc7c3d06c /PositionCalculator.cs
parent0e21907c76dbefed11f382bcf949143f0716567f (diff)
v0.4-rc1v0.4-rc1
Diffstat (limited to 'PositionCalculator.cs')
-rw-r--r--PositionCalculator.cs29
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;