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