| | | 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 | | |
| | 177 | 34 | | public Snapper(RoutingNetwork routingNetwork, IEnumerable<Profile> profiles, bool anyProfile, bool checkCanStopOn, d |
| | 177 | 35 | | { |
| | 177 | 36 | | _routingNetwork = routingNetwork; |
| | 177 | 37 | | _anyProfile = anyProfile; |
| | 177 | 38 | | _checkCanStopOn = checkCanStopOn; |
| | 177 | 39 | | _offsetInMeter = offsetInMeter; |
| | 177 | 40 | | _offsetInMeterMax = offsetInMeterMax; |
| | 177 | 41 | | _maxDistance = maxDistance; |
| | 177 | 42 | | _profiles = profiles.ToArray(); |
| | | 43 | | |
| | 177 | 44 | | _costFunctions = _profiles.Select(_routingNetwork.GetCostFunctionFor).ToArray(); |
| | 185 | 45 | | _islands = routingNetwork.IslandManager.MaxIslandSize == 0 ? [] : _profiles.Select(p => _routingNetwork.IslandMa |
| | 177 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc/> |
| | | 49 | | public async IAsyncEnumerable<Result<SnapPoint>> ToAsync(VertexId vertexId, bool asDeparture = true) |
| | 59 | 50 | | { |
| | 59 | 51 | | var enumerator = _routingNetwork.GetEdgeEnumerator(); |
| | 59 | 52 | | RoutingNetworkEdgeEnumerator? secondEnumerator = null; |
| | | 53 | | |
| | 59 | 54 | | if (!enumerator.MoveTo(vertexId)) |
| | 1 | 55 | | { |
| | 1 | 56 | | yield break; |
| | | 57 | | } |
| | | 58 | | |
| | 62 | 59 | | while (enumerator.MoveNext()) |
| | 59 | 60 | | { |
| | 59 | 61 | | if (_costFunctions.Length == 0) |
| | 59 | 62 | | { |
| | 59 | 63 | | if (enumerator.Forward) |
| | 32 | 64 | | { |
| | 32 | 65 | | yield return new Result<SnapPoint>(new SnapPoint(enumerator.EdgeId, 0)); |
| | 2 | 66 | | } |
| | | 67 | | else |
| | 27 | 68 | | { |
| | 27 | 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 | | } |
| | 59 | 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) |
| | 79 | 130 | | { |
| | 79 | 131 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | | 132 | | |
| | | 133 | | // First load region. |
| | 79 | 134 | | var box = location.BoxAround(_offsetInMeter); |
| | 79 | 135 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | | 136 | | |
| | | 137 | | // Iterate over the MaxDistance schedule (50, 200, 800, …, MaxDistance). |
| | | 138 | | // Most snaps land on the first small-D pass; sparse-area / no-snap |
| | | 139 | | // cases escalate through the schedule. PR2's tile + vertex predicates |
| | | 140 | | // make small-D passes nearly free because almost every tile in the |
| | | 141 | | // load box is excluded. |
| | 334 | 142 | | foreach (var d in ExpandSchedule(_maxDistance)) |
| | 85 | 143 | | { |
| | 85 | 144 | | var snapPoint = await _routingNetwork.SnapInBoxAsync(box, this, maxDistance: d, cancellationToken); |
| | 158 | 145 | | if (snapPoint.EdgeId != EdgeId.Empty) return snapPoint; |
| | 12 | 146 | | if (cancellationToken.IsCancellationRequested) break; |
| | 12 | 147 | | } |
| | | 148 | | |
| | | 149 | | // Retry once with a larger load region — same role as today. |
| | 6 | 150 | | if (!(_offsetInMeter < _offsetInMeterMax)) |
| | 2 | 151 | | { |
| | 2 | 152 | | return new Result<SnapPoint>( |
| | 2 | 153 | | FormattableString.Invariant($"Could not snap to location: {location.longitude},{location.latitude}")); |
| | | 154 | | } |
| | | 155 | | |
| | 4 | 156 | | box = location.BoxAround(_offsetInMeterMax); |
| | 4 | 157 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | | 158 | | |
| | 24 | 159 | | foreach (var d in ExpandSchedule(_maxDistance)) |
| | 7 | 160 | | { |
| | 7 | 161 | | var snapPoint = await _routingNetwork.SnapInBoxAsync(box, this, maxDistance: d, cancellationToken); |
| | 9 | 162 | | if (snapPoint.EdgeId != EdgeId.Empty) return snapPoint; |
| | 5 | 163 | | if (cancellationToken.IsCancellationRequested) break; |
| | 5 | 164 | | } |
| | | 165 | | |
| | 2 | 166 | | return new Result<SnapPoint>( |
| | 2 | 167 | | FormattableString.Invariant($"Could not snap to location: {location.longitude},{location.latitude}")); |
| | 79 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Iterative MaxDistance schedule: start at 50 m, ×4 per step, cap the |
| | | 172 | | /// growth at 10 km (so an unbounded MaxDistance doesn't produce an |
| | | 173 | | /// unbounded schedule), and always end with <paramref name="maxDistance"/> |
| | | 174 | | /// as the final entry. See snap-iterative-cutoff.md for the design. |
| | | 175 | | /// |
| | | 176 | | /// Examples: |
| | | 177 | | /// maxDistance = ∞ → 50, 200, 800, 3200, 12800, ∞ |
| | | 178 | | /// maxDistance = 1 000 → 50, 200, 800, 1000 |
| | | 179 | | /// maxDistance = 100 → 50, 100 |
| | | 180 | | /// maxDistance = 30 → 30 (single iteration, no growth) |
| | | 181 | | /// </summary> |
| | | 182 | | private static IEnumerable<double> ExpandSchedule(double maxDistance) |
| | 83 | 183 | | { |
| | | 184 | | const double start = 50.0; |
| | | 185 | | const double growthFactor = 4.0; |
| | | 186 | | const double growthCap = 10_000.0; |
| | | 187 | | |
| | 83 | 188 | | if (maxDistance <= start) |
| | 1 | 189 | | { |
| | 1 | 190 | | yield return maxDistance; |
| | 1 | 191 | | yield break; |
| | | 192 | | } |
| | | 193 | | |
| | 82 | 194 | | var d = start; |
| | 82 | 195 | | yield return d; |
| | 16 | 196 | | while (d < maxDistance && d < growthCap) |
| | 9 | 197 | | { |
| | 9 | 198 | | d *= growthFactor; |
| | 11 | 199 | | if (d < maxDistance) yield return d; |
| | 8 | 200 | | } |
| | 7 | 201 | | yield return maxDistance; |
| | 7 | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <inheritdoc/> |
| | | 205 | | public async IAsyncEnumerable<SnapPoint> ToAllAsync(double longitude, double latitude, [EnumeratorCancellation] Canc |
| | 2108 | 206 | | { |
| | | 207 | | // calculate one box for all locations. |
| | 2108 | 208 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | 2108 | 209 | | var box = location.BoxAround(_offsetInMeter); |
| | | 210 | | |
| | | 211 | | // make sure data is loaded. |
| | 2108 | 212 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | | 213 | | |
| | | 214 | | // snap all. |
| | 2108 | 215 | | var snapped = _routingNetwork.SnapAllInBoxAsync(box, this, maxDistance: _maxDistance, cancellationToken: cancell |
| | 69590 | 216 | | await foreach (var snapPoint in snapped) |
| | 31633 | 217 | | { |
| | 31633 | 218 | | yield return snapPoint; |
| | 31633 | 219 | | } |
| | 2108 | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <inheritdoc/> |
| | | 223 | | public async Task<Result<VertexId>> ToVertexAsync(double longitude, double latitude, CancellationToken cancellationT |
| | 1 | 224 | | { |
| | 1 | 225 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | | 226 | | |
| | | 227 | | // calculate one box for all locations. |
| | 1 | 228 | | var box = location.BoxAround(_maxDistance); |
| | | 229 | | |
| | | 230 | | // make sure data is loaded. |
| | 1 | 231 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | | 232 | | |
| | | 233 | | // snap to closest vertex. |
| | 1 | 234 | | var vertex = await _routingNetwork.SnapToVertexInBoxAsync(box, _costFunctions.Length > 0 ? this : null, maxDista |
| | 1 | 235 | | if (vertex.IsEmpty()) return new Result<VertexId>("No vertex in range found"); |
| | | 236 | | |
| | 1 | 237 | | return vertex; |
| | 1 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <inheritdoc/> |
| | | 241 | | public async IAsyncEnumerable<VertexId> ToAllVerticesAsync(double longitude, double latitude, |
| | | 242 | | [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| | 0 | 243 | | { |
| | 0 | 244 | | (double longitude, double latitude, float? e) location = (longitude, latitude, null); |
| | | 245 | | |
| | | 246 | | // calculate one box for all locations. |
| | 0 | 247 | | var box = location.BoxAround(_maxDistance); |
| | | 248 | | |
| | | 249 | | // make sure data is loaded. |
| | 0 | 250 | | await _routingNetwork.UsageNotifier.NotifyBox(_routingNetwork, box, cancellationToken); |
| | | 251 | | |
| | | 252 | | // snap to closest vertex. |
| | 0 | 253 | | await foreach (var vertex in _routingNetwork.SnapToAllVerticesInBoxAsync(box, this, |
| | 0 | 254 | | maxDistance: _maxDistance, cancellationToken: cancellationToken)) |
| | 0 | 255 | | { |
| | 0 | 256 | | yield return vertex; |
| | 0 | 257 | | } |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | private bool? IsAcceptable(IEdgeEnumerator<RoutingNetwork> edgeEnumerator) |
| | 57671 | 261 | | { |
| | 57671 | 262 | | var hasProfiles = _costFunctions.Length > 0; |
| | 57703 | 263 | | if (!hasProfiles) return true; |
| | | 264 | | |
| | 57639 | 265 | | var allOk = true; |
| | 230542 | 266 | | for (var p = 0; p < _costFunctions.Length; p++) |
| | 57639 | 267 | | { |
| | 57639 | 268 | | var costFunction = _costFunctions[p]; |
| | | 269 | | |
| | | 270 | | // check if the edge can be used in either direction. |
| | | 271 | | // both directions need to be checked here because SnapAllInBoxAsync |
| | | 272 | | // deduplicates by EdgeId: a one-way edge first encountered from its |
| | | 273 | | // head vertex would be rejected if only the tail-to-head direction is checked. |
| | 57639 | 274 | | var costs = costFunction.Get(edgeEnumerator, true, []); |
| | 57639 | 275 | | var costsReverse = costFunction.Get(edgeEnumerator, false, []); |
| | | 276 | | |
| | | 277 | | // if edge is not accessible in either direction, skip it. |
| | 57639 | 278 | | if (!costs.canAccess && !costsReverse.canAccess) |
| | 25952 | 279 | | { |
| | 25952 | 280 | | allOk = false; |
| | 25952 | 281 | | continue; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | // check if needed if the edge can be stopped on (in either direction). |
| | 31687 | 285 | | if (_checkCanStopOn) |
| | 31679 | 286 | | { |
| | 31679 | 287 | | if (!costs.canStop && !costsReverse.canStop) |
| | 0 | 288 | | { |
| | 0 | 289 | | allOk = false; |
| | 0 | 290 | | continue; |
| | | 291 | | } |
| | 31679 | 292 | | } |
| | | 293 | | |
| | | 294 | | // check if the edge is on an island. |
| | 31687 | 295 | | if (_islands.Length > 0) |
| | 18 | 296 | | { |
| | 18 | 297 | | var tailTileId = edgeEnumerator.Forward ? edgeEnumerator.Tail.TileId : edgeEnumerator.Head.TileId; |
| | 18 | 298 | | var islands = _islands[p]; |
| | | 299 | | |
| | | 300 | | // fast path: if the tile is fully done, just check _islandEdges. |
| | 18 | 301 | | if (islands.GetTileDone(tailTileId)) |
| | 11 | 302 | | { |
| | 11 | 303 | | if (islands.IsEdgeOnIsland(edgeEnumerator.EdgeId)) |
| | 4 | 304 | | { |
| | 4 | 305 | | allOk = false; |
| | 4 | 306 | | continue; |
| | | 307 | | } |
| | | 308 | | // tile done + not in island set → not island. |
| | 7 | 309 | | } |
| | | 310 | | else |
| | 7 | 311 | | { |
| | | 312 | | // tile not done — check DG for already resolved edges. |
| | 7 | 313 | | var onIsland = _routingNetwork.IslandManager.IsEdgeOnIsland(_profiles[p], edgeEnumerator.EdgeId); |
| | 7 | 314 | | if (onIsland == true) |
| | 0 | 315 | | { |
| | 0 | 316 | | allOk = false; |
| | 0 | 317 | | continue; |
| | | 318 | | } |
| | | 319 | | |
| | 7 | 320 | | if (onIsland == false) |
| | 0 | 321 | | { |
| | | 322 | | // confirmed not island. |
| | 0 | 323 | | } |
| | | 324 | | else |
| | 7 | 325 | | { |
| | | 326 | | // not yet resolved — return null to trigger async resolution. |
| | 7 | 327 | | return null; |
| | | 328 | | } |
| | 0 | 329 | | } |
| | 7 | 330 | | } |
| | | 331 | | |
| | | 332 | | // any profile is good for a positive result. |
| | 31676 | 333 | | if (_anyProfile) return true; |
| | 31676 | 334 | | } |
| | | 335 | | |
| | 57632 | 336 | | return allOk; |
| | 57671 | 337 | | } |
| | | 338 | | |
| | | 339 | | bool? IEdgeChecker.IsAcceptable(IEdgeEnumerator<RoutingNetwork> edgeEnumerator) |
| | 57671 | 340 | | { |
| | 57671 | 341 | | return this.IsAcceptable(edgeEnumerator); |
| | 57671 | 342 | | } |
| | | 343 | | |
| | | 344 | | async Task<bool> IEdgeChecker.RunCheckAsync(IEdgeEnumerator<RoutingNetwork> edgeEnumerator, CancellationToken cancel |
| | 7 | 345 | | { |
| | | 346 | | // Build the edge's tile first so every traversable edge in the tile |
| | | 347 | | // gets a definitive Island / NotIsland verdict via ClassifyAsync. |
| | | 348 | | // Subsequent snap candidates in the same tile then short-circuit on |
| | | 349 | | // the per-profile Islands set / dg fast-paths. The IslandManager |
| | | 350 | | // deduplicates concurrent builds for the same (profile, tile). |
| | 7 | 351 | | var tailTileId = edgeEnumerator.Forward ? edgeEnumerator.Tail.TileId : edgeEnumerator.Head.TileId; |
| | 33 | 352 | | foreach (var profile in _profiles) |
| | 7 | 353 | | { |
| | 7 | 354 | | await _routingNetwork.IslandManager.BuildForTileAsync(_routingNetwork, profile, tailTileId, cancellationToke |
| | 7 | 355 | | if (cancellationToken.IsCancellationRequested) return true; |
| | | 356 | | |
| | 7 | 357 | | var islands = _routingNetwork.IslandManager.GetIslandsFor(profile); |
| | 9 | 358 | | if (islands.IsEdgeOnIsland(edgeEnumerator.EdgeId)) return false; |
| | | 359 | | // tile DONE + not in Islands set → NotIsland → acceptable for this profile. |
| | 5 | 360 | | } |
| | | 361 | | |
| | 5 | 362 | | return (this as IEdgeChecker).IsAcceptable(edgeEnumerator) ?? true; |
| | 7 | 363 | | } |
| | | 364 | | } |