Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions src/AprsParser/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,35 +198,6 @@ public void Decode(string coords)
}
}

private static int DecodeBase91(string encoded)
{
var bytes = Encoding.ASCII.GetBytes(encoded);

var result = 0;
for (var i = 0; i < bytes.Length; i++)
{
result += i == bytes.Length - 1
? bytes[i] - 33
: (bytes[i] - 33) * (int)Math.Pow(91, bytes.Length - i - 1);
}

return result;
}

private static double DecodeCompressedLatitude(string coords)
{
Debug.Assert(coords.Length == 4, "Compressed latitude must be 4 characters");
var latitude = 90 - (DecodeBase91(coords) / 380926.0);
return Math.Round(latitude, 4);
}

private static double DecodeCompressedLongitude(string coords)
{
Debug.Assert(coords.Length == 4, "Compressed longitude must be 4 characters");
var longitude = -180 + (DecodeBase91(coords) / 190463.0);
return Math.Round(longitude, 4);
}

/// <summary>
/// Decode from Maidenhead Location System gridsquare to a point in the middle of the gridsquare.
/// </summary>
Expand Down Expand Up @@ -477,6 +448,35 @@ public string EncodeLongitude()
return EncodeCoordinates(CoordinateSystem.Longitude);
}

private static int DecodeBase91(string encoded)
{
var bytes = Encoding.ASCII.GetBytes(encoded);

var result = 0;
for (var i = 0; i < bytes.Length; i++)
{
result += i == bytes.Length - 1
? bytes[i] - 33
: (bytes[i] - 33) * (int)Math.Pow(91, bytes.Length - i - 1);
}

return result;
}

private static double DecodeCompressedLatitude(string coords)
{
Debug.Assert(coords.Length == 4, "Compressed latitude must be 4 characters");
var latitude = 90 - (DecodeBase91(coords) / 380926.0);
return Math.Round(latitude, 4);
}

private static double DecodeCompressedLongitude(string coords)
{
Debug.Assert(coords.Length == 4, "Compressed longitude must be 4 characters");
var longitude = -180 + (DecodeBase91(coords) / 190463.0);
return Math.Round(longitude, 4);
}

/// <summary>
/// Encode either the latitude or longitude part of a Maidenhead gridsquare.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/AprsParser/RegexStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal static class RegexStrings
/// Compressed Longitude
/// Symbol code
/// Compressed one of: course/speed, radio range, or altitude
/// Compressed data type
/// Compressed data type.
/// </summary>
public const string CompressedPosition = @"(.)(.{4})(.{4})(.)(.{2})(.)";

Expand Down