-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPolylineEncoderExtensions.cs
More file actions
84 lines (75 loc) · 3.84 KB
/
PolylineEncoderExtensions.cs
File metadata and controls
84 lines (75 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// Copyright © Pete Sramek. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace PolylineAlgorithm.Extensions;
using PolylineAlgorithm.Abstraction;
using PolylineAlgorithm.Internal.Diagnostics;
using System;
using System.Collections.Generic;
#if NET5_0_OR_GREATER
using System.Runtime.InteropServices;
#endif
/// <summary>
/// Provides extension methods for the <see cref="IPolylineEncoder{TCoordinate, TPolyline}"/> interface to facilitate encoding geographic coordinates into polylines.
/// </summary>
public static class PolylineEncoderExtensions {
/// <summary>
/// Encodes a <see cref="List{T}"/> of <typeparamref name="TCoordinate"/> instances into an encoded polyline.
/// </summary>
/// <typeparam name="TCoordinate">The type that represents a geographic coordinate to encode.</typeparam>
/// <typeparam name="TPolyline">The type that represents the encoded polyline output.</typeparam>
/// <param name="encoder">
/// The <see cref="IPolylineEncoder{TCoordinate, TPolyline}"/> instance used to perform the encoding operation.
/// </param>
/// <param name="coordinates">
/// The list of <typeparamref name="TCoordinate"/> objects to encode.
/// </param>
/// <returns>
/// A <typeparamref name="TPolyline"/> instance representing the encoded polyline for the provided coordinates.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="encoder"/> or <paramref name="coordinates"/> is <see langword="null"/>.
/// </exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1002:Do not expose generic lists", Justification = "We need a list as we do need to marshal it as span.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "MA0016:Prefer using collection abstraction instead of implementation", Justification = "We need a list as we do need to marshal it as span.")]
public static TPolyline Encode<TCoordinate, TPolyline>(this IPolylineEncoder<TCoordinate, TPolyline> encoder, List<TCoordinate> coordinates) {
if (encoder is null) {
ExceptionGuard.ThrowArgumentNull(nameof(encoder));
}
if (coordinates is null) {
ExceptionGuard.ThrowArgumentNull(nameof(coordinates));
}
#if NET5_0_OR_GREATER
return encoder.Encode(CollectionsMarshal.AsSpan(coordinates));
#else
return encoder.Encode([.. coordinates]);
#endif
}
/// <summary>
/// Encodes an array of <typeparamref name="TCoordinate"/> instances into an encoded polyline.
/// </summary>
/// <typeparam name="TCoordinate">The type that represents a geographic coordinate to encode.</typeparam>
/// <typeparam name="TPolyline">The type that represents the encoded polyline output.</typeparam>
/// <param name="encoder">
/// The <see cref="IPolylineEncoder{TCoordinate, TPolyline}"/> instance used to perform the encoding operation.
/// </param>
/// <param name="coordinates">
/// The array of <typeparamref name="TCoordinate"/> objects to encode.
/// </param>
/// <returns>
/// A <typeparamref name="TPolyline"/> instance representing the encoded polyline for the provided coordinates.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="encoder"/> or <paramref name="coordinates"/> is <see langword="null"/>.
/// </exception>
public static TPolyline Encode<TCoordinate, TPolyline>(this IPolylineEncoder<TCoordinate, TPolyline> encoder, TCoordinate[] coordinates) {
if (encoder is null) {
ExceptionGuard.ThrowArgumentNull(nameof(encoder));
}
if (coordinates is null) {
ExceptionGuard.ThrowArgumentNull(nameof(coordinates));
}
return encoder.Encode(coordinates.AsSpan());
}
}