Skip to content

Commit

Permalink
refactor: GeoSpatial classes (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb authored Aug 24, 2024
1 parent 5d4214e commit 71a4778
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Axuno.Tools/GeoSpatial/Latitude.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System;
using System.Globalization;

namespace Axuno.Tools.GeoSpatial;

Expand Down
21 changes: 12 additions & 9 deletions Axuno.Tools/GeoSpatial/Location.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Axuno.Tools.GeoSpatial;
/// </summary>
public partial class Location
{
// Default timeout value for the regular expressions
private static int _regExTimeout = 1000;

/// <summary>Parsing latitude and longitude information.</summary>
/// <example>
/// User input:
Expand Down Expand Up @@ -83,15 +86,15 @@ private static class Parser
private const RegexOptions Options =
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase;

private static readonly Regex DegreeRegex = new(DegreePattern, Options);
private static readonly Regex DegreeRegex = new(DegreePattern, Options, TimeSpan.FromMilliseconds(_regExTimeout));

private static readonly Regex DegreeMinuteRegex = new(DegreeMinutePattern, Options);
private static readonly Regex DegreeMinuteRegex = new(DegreeMinutePattern, Options, TimeSpan.FromMilliseconds(_regExTimeout));

private static readonly Regex DegreeMinuteSecondRegex = new(DegreeMinuteSecondPattern, Options);
private static readonly Regex DegreeMinuteSecondRegex = new(DegreeMinuteSecondPattern, Options, TimeSpan.FromMilliseconds(_regExTimeout));

private static readonly Regex IsoRegex =
new(IsoPattern,
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace);
RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(_regExTimeout));

private static Location? CreateLocation(Angle? latitude, Angle? longitude, double? altitude)
{
Expand Down Expand Up @@ -216,7 +219,7 @@ private static double MakeSameSign(double source, double value)
}

/// <summary>
/// Parses the input string for a value containg a pair of degree
/// Parses the input string for a value containing a pair of degree
/// minute second values.
/// </summary>
/// <param name="value">The input to parse.</param>
Expand Down Expand Up @@ -278,13 +281,13 @@ private static double MakeSameSign(double source, double value)
break;
case 3: // sign + MM : value is degrees and minutes
angle = Angle.FromDegrees(
int.Parse(value.Substring(1, degreeDigits), CultureInfo.InvariantCulture),
int.Parse(value.AsSpan(1, degreeDigits), CultureInfo.InvariantCulture),
double.Parse(value[(degreeDigits + 1)..], CultureInfo.InvariantCulture));
break;
case 5: // sign + MM + SS : value is degrees, minutes and seconds
angle = Angle.FromDegrees(
int.Parse(value.Substring(1, degreeDigits), CultureInfo.InvariantCulture),
int.Parse(value.Substring(degreeDigits + 1, 2), CultureInfo.InvariantCulture),
int.Parse(value.AsSpan(1, degreeDigits), CultureInfo.InvariantCulture),
int.Parse(value.AsSpan(degreeDigits + 1, 2), CultureInfo.InvariantCulture),
double.Parse(value[(degreeDigits + 3)..], CultureInfo.InvariantCulture));
break;
default:
Expand All @@ -301,7 +304,7 @@ private static bool TryGetValue(Match match, string groupName, out string? resul
var group = match.Groups[groupName];

// Need to check that only a single capture occurred, as the suffixes are used more than once
if (group.Success && group.Captures.Count == 1)
if (group is { Success: true, Captures.Count: 1 })
{
result = group.Value;
return true;
Expand Down
2 changes: 1 addition & 1 deletion Axuno.Tools/GeoSpatial/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public Angle Azimuth(Location point)

/// <summary>
/// Calculates the great circle distance, in meters, between this instance
/// and the specified value.
/// and the specified value, using the Haversine formula.
/// </summary>
/// <param name="point">The location of the other point.</param>
/// <returns>The great circle distance, in meters.</returns>
Expand Down
3 changes: 2 additions & 1 deletion Axuno.Tools/GeoSpatial/LocationCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Globalization;
using System.Text;
using System.Xml;
Expand Down
2 changes: 1 addition & 1 deletion Axuno.Tools/GeoSpatial/LocationStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ public enum LocationStyles
/// and Iso styles are used.
/// </summary>
Any = Degrees | DegreesMinutes | DegreesMinutesSeconds | Iso
}
}
7 changes: 5 additions & 2 deletions Axuno.Tools/GeoSpatial/MaidenheadLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace Axuno.Tools.GeoSpatial;
/// </remarks>
public class MaidenheadLocator
{
// Default timeout for regular expressions
private static int _regExTimeout = 1000;

/// <summary>
/// Checks whether a string is a valid Maidenhead locator
/// </summary>
/// <param name="locator">A Maidenhead locator string</param>
/// <returns>Return true, if the string is a valid Maidenhead locator</returns>
public static bool IsLocator(string locator)
{
return Regex.IsMatch(locator, "^[A-R]{2}[0-9]{2}[A-X]{2}$");
return Regex.IsMatch(locator, "^[A-R]{2}[0-9]{2}[A-X]{2}$", RegexOptions.None, TimeSpan.FromMicroseconds(_regExTimeout));
}

/// <summary>
Expand Down Expand Up @@ -95,4 +98,4 @@ public static Location LocatorToLocation(string locator)

return location;
}
}
}

0 comments on commit 71a4778

Please sign in to comment.