|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using HotChocolate.Language; |
| 5 | +using static System.Math; |
| 6 | + |
| 7 | +namespace HotChocolate.Types |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// The `LongitudeType` scalar represents a valid decimal degrees longitude number. |
| 11 | + /// <a href="https://en.wikipedia.org/wiki/Longitude">Read More</a> |
| 12 | + /// </summary> |
| 13 | + public class LongitudeType : ScalarType<double, StringValueNode> |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of <see cref="LongitudeType"/> |
| 17 | + /// </summary> |
| 18 | + public LongitudeType() |
| 19 | + : this( |
| 20 | + WellKnownScalarTypes.Longitude, |
| 21 | + ScalarResources.LongitudeType_Description) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of <see cref="LongitudeType"/> |
| 27 | + /// </summary> |
| 28 | + public LongitudeType( |
| 29 | + NameString name, |
| 30 | + string? description = null, |
| 31 | + BindingBehavior bind = BindingBehavior.Explicit) |
| 32 | + : base(name, bind) |
| 33 | + { |
| 34 | + Description = description; |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + protected override bool IsInstanceOfType(double runtimeValue) => |
| 39 | + Longitude.IsValid(runtimeValue); |
| 40 | + |
| 41 | + /// <inheritdoc /> |
| 42 | + public override IValueNode ParseResult(object? resultValue) |
| 43 | + { |
| 44 | + return resultValue switch |
| 45 | + { |
| 46 | + null => NullValueNode.Default, |
| 47 | + |
| 48 | + string s when Longitude.TryDeserialize(s, out var runtimeValue) => |
| 49 | + ParseValue(runtimeValue), |
| 50 | + |
| 51 | + int i => ParseValue(i), |
| 52 | + |
| 53 | + double d => ParseValue(d), |
| 54 | + |
| 55 | + _ => throw ThrowHelper.LongitudeType_ParseValue_IsInvalid(this) |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + /// <inheritdoc /> |
| 60 | + protected override double ParseLiteral(StringValueNode valueSyntax) |
| 61 | + { |
| 62 | + if (Longitude.TryDeserialize(valueSyntax.Value, out var runtimeValue)) |
| 63 | + { |
| 64 | + return runtimeValue.Value; |
| 65 | + } |
| 66 | + |
| 67 | + throw ThrowHelper.LongitudeType_ParseLiteral_IsInvalid(this); |
| 68 | + } |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + protected override StringValueNode ParseValue(double runtimeValue) |
| 72 | + { |
| 73 | + if (Longitude.TrySerialize(runtimeValue, out var s)) |
| 74 | + { |
| 75 | + return new StringValueNode(s); |
| 76 | + } |
| 77 | + |
| 78 | + throw ThrowHelper.LongitudeType_ParseValue_IsInvalid(this); |
| 79 | + } |
| 80 | + |
| 81 | + /// <inheritdoc /> |
| 82 | + public override bool TrySerialize(object? runtimeValue, out object? resultValue) |
| 83 | + { |
| 84 | + switch (runtimeValue) |
| 85 | + { |
| 86 | + case double d when Longitude.TrySerialize(d, out var serializedDouble): |
| 87 | + resultValue = serializedDouble; |
| 88 | + return true; |
| 89 | + |
| 90 | + case int i when Longitude.TrySerialize(i, out var serializedInt): |
| 91 | + resultValue = serializedInt; |
| 92 | + return true; |
| 93 | + |
| 94 | + default: |
| 95 | + resultValue = null; |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// <inheritdoc /> |
| 101 | + public override bool TryDeserialize(object? resultValue, out object? runtimeValue) |
| 102 | + { |
| 103 | + if (resultValue is string s && Longitude.TryDeserialize(s, out var value)) |
| 104 | + { |
| 105 | + runtimeValue = value; |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + runtimeValue = null; |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + private static class Longitude |
| 114 | + { |
| 115 | + private const double _min = -180.0; |
| 116 | + private const double _max = 180.0; |
| 117 | + private const int _maxPrecision = 8; |
| 118 | + |
| 119 | + private const string _sexagesimalRegex = |
| 120 | + "^([0-9]{1,3})°\\s*([0-9]{1,3}(?:\\.(?:[0-9]{1,}))?)['′]\\s*(([0-9]{1,3}" + |
| 121 | + "(\\.([0-9]{1,}))?)[\"″]\\s*)?([NEOSW]?)$"; |
| 122 | + |
| 123 | + private static readonly Regex _validationPattern = |
| 124 | + new(_sexagesimalRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase); |
| 125 | + |
| 126 | + public static bool IsValid(double value) => value is > _min and < _max; |
| 127 | + |
| 128 | + public static bool TryDeserialize( |
| 129 | + string serialized, |
| 130 | + [NotNullWhen(true)] out double? value) |
| 131 | + { |
| 132 | + MatchCollection coords = _validationPattern.Matches(serialized); |
| 133 | + if (coords.Count > 0) |
| 134 | + { |
| 135 | + var minute = double.TryParse(coords[0].Groups[2].Value, out var min) |
| 136 | + ? min / 60 |
| 137 | + : 0; |
| 138 | + var second = double.TryParse(coords[0].Groups[4].Value, out var sec) |
| 139 | + ? sec / 3600 |
| 140 | + : 0; |
| 141 | + var degree = double.Parse(coords[0].Groups[1].Value); |
| 142 | + var result = degree + minute + second; |
| 143 | + |
| 144 | + // Southern and western coordinates must be negative decimals |
| 145 | + var deserialized = coords[0].Groups[7].Value is "W" or "S" ? -result : result; |
| 146 | + |
| 147 | + value = deserialized; |
| 148 | + return IsValid(deserialized); |
| 149 | + } |
| 150 | + |
| 151 | + value = null; |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + public static bool TrySerialize( |
| 156 | + double runtimeValue, |
| 157 | + [NotNullWhen(true)] out string? resultValue) |
| 158 | + { |
| 159 | + if (IsValid(runtimeValue)) |
| 160 | + { |
| 161 | + var degree = runtimeValue >= 0 |
| 162 | + ? Floor(runtimeValue) |
| 163 | + : Ceiling(runtimeValue); |
| 164 | + var degreeDecimals = runtimeValue - degree; |
| 165 | + |
| 166 | + var minutesWhole = degreeDecimals * 60; |
| 167 | + var minutes = minutesWhole >= 0 |
| 168 | + ? Floor(minutesWhole) |
| 169 | + : Ceiling(minutesWhole); |
| 170 | + var minutesDecimal = minutesWhole - minutes; |
| 171 | + |
| 172 | + var seconds = |
| 173 | + Round(minutesDecimal * 60, _maxPrecision, MidpointRounding.AwayFromZero); |
| 174 | + |
| 175 | + string serializedLatitude = degree switch |
| 176 | + { |
| 177 | + >= 0 and < _max => $"{degree}° {minutes}' {seconds}\" E", |
| 178 | + < 0 and > _min => $"{Abs(degree)}° {Abs(minutes)}' {Abs(seconds)}\" W", |
| 179 | + _ => $"{degree}° {minutes}' {seconds}\"" |
| 180 | + }; |
| 181 | + |
| 182 | + resultValue = serializedLatitude; |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
| 186 | + resultValue = null; |
| 187 | + return false; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments