| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Itinero.Geo; |
| | 8 | | using Itinero.Network; |
| | 9 | | using Itinero.Network.Enumerators.Edges; |
| | 10 | | using Itinero.Network.Search.Edges; |
| | 11 | | using Itinero.Network.Search.Islands; |
| | 12 | | using Itinero.Profiles; |
| | 13 | | using Itinero.Routing.Costs; |
| | 14 | |
|
| | 15 | | namespace Itinero.Snapping; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Just like the `Snapper`, it'll snap to a location. |
| | 19 | | /// However, the 'Snapper' will match to any road whereas the `LocationSnapper` will only snap to roads accessible to th |
| | 20 | | /// </summary> |
| | 21 | | internal sealed class Snapper : ISnapper, IEdgeChecker |
| | 22 | | { |
| | 23 | | private readonly RoutingNetwork _routingNetwork; |
| | 24 | | private readonly bool _anyProfile; |
| | 25 | | private readonly bool _checkCanStopOn; |
| | 26 | | private readonly double _offsetInMeter; |
| | 27 | | private readonly double _offsetInMeterMax; |
| | 28 | | private readonly double _maxDistance; |
| | 29 | | private readonly Islands[] _islands; |
| | 30 | | private readonly ICostFunction[] _costFunctions; |
| | 31 | | private readonly Profile[] _profiles; |
| | 32 | |
|
| 69 | 33 | | public Snapper(RoutingNetwork routingNetwork, IEnumerable<Profile> profiles, bool anyProfile, bool checkCanStopOn, d |
| 69 | 34 | | { |
| 69 | 35 | | _routingNetwork = routingNetwork; |
| 69 | 36 | | _anyProfile = anyProfile; |
| 69 | 37 | | _checkCanStopOn = checkCanStopOn; |
| 69 | 38 | | _offsetInMeter = offsetInMeter; |
| 69 | 39 | | _offsetInMeterMax = offsetInMeterMax; |
| 69 | 40 | | _maxDistance = maxDistance; |
| 69 | 41 | | _profiles = profiles.ToArray(); |
| | 42 | |
|
| 69 | 43 | | _costFunctions = _profiles.Select(_routingNetwork.GetCostFunctionFor).ToArray(); |
| 69 | 44 | | _islands = routingNetwork.IslandManager.MaxIslandSize == 0 ? [] : _profiles.Select(p => _routingNetwork.IslandMa |
| 69 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <inheritdoc/> |
| | 48 | | public async IAsyncEnumerable<Result<SnapPoint>> ToAsync(VertexId vertexId, bool asDeparture = true) |
| 48 | 49 | | { |
| 48 | 50 | | var enumerator = _routingNetwork.GetEdgeEnumerator(); |
| 48 | 51 | | RoutingNetworkEdgeEnumerator? secondEnumerator = null; |
| | 52 | |
|
| 48 | 53 | | if (!enumerator.MoveTo(vertexId)) |
| 1 | 54 | | { |
| 1 | 55 | | yield break; |
| | 56 | | } |
| | 57 | |
|
| 51 | 58 | | while (enumerator.MoveNext()) |
| 48 | 59 | | { |
| 48 | 60 | | if (_costFunctions.Length == 0) |
| 48 | 61 | | { |
| 48 | 62 | | if (enumerator.Forward) |
| 24 | 63 | | { |
| 24 | 64 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, 0)); |
| 2 | 65 | | } |
| | 66 | | else |
| 24 | 67 | | { |
| 24 | 68 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, ushort.MaxValue)); |
| 2 | 69 | | } |
| 4 | 70 | | } |
| | 71 | | else |
| 0 | 72 | | { |
| 0 | 73 | | if (asDeparture) |
| 0 | 74 | | { |
| 0 | 75 | | if (!(this.IsAcceptable(enumerator) ?? await (this as IEdgeChecker).RunCheckAsync(enumerator, defaul |
| 0 | 76 | | { |
| | 77 | |
|
| 0 | 78 | | continue; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | if (enumerator.Forward) |
| 0 | 82 | | { |
| 0 | 83 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, 0)); |
| 0 | 84 | | } |
| | 85 | | else |
| 0 | 86 | | { |
| 0 | 87 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, ushort.MaxValue)); |
| 0 | 88 | | } |
| 0 | 89 | | } |
| | 90 | | else |
| 0 | 91 | | { |
| 0 | 92 | | secondEnumerator ??= _routingNetwork.GetEdgeEnumerator(); |
| 0 | 93 | | secondEnumerator.MoveTo(enumerator.EdgeId, !enumerator.Forward); |
| 0 | 94 | | if (!(this.IsAcceptable(secondEnumerator) ?? await (this as IEdgeChecker).RunCheckAsync(secondEnumer |
| 0 | 95 | | { |
| 0 | 96 | | continue; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | if (enumerator.Forward) |
| 0 | 100 | | { |
| 0 | 101 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, 0)); |
| 0 | 102 | | } |
| | 103 | | else |
| 0 | 104 | | { |
| 0 | 105 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, ushort.MaxValue)); |
| 0 | 106 | | } |
| 0 | 107 | | } |
| 0 | 108 | | } |
| 4 | 109 | | } |
| 48 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <inheritdoc/> |
| | 113 | | public async Task<Result<SnapPoint>> ToAsync(EdgeId edgeId, ushort offset, bool forward = true) |
| 0 | 114 | | { |
| 0 | 115 | | var enumerator = _routingNetwork.GetEdgeEnumerator(); |
| | 116 | |
|
| 0 | 117 | | if (!enumerator.MoveTo(edgeId, forward)) return new Result<SnapPoint>("Edge not found"); |
| | 118 | |
|
| 0 | 119 | | if (!(this.IsAcceptable(enumerator) ?? await (this as IEdgeChecker).RunCheckAsync(enumerator, default))) |
| 0 | 120 | | return new Result<SnapPoint>("Edge cannot be snapped to by configured profiles in the given direction"); |
| | 121 | |
|
| 0 | 122 | | return new Result<SnapPoint>(new SnapPoint(edgeId, offset)); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <inheritdoc/> |
| | 126 | | public async Task<Result<SnapPoint>> ToAsync( |
| | 127 | | double longitude, double latitude, |
| | 128 | | CancellationToken cancellationToken = default) |
| 21 | 129 | | { |
| 21 | 130 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | 131 | |
|
| | 132 | | // calculate one box for all locations. |
| 21 | 133 | | var box = location.BoxAround(_offsetInMeter); |
| | 134 | |
|
| | 135 | | // make sure data is loaded. |
| 21 | 136 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | 137 | |
|
| | 138 | | // snap to closest edge. |
| 21 | 139 | | var snapPoint = await _routingNetwork.SnapInBoxAsync(box, this, maxDistance: _maxDistance, cancellationToken); |
| 42 | 140 | | if (snapPoint.EdgeId != EdgeId.Empty) return snapPoint; |
| | 141 | |
|
| | 142 | | // retry only if requested. |
| 0 | 143 | | if (!(_offsetInMeter < _offsetInMeterMax)) |
| 0 | 144 | | { |
| 0 | 145 | | return new Result<SnapPoint>( |
| 0 | 146 | | FormattableString.Invariant($"Could not snap to location: {location.longitude},{location.latitude}")); |
| | 147 | | } |
| | 148 | |
|
| | 149 | | // use bigger box. |
| 0 | 150 | | box = location.BoxAround(_offsetInMeterMax); |
| | 151 | |
|
| | 152 | | // make sure data is loaded. |
| 0 | 153 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, |
| 0 | 154 | | cancellationToken); |
| | 155 | |
|
| | 156 | | // snap to closest edge. |
| 0 | 157 | | snapPoint = await _routingNetwork.SnapInBoxAsync(box, this, maxDistance: _maxDistance, cancellationToken); |
| 0 | 158 | | if (snapPoint.EdgeId != EdgeId.Empty) |
| 0 | 159 | | { |
| 0 | 160 | | return snapPoint; |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | return new Result<SnapPoint>( |
| 0 | 164 | | FormattableString.Invariant($"Could not snap to location: {location.longitude},{location.latitude}")); |
| 21 | 165 | | } |
| | 166 | |
|
| | 167 | | /// <inheritdoc/> |
| | 168 | | public async IAsyncEnumerable<SnapPoint> ToAllAsync(double longitude, double latitude, [EnumeratorCancellation] Canc |
| 0 | 169 | | { |
| | 170 | | // calculate one box for all locations. |
| 0 | 171 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| 0 | 172 | | var box = location.BoxAround(_offsetInMeter); |
| | 173 | |
|
| | 174 | | // make sure data is loaded. |
| 0 | 175 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | 176 | |
|
| | 177 | | // snap all. |
| 0 | 178 | | var snapped = _routingNetwork.SnapAllInBoxAsync(box, this, maxDistance: _maxDistance, cancellationToken: cancell |
| 0 | 179 | | await foreach (var snapPoint in snapped) |
| 0 | 180 | | { |
| 0 | 181 | | yield return snapPoint; |
| 0 | 182 | | } |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | /// <inheritdoc/> |
| | 186 | | public async Task<Result<VertexId>> ToVertexAsync(double longitude, double latitude, CancellationToken cancellationT |
| 0 | 187 | | { |
| 0 | 188 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | 189 | |
|
| | 190 | | // calculate one box for all locations. |
| 0 | 191 | | var box = location.BoxAround(_maxDistance); |
| | 192 | |
|
| | 193 | | // make sure data is loaded. |
| 0 | 194 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | 195 | |
|
| | 196 | | // snap to closest vertex. |
| 0 | 197 | | var vertex = await _routingNetwork.SnapToVertexInBoxAsync(box, _costFunctions.Length > 0 ? this : null, maxDista |
| 0 | 198 | | if (vertex.IsEmpty()) return new Result<VertexId>("No vertex in range found"); |
| | 199 | |
|
| 0 | 200 | | return vertex; |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | /// <inheritdoc/> |
| | 204 | | public async IAsyncEnumerable<VertexId> ToAllVerticesAsync(double longitude, double latitude, |
| | 205 | | [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 0 | 206 | | { |
| 0 | 207 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | 208 | |
|
| | 209 | | // calculate one box for all locations. |
| 0 | 210 | | var box = location.BoxAround(_maxDistance); |
| | 211 | |
|
| | 212 | | // make sure data is loaded. |
| 0 | 213 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | 214 | |
|
| | 215 | | // snap to closest vertex. |
| 0 | 216 | | await foreach (var vertex in _routingNetwork.SnapToAllVerticesInBoxAsync(box, this, |
| 0 | 217 | | maxDistance: _maxDistance, cancellationToken: cancellationToken)) |
| 0 | 218 | | { |
| 0 | 219 | | yield return vertex; |
| 0 | 220 | | } |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | private bool? IsAcceptable(IEdgeEnumerator<RoutingNetwork> edgeEnumerator) |
| 23 | 224 | | { |
| 23 | 225 | | var hasProfiles = _costFunctions.Length > 0; |
| 46 | 226 | | if (!hasProfiles) return true; |
| | 227 | |
|
| 0 | 228 | | var allOk = true; |
| 0 | 229 | | for (var p = 0; p < _costFunctions.Length; p++) |
| 0 | 230 | | { |
| 0 | 231 | | var costFunction = _costFunctions[p]; |
| | 232 | |
|
| | 233 | | // check for the positive case, can the edge be used in the forward direction. |
| | 234 | | // the backward direction is also done later in the snapping code. |
| 0 | 235 | | var costs = costFunction.Get(edgeEnumerator, true, |
| 0 | 236 | | []); |
| | 237 | |
|
| | 238 | | // if edge is not accessible, no need to look any further. |
| 0 | 239 | | if (!costs.canAccess) |
| 0 | 240 | | { |
| 0 | 241 | | allOk = false; |
| 0 | 242 | | continue; |
| | 243 | | } |
| | 244 | |
|
| | 245 | | // check if needed if the edge can be stopped on. |
| 0 | 246 | | if (_checkCanStopOn) |
| 0 | 247 | | { |
| 0 | 248 | | if (!costs.canStop) |
| 0 | 249 | | { |
| 0 | 250 | | allOk = false; |
| 0 | 251 | | continue; |
| | 252 | | } |
| 0 | 253 | | } |
| | 254 | |
|
| | 255 | | // check if the edge is on an island. |
| | 256 | | // if the result is inclusive null is returned and islands will be built. |
| 0 | 257 | | var tailIsland = edgeEnumerator.Tail.TileId; |
| 0 | 258 | | if (!edgeEnumerator.Forward) tailIsland = edgeEnumerator.Head.TileId; |
| 0 | 259 | | var islands = _islands[p]; |
| | 260 | |
|
| | 261 | | // when an edge is not an island, it is sure it is not an island. |
| 0 | 262 | | var onIsland = islands.IsEdgeOnIsland(edgeEnumerator.EdgeId); |
| 0 | 263 | | if (onIsland) |
| 0 | 264 | | { |
| 0 | 265 | | allOk = false; |
| 0 | 266 | | continue; |
| | 267 | | } |
| | 268 | |
|
| | 269 | | // if it is not on an island we need to check if the tile was done. |
| 0 | 270 | | if (!islands.GetTileDone(tailIsland)) return null; // inconclusive. |
| | 271 | |
|
| | 272 | | // any profile is good for a positive result. |
| 0 | 273 | | if (_anyProfile) return true; |
| 0 | 274 | | } |
| | 275 | |
|
| 0 | 276 | | return allOk; |
| 23 | 277 | | } |
| | 278 | |
|
| | 279 | | bool? IEdgeChecker.IsAcceptable(IEdgeEnumerator<RoutingNetwork> edgeEnumerator) |
| 23 | 280 | | { |
| 23 | 281 | | return this.IsAcceptable(edgeEnumerator); |
| 23 | 282 | | } |
| | 283 | |
|
| | 284 | | async Task<bool> IEdgeChecker.RunCheckAsync(IEdgeEnumerator<RoutingNetwork> edgeEnumerator, CancellationToken cancel |
| 0 | 285 | | { |
| 0 | 286 | | foreach (var profile in _profiles) |
| 0 | 287 | | { |
| 0 | 288 | | var tileId = edgeEnumerator.Forward ? edgeEnumerator.Tail.TileId : edgeEnumerator.Head.TileId; |
| 0 | 289 | | await _routingNetwork.IslandManager.BuildForTileAsync(_routingNetwork, profile, tileId, cancellationToken); |
| 0 | 290 | | if (cancellationToken.IsCancellationRequested) return true; |
| 0 | 291 | | } |
| | 292 | |
|
| 0 | 293 | | return (this as IEdgeChecker).IsAcceptable(edgeEnumerator) ?? throw new Exception("Edges were just calculated"); |
| 0 | 294 | | } |
| | 295 | | } |