| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using Itinero.Network.Enumerators.Edges; |
| | 6 | | using Itinero.Network.Search; |
| | 7 | | using Itinero.Profiles; |
| | 8 | | using Itinero.Routing.Costs; |
| | 9 | | using Itinero.Routing.Costs.Caches; |
| | 10 | |
|
| | 11 | | [assembly: InternalsVisibleTo("Itinero.Geo")] |
| | 12 | |
|
| | 13 | | namespace Itinero.Network; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Extensions methods for routing networks. |
| | 17 | | /// </summary> |
| | 18 | | public static class RoutingNetworkSnapshotExtensions |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// Gets the location of the given vertex. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="routingNetwork">The network</param> |
| | 24 | | /// <param name="vertex">The vertex.</param> |
| | 25 | | /// <returns>The location.</returns> |
| | 26 | | /// <exception cref="ArgumentOutOfRangeException">When the vertex doesn't exist.</exception> |
| | 27 | | public static (double longitude, double latitude, float? e) GetVertex(this RoutingNetwork routingNetwork, |
| | 28 | | VertexId vertex) |
| 9 | 29 | | { |
| 9 | 30 | | if (!routingNetwork.TryGetVertex(vertex, out var longitude, out var latitude, out var e)) |
| 0 | 31 | | { |
| 0 | 32 | | throw new ArgumentOutOfRangeException(nameof(vertex)); |
| | 33 | | } |
| | 34 | |
|
| 9 | 35 | | return (longitude, latitude, e); |
| 9 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets an enumerable with all vertices. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="routingNetwork">The routing network.</param> |
| | 42 | | /// <param name="box">The bounding box, if any.</param> |
| | 43 | | /// <returns>An enumerable with all vertices.</returns> |
| | 44 | | public static IEnumerable<VertexId> GetVertices(this RoutingNetwork routingNetwork) |
| 8 | 45 | | { |
| 8 | 46 | | var enumerator = routingNetwork.GetVertexEnumerator(); |
| 20 | 47 | | while (enumerator.MoveNext()) |
| 12 | 48 | | { |
| 12 | 49 | | yield return enumerator.Current; |
| 12 | 50 | | } |
| 8 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets an enumerable with all edges. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="routingNetwork">The routing network.</param> |
| | 57 | | /// <param name="box">The bounding box, if any.</param> |
| | 58 | | /// <returns>An enumerable with all edges.</returns> |
| | 59 | | public static IEnumerable<EdgeId> GetEdges(this RoutingNetwork routingNetwork) |
| 1 | 60 | | { |
| 1 | 61 | | var edgeEnumerator = routingNetwork.GetEdgeEnumerator(); |
| 7 | 62 | | foreach (var vertex in routingNetwork.GetVertices()) |
| 2 | 63 | | { |
| 2 | 64 | | if (!edgeEnumerator.MoveTo(vertex)) |
| 0 | 65 | | { |
| 0 | 66 | | continue; |
| | 67 | | } |
| | 68 | |
|
| 4 | 69 | | while (edgeEnumerator.MoveNext()) |
| 2 | 70 | | { |
| 2 | 71 | | if (!edgeEnumerator.Forward) |
| 1 | 72 | | { |
| 1 | 73 | | continue; |
| | 74 | | } |
| | 75 | |
|
| 1 | 76 | | yield return edgeEnumerator.EdgeId; |
| 1 | 77 | | } |
| 2 | 78 | | } |
| 1 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Gets a cached version of the given profile. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="network">The network.</param> |
| | 85 | | /// <param name="profile">The profile.</param> |
| | 86 | | /// <returns>The cached profile.</returns> |
| | 87 | | public static ProfileCached GetCachedProfile(this RoutingNetwork network, Profile profile) |
| 21 | 88 | | { |
| 21 | 89 | | if (!network.RouterDb.ProfileConfiguration.TryGetProfileHandlerEdgeTypesCache(profile.Name, out var cache, out v |
| 21 | 90 | | { |
| 21 | 91 | | return new ProfileCached(profile, new EdgeFactorCache(), new TurnCostFactorCache()); |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | return new ProfileCached(profile, cache ?? new EdgeFactorCache(), turnCostFactorCache ?? new TurnCostFactorCache |
| 21 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Gets the cost function for the given profile. |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="network">The network.</param> |
| | 101 | | /// <param name="profile">The profile.</param> |
| | 102 | | /// <returns>The cost function.</returns> |
| | 103 | | public static ICostFunction GetCostFunctionFor(this RoutingNetwork network, Profile profile) |
| 26 | 104 | | { |
| 26 | 105 | | if (!network.RouterDb.ProfileConfiguration.TryGetProfileHandlerEdgeTypesCache(profile.Name, out var cache, out v |
| 26 | 106 | | { |
| 26 | 107 | | return new ProfileCostFunction(profile); |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | return new ProfileCostFunctionCached(profile, cache ?? new EdgeFactorCache(), turnCostFactorCache ?? new TurnCos |
| 26 | 111 | | } |
| | 112 | |
|
| | 113 | | internal static IEnumerable<(string key, string value)> |
| | 114 | | GetAttributes(this RoutingNetwork routerDb, EdgeId edge) |
| 1 | 115 | | { |
| 1 | 116 | | var enumerator = routerDb.GetEdgeEnumerator(); |
| 1 | 117 | | if (!enumerator.MoveTo(edge)) |
| 0 | 118 | | { |
| 0 | 119 | | return Enumerable.Empty<(string key, string value)>(); |
| | 120 | | } |
| | 121 | |
|
| 1 | 122 | | return enumerator.Attributes; |
| 1 | 123 | | } |
| | 124 | | } |