| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.IO; |
| | 3 | | using System.Linq; |
| | 4 | | using Itinero.Geo; |
| | 5 | | using Itinero.Geo.Directions; |
| | 6 | | using Itinero.Network; |
| | 7 | | using Itinero.Snapping; |
| | 8 | |
|
| | 9 | | namespace Itinero.Routing; |
| | 10 | |
|
| | 11 | | internal static class RouterExtensions |
| | 12 | | { |
| | 13 | | internal static (SnapPoint snapPoint, bool? direction) ToDirected( |
| | 14 | | this (SnapPoint snapPoint, DirectionEnum? angle) snapPointAndDirection, RoutingNetwork routerDb) |
| 0 | 15 | | { |
| 0 | 16 | | if (snapPointAndDirection.angle == null) |
| 0 | 17 | | { |
| 0 | 18 | | return (snapPointAndDirection.snapPoint, null); |
| | 19 | | } |
| | 20 | |
|
| 0 | 21 | | return (snapPointAndDirection.snapPoint, |
| 0 | 22 | | snapPointAndDirection.snapPoint.DirectionFromAngle(routerDb, (double)snapPointAndDirection.angle, |
| 0 | 23 | | out _)); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | internal static (SnapPoint snapPoint, bool? direction) ToDirected( |
| | 27 | | this (SnapPoint snapPoint, double angle) snapPointAndDirection, RoutingNetwork routerDb) |
| 0 | 28 | | { |
| 0 | 29 | | return (snapPointAndDirection.snapPoint, |
| 0 | 30 | | snapPointAndDirection.snapPoint.DirectionFromAngle(routerDb, snapPointAndDirection.angle, out _)); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | internal static IReadOnlyList<(SnapPoint sp, bool? directed)> ToDirected( |
| | 34 | | this IEnumerable<(SnapPoint snapPoint, DirectionEnum? directionEnum)> sps, |
| | 35 | | RoutingNetwork routerDb) |
| 0 | 36 | | { |
| 0 | 37 | | var directedSps = new List<(SnapPoint sp, bool? directed)>(); |
| 0 | 38 | | foreach (var sp in sps) |
| 0 | 39 | | { |
| 0 | 40 | | directedSps.Add((sp.snapPoint, (double)sp.directionEnum).ToDirected(routerDb)); |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return directedSps; |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | internal static IReadOnlyList<(SnapPoint sp, bool? directed)> ToDirected( |
| | 47 | | this IReadOnlyList<(SnapPoint snapPoint, double angle)> sps, |
| | 48 | | RoutingNetwork routerDb) |
| 0 | 49 | | { |
| 0 | 50 | | var directedSps = new List<(SnapPoint sp, bool? directed)>(); |
| 0 | 51 | | foreach (var sp in sps) |
| 0 | 52 | | { |
| 0 | 53 | | directedSps.Add(sp.ToDirected(routerDb)); |
| 0 | 54 | | } |
| | 55 | |
|
| 0 | 56 | | return directedSps; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | internal static IReadOnlyList<(SnapPoint sp, bool? directed)> ToDirected(this IReadOnlyList<SnapPoint> sps) |
| 0 | 60 | | { |
| 0 | 61 | | var directedSps = new List<(SnapPoint sp, bool? directed)>(); |
| 0 | 62 | | foreach (var sp in sps) |
| 0 | 63 | | { |
| 0 | 64 | | directedSps.Add((sp, null)); |
| 0 | 65 | | } |
| | 66 | |
|
| 0 | 67 | | return directedSps; |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | internal static IReadOnlyList<SnapPoint> ToUndirected( |
| | 71 | | this IReadOnlyList<(SnapPoint sp, bool? directed)> directedSps) |
| 0 | 72 | | { |
| 0 | 73 | | var sps = new List<SnapPoint>(); |
| 0 | 74 | | foreach (var (sp, direction) in directedSps) |
| 0 | 75 | | { |
| 0 | 76 | | if (direction != null) |
| 0 | 77 | | { |
| 0 | 78 | | throw new InvalidDataException($"{nameof(SnapPoint)} is directed cannot convert to undirected."); |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | sps.Add(sp); |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | return sps; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | internal static bool TryToUndirected( |
| | 88 | | this IEnumerable<(SnapPoint sp, bool? directed)> directedSps, out IReadOnlyList<SnapPoint> undirected) |
| 14 | 89 | | { |
| 14 | 90 | | var sps = new List<SnapPoint>(); |
| 70 | 91 | | foreach (var (sp, direction) in directedSps) |
| 14 | 92 | | { |
| 14 | 93 | | if (direction != null) |
| 0 | 94 | | { |
| 0 | 95 | | sps.Clear(); |
| 0 | 96 | | undirected = sps; |
| 0 | 97 | | return false; |
| | 98 | | } |
| | 99 | |
|
| 14 | 100 | | sps.Add(sp); |
| 14 | 101 | | } |
| | 102 | |
|
| 14 | 103 | | undirected = sps; |
| 14 | 104 | | return true; |
| 14 | 105 | | } |
| | 106 | |
|
| | 107 | | internal static ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, |
| | 108 | | float? e) bottomRight)? |
| | 109 | | MaxBoxFor(this RoutingSettings settings, |
| | 110 | | RoutingNetwork routerDb, IEnumerable<(SnapPoint sp, bool? direction)> sps) |
| 0 | 111 | | { |
| 0 | 112 | | return settings.MaxBoxFor(routerDb, sps.Select(x => x.sp)); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | internal static ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, |
| | 116 | | float? e) bottomRight)? MaxBoxFor(this RoutingSettings settings, |
| | 117 | | RoutingNetwork routerDb, IEnumerable<SnapPoint> sp) |
| 7 | 118 | | { |
| 7 | 119 | | ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, float? e) |
| 7 | 120 | | bottomRight)? maxBox = |
| 7 | 121 | | null; |
| | 122 | |
|
| 7 | 123 | | if (!(settings.MaxDistance < double.MaxValue)) |
| 7 | 124 | | { |
| 7 | 125 | | return null; |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | foreach (var source in sp) |
| 0 | 129 | | { |
| 0 | 130 | | var sourceLocation = source.LocationOnNetwork(routerDb); |
| 0 | 131 | | var sourceBox = sourceLocation.BoxAround(settings.MaxDistance); |
| 0 | 132 | | maxBox = maxBox?.Expand(sourceBox) ?? sourceBox; |
| 0 | 133 | | } |
| | 134 | |
|
| 0 | 135 | | return maxBox; |
| 7 | 136 | | } |
| | 137 | |
|
| | 138 | | internal static ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, |
| | 139 | | float? e) bottomRight)? MaxBoxFor(this RoutingSettings settings, |
| | 140 | | RoutingNetwork routerDb, SnapPoint sp) |
| 0 | 141 | | { |
| 0 | 142 | | ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, float? e) |
| 0 | 143 | | bottomRight)? maxBox = |
| 0 | 144 | | null; |
| | 145 | |
|
| 0 | 146 | | if (!(settings.MaxDistance < double.MaxValue)) |
| 0 | 147 | | { |
| 0 | 148 | | return null; |
| | 149 | | } |
| | 150 | |
|
| 0 | 151 | | var sourceLocation = sp.LocationOnNetwork(routerDb); |
| 0 | 152 | | var sourceBox = sourceLocation.BoxAround(settings.MaxDistance); |
| 0 | 153 | | maxBox = maxBox?.Expand(sourceBox) ?? sourceBox; |
| | 154 | |
|
| 0 | 155 | | return maxBox; |
| 0 | 156 | | } |
| | 157 | | } |