Skip to content

Commit d9fcd55

Browse files
committed
imporvements
1 parent 86580da commit d9fcd55

5 files changed

Lines changed: 230 additions & 212 deletions

File tree

src/PolylineAlgorithm/Abstraction/AbstractPolylineEncoder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public TPolyline Encode(ReadOnlySpan<TCoordinate> coordinates) {
9292

9393
try {
9494
for (var i = 0; i < coordinates.Length; i++) {
95+
96+
9597
delta
9698
.Next(
9799
PolylineEncoding.Normalize(GetLatitude(coordinates[i]), CoordinateValueType.Latitude),

src/PolylineAlgorithm/Coordinate.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace PolylineAlgorithm;
1414
using System.Text.Json.Serialization;
1515
#if NET8_0_OR_GREATER
1616
using System.Text;
17+
using System.Linq.Expressions;
1718
#endif
1819

1920
/// <summary>
@@ -55,13 +56,8 @@ public Coordinate() {
5556
/// or when <paramref name="longitude"/> is less than -180 or greater than 180.
5657
/// </exception>
5758
public Coordinate(double latitude, double longitude) {
58-
if (latitude < -90 || latitude > 90 || !double.IsFinite(latitude)) {
59-
throw new ArgumentOutOfRangeException(nameof(latitude), string.Format(CultureInfo.InvariantCulture, _coordinateValueMustBeBetweenValuesMessageFormat, "Latitude", -90, 90));
60-
}
61-
62-
if (longitude < -180 || longitude > 180 || !double.IsFinite(longitude)) {
63-
throw new ArgumentOutOfRangeException(nameof(longitude), string.Format(CultureInfo.InvariantCulture, _coordinateValueMustBeBetweenValuesMessageFormat, "Longitude", -180, 180));
64-
}
59+
Validator.ValidateLatitude(latitude);
60+
Validator.ValidateLongitude(longitude);
6561

6662
Latitude = latitude;
6763
Longitude = longitude;
@@ -128,4 +124,35 @@ public override string ToString() {
128124
/// <see langword="true"/> if the coordinates are not equal; otherwise, <see langword="false"/>.
129125
/// </returns>
130126
public static bool operator !=(Coordinate left, Coordinate right) => !(left == right);
127+
128+
/// <summary>
129+
/// Provides validation methods for latitude and longitude values used in <see cref="Coordinate"/>.
130+
/// </summary>
131+
internal static class Validator {
132+
/// <summary>
133+
/// Validates that the specified latitude is within the valid range of -90 to 90 degrees and is a finite value.
134+
/// </summary>
135+
/// <param name="latitude">The latitude value to validate.</param>
136+
/// <exception cref="ArgumentOutOfRangeException">
137+
/// Thrown when <paramref name="latitude"/> is less than -90, greater than 90, or not a finite value.
138+
/// </exception>
139+
internal static void ValidateLatitude(double latitude) {
140+
if (latitude < -90 || latitude > 90 || !double.IsFinite(latitude)) {
141+
throw new ArgumentOutOfRangeException(nameof(latitude), string.Format(CultureInfo.InvariantCulture, _coordinateValueMustBeBetweenValuesMessageFormat, "Latitude", -90, 90));
142+
}
143+
}
144+
145+
/// <summary>
146+
/// Validates that the specified longitude is within the valid range of -180 to 180 degrees and is a finite value.
147+
/// </summary>
148+
/// <param name="longitude">The longitude value to validate.</param>
149+
/// <exception cref="ArgumentOutOfRangeException">
150+
/// Thrown when <paramref name="longitude"/> is less than -180, greater than 180, or not a finite value.
151+
/// </exception>
152+
internal static void ValidateLongitude(double longitude) {
153+
if (longitude < -180 || longitude > 180 || !double.IsFinite(longitude)) {
154+
throw new ArgumentOutOfRangeException(nameof(longitude), string.Format(CultureInfo.InvariantCulture, _coordinateValueMustBeBetweenValuesMessageFormat, "Longitude", -180, 180));
155+
}
156+
}
157+
}
131158
}

src/PolylineAlgorithm/Internal/Defaults.cs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ internal static class Logging {
2626
/// Contains default values and constants specific to the polyline encoding algorithm.
2727
/// </summary>
2828
internal static class Algorithm {
29-
/// <summary>
30-
/// The precision factor used to round coordinate values during polyline encoding.
31-
/// </summary>
32-
internal const int Precision = 100_000;
3329

3430
/// <summary>
3531
/// The number of bits to shift during polyline encoding.
@@ -69,21 +65,6 @@ internal static class Latitude {
6965
/// The maximum valid latitude value.
7066
/// </summary>
7167
internal const double Max = 90.00000;
72-
73-
/// <summary>
74-
/// Contains constants related to normalized latitude values.
75-
/// </summary>
76-
[SuppressMessage("Critical Code Smell", "S3218:Inner class members should not shadow outer class \"static\" or type members", Justification = "Internal use only.")]
77-
internal static class Normalized {
78-
/// <summary>
79-
/// The minimum normalized latitude value.
80-
/// </summary>
81-
internal const int Min = (int)(Latitude.Min * Algorithm.Precision);
82-
/// <summary>
83-
/// The maximum normalized latitude value.
84-
/// </summary>
85-
internal const int Max = (int)(Latitude.Max * Algorithm.Precision);
86-
}
8768
}
8869

8970
/// <summary>
@@ -102,21 +83,6 @@ internal static class Longitude {
10283
/// The maximum valid longitude value.
10384
/// </summary>
10485
internal const double Max = 180.00000;
105-
106-
/// <summary>
107-
/// Contains constants related to normalized longitude values.
108-
/// </summary>
109-
[SuppressMessage("Critical Code Smell", "S3218:Inner class members should not shadow outer class \"static\" or type members", Justification = "Internal use only.")]
110-
internal static class Normalized {
111-
/// <summary>
112-
/// The minimum normalized latitude value.
113-
/// </summary>
114-
internal const int Min = (int)(Longitude.Min * Algorithm.Precision);
115-
/// <summary>
116-
/// The maximum normalized latitude value.
117-
/// </summary>
118-
internal const int Max = (int)(Longitude.Max * Algorithm.Precision);
119-
}
12086
}
12187
}
12288

@@ -129,18 +95,18 @@ internal static class Polyline {
12995
/// </summary>
13096
internal static class Block {
13197
/// <summary>
132-
/// Contains constants related to the length of encoded coordinates in polyline encoding.
98+
/// Contains constants related to the length of encoded vakues in polyline encoding.
13399
/// </summary>
134100
internal static class Length {
135101
/// <summary>
136-
/// The minimum number of characters required to represent an encoded coordinate.
102+
/// The minimum number of characters required to represent an encoded value.
137103
/// </summary>
138104
internal const int Min = 2;
139105

140106
/// <summary>
141-
/// The maximum number of characters allowed to represent an encoded coordinate.
107+
/// The maximum number of characters allowed to represent an encoded value.
142108
/// </summary>
143-
internal const int Max = 12;
109+
internal const int Max = 14;
144110
}
145111
}
146112
}

0 commit comments

Comments
 (0)