| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.CompilerServices; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Itinero.Network; |
| | 7 | |
|
| | 8 | | namespace Itinero.Snapping; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Contains extensions for ILocations |
| | 12 | | /// </summary> |
| | 13 | | public static class ISnapperExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Snap to the given location. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="snapper">The snappable.</param> |
| | 19 | | /// <param name="location">The location.</param> |
| | 20 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 21 | | /// <returns></returns> |
| | 22 | | public static async Task<Result<SnapPoint>> ToAsync(this ISnapper snapper, |
| | 23 | | (double longitude, double latitude, float? e) location, CancellationToken cancellationToken = default) |
| 21 | 24 | | { |
| 21 | 25 | | return await snapper.ToAsync(location.longitude, location.latitude, cancellationToken); |
| 21 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Snaps to the given locations. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="snapper"></param> |
| | 32 | | /// <param name="locations"></param> |
| | 33 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 34 | | /// <returns></returns> |
| | 35 | | public static async IAsyncEnumerable<Result<SnapPoint>> ToAsync(this ISnapper snapper, IEnumerable<(double longitude |
| | 36 | | [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 0 | 37 | | { |
| 0 | 38 | | foreach (var location in locations) |
| 0 | 39 | | { |
| 0 | 40 | | yield return await snapper.ToAsync(location, cancellationToken: cancellationToken); |
| 0 | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Snap to the given location. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="snapper">The snappable.</param> |
| | 48 | | /// <param name="location">The location.</param> |
| | 49 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 50 | | /// <returns></returns> |
| | 51 | | public static async Task<Result<VertexId>> ToVertexAsync(this ISnapper snapper, |
| | 52 | | (double longitude, double latitude, float? e) location, CancellationToken cancellationToken = default) |
| 0 | 53 | | { |
| 0 | 54 | | return await snapper.ToVertexAsync(location.longitude, location.latitude, cancellationToken); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Snaps to the given locations. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="snapper">The locations snapper.</param> |
| | 61 | | /// <param name="locations">The locations.</param> |
| | 62 | | /// <returns>The vertices snapped to.</returns> |
| | 63 | | public static async IAsyncEnumerable<Result<VertexId>> ToVerticesAsync(this ISnapper snapper, IEnumerable<(double lo |
| 0 | 64 | | { |
| 0 | 65 | | foreach (var location in locations) |
| 0 | 66 | | { |
| 0 | 67 | | yield return await snapper.ToVertexAsync(location); |
| 0 | 68 | | } |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Snaps to the given vertex and edge exactly. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="snapper">The snapper.</param> |
| | 75 | | /// <param name="vertexId">The vertex to snap to.</param> |
| | 76 | | /// <param name="edgeId">The edge.</param> |
| | 77 | | /// <param name="asDeparture">When this has a value, any edge will be checked against the configured profile(s) as s |
| | 78 | | /// <returns>The result if any. Snapping will fail if a vertex has no edges.</returns> |
| | 79 | | public static async Task<Result<SnapPoint>> ToExactAsync(this ISnapper snapper, VertexId vertexId, EdgeId edgeId, bo |
| 4 | 80 | | { |
| 4 | 81 | | var result = snapper.ToAsync(vertexId, asDeparture); |
| 20 | 82 | | await foreach (var result1 in result) |
| 6 | 83 | | { |
| 10 | 84 | | if (result1.Value.EdgeId == edgeId) return result1; |
| 2 | 85 | | } |
| 0 | 86 | | return new Result<SnapPoint>("Vertex probably does not have edge as neighbour or edge id not snappable by config |
| 4 | 87 | | } |
| | 88 | | } |