| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using Itinero.Geo; |
| | 6 | | using Itinero.Routes; |
| | 7 | |
|
| | 8 | | [assembly: InternalsVisibleTo("Itinero.Tests.Instructions")] |
| | 9 | |
|
| | 10 | | namespace Itinero.Instructions; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Helper class to allow easy access on some parts of the route |
| | 14 | | /// </summary> |
| | 15 | | public class IndexedRoute |
| | 16 | | { |
| | 17 | | private readonly Route _route; |
| | 18 | |
|
| 37 | 19 | | public IndexedRoute(Route route) |
| 37 | 20 | | { |
| 37 | 21 | | _route = route; |
| 37 | 22 | | this.Meta = BuildMetaList(route); |
| 37 | 23 | | this.Branches = BuildBranchesList(route); |
| 37 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Gets branches . |
| | 28 | | /// </summary> |
| 66 | 29 | | public List<List<Route.Branch>> Branches { get; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// A one-on-one list, where every meta is matched with every shape. |
| | 33 | | /// (thus: Meta[i] will correspond with the meta for the segment starting at Shape[i] |
| | 34 | | /// (Note that Shape[Shape.Count - 1] is thus _not_ defined |
| | 35 | | /// </summary> |
| 82 | 36 | | public List<Route.Meta> Meta { get; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The shape. |
| | 40 | | /// </summary> |
| 1038 | 41 | | public List<(double longitude, double latitude, float? e)> Shape => _route.Shape; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The index of the last shape point. |
| | 45 | | /// </summary> |
| 359 | 46 | | public int Last => this.Shape.Count - 1; |
| | 47 | |
|
| | 48 | | private static List<Route.Meta> BuildMetaList(Route route) |
| 37 | 49 | | { |
| 37 | 50 | | var metas = new List<Route.Meta>(); |
| 37 | 51 | | if (route.ShapeMeta == null || route.ShapeMeta.Count == 0) |
| 0 | 52 | | { |
| 0 | 53 | | throw new ArgumentException("Cannot generate route instructions if meta information is missing"); |
| | 54 | | } |
| | 55 | |
|
| 255 | 56 | | foreach (var meta in route.ShapeMeta) |
| 72 | 57 | | { |
| | 58 | | // Meta.shape indicates the last element in the list |
| 289 | 59 | | while (metas.Count < meta.Shape) |
| 217 | 60 | | { |
| 217 | 61 | | metas.Add(meta); |
| 217 | 62 | | } |
| 72 | 63 | | } |
| | 64 | | #if DEBUG |
| 37 | 65 | | if (metas.Count + 1 != route.Shape.Count) |
| 0 | 66 | | { |
| 0 | 67 | | throw new Exception("Length of the meta doesn't match. There are " + route.Shape.Count + |
| 0 | 68 | | " shapes, but the last meta has an index of " + |
| 0 | 69 | | route.ShapeMeta[^1].Shape + ", resulting in a meta list of " + metas.Count); |
| | 70 | | } |
| | 71 | | #endif |
| | 72 | |
|
| 37 | 73 | | return metas; |
| 37 | 74 | | } |
| | 75 | |
|
| | 76 | | private static List<List<Route.Branch>> BuildBranchesList(Route route) |
| 37 | 77 | | { |
| 37 | 78 | | var branches = new List<List<Route.Branch>>(); |
| 37 | 79 | | if (route.Branches == null) |
| 0 | 80 | | { |
| 0 | 81 | | return branches; |
| | 82 | | } |
| | 83 | |
|
| 619 | 84 | | foreach (var _ in route.Shape) |
| 254 | 85 | | { |
| 254 | 86 | | branches.Add(new List<Route.Branch>()); |
| 254 | 87 | | } |
| | 88 | |
|
| 137 | 89 | | foreach (var branch in route.Branches) |
| 13 | 90 | | { |
| 13 | 91 | | branches[branch.Shape].Add(branch); |
| 13 | 92 | | } |
| | 93 | |
|
| 37 | 94 | | return branches; |
| 37 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Returns the distance to the next point. |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="shape">The shape point.</param> |
| | 101 | | /// <returns>The distance to the next shape point.</returns> |
| | 102 | | public double DistanceToNextPoint(int shape) |
| 131 | 103 | | { |
| 131 | 104 | | var prevPoint = |
| 131 | 105 | | shape == -1 ? _route.Stops[0].Coordinate : this.Shape[shape]; |
| | 106 | |
|
| 131 | 107 | | (double, double, float? e) nextPoint = this.Last == shape ? |
| 131 | 108 | | _route.Stops[^1].Coordinate : this.Shape[shape + 1]; |
| | 109 | |
|
| 131 | 110 | | return prevPoint.DistanceEstimateInMeter(nextPoint); |
| 131 | 111 | | } |
| | 112 | |
|
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Calculates the total distance in meter between shape-index 'start' and shapeindex 'end' |
| | 116 | | /// </summary> |
| | 117 | | /// <param name="shapeStart"></param> |
| | 118 | | /// <param name="shapeEnd"></param> |
| | 119 | | /// <returns></returns> |
| | 120 | | public double DistanceBetween(int shapeStart, int shapeEnd) |
| 23 | 121 | | { |
| 23 | 122 | | var total = 0.0; |
| 210 | 123 | | for (int i = shapeStart; i < shapeEnd; i++) |
| 82 | 124 | | { |
| 82 | 125 | | total += this.DistanceToNextPoint(i); |
| 82 | 126 | | } |
| | 127 | |
|
| 23 | 128 | | return total; |
| 23 | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// The departing angle with the meridian. |
| | 133 | | /// </summary> |
| | 134 | | /// <param name="shape">The shape point index.</param> |
| | 135 | | /// <returns>The angle.</returns> |
| | 136 | | public double DepartingDirectionAt(int shape) |
| 91 | 137 | | { |
| 91 | 138 | | return this.Shape[shape] |
| 91 | 139 | | .AngleWithMeridian(this.Last == shape ? _route.Stops[^1].Coordinate : this.Shape[shape + 1]); |
| 91 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// The absolute angle when arriving |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="shape"></param> |
| | 146 | | /// <returns>The angle.</returns> |
| | 147 | | public double ArrivingDirectionAt(int shape) |
| 101 | 148 | | { |
| 101 | 149 | | var prevPoint = |
| 101 | 150 | | shape == 0 ? _route.Stops[0].Coordinate : this.Shape[shape - 1]; |
| | 151 | |
|
| 101 | 152 | | return prevPoint.AngleWithMeridian(this.Shape[shape]); |
| 101 | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// The direction change at a given shape index. |
| | 157 | | /// Going straight on at this shape will result in 0° here. |
| | 158 | | /// Making a perfectly right turn, results in -90° |
| | 159 | | /// Making a perfectly left turn results in +90° |
| | 160 | | /// Value will always be between +180 and -180 |
| | 161 | | /// </summary> |
| | 162 | | /// <param name="shape"></param> |
| | 163 | | /// <returns></returns> |
| | 164 | | public int DirectionChangeAt(int shape) |
| 84 | 165 | | { |
| 84 | 166 | | return (this.ArrivingDirectionAt(shape) - this.DepartingDirectionAt(shape)).NormalizeDegrees(); |
| 84 | 167 | | } |
| | 168 | | // |
| | 169 | | // // ReSharper disable once UnusedMember.Global |
| | 170 | | // public string GeojsonPoints() |
| | 171 | | // { |
| | 172 | | // var parts = this.Shape.Select( |
| | 173 | | // (s, i) => |
| | 174 | | // "{ \"type\": \"Feature\", \"properties\": { \"marker-color\": \"#7e7e7e\", \"marker-size\": \"medium\ |
| | 175 | | // i + "\" }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [ " + s.longitude + "," + |
| | 176 | | // s.latitude + " ] }}"); |
| | 177 | | // return string.Join(",\n", parts); |
| | 178 | | // } |
| | 179 | | // |
| | 180 | | // public string GeojsonLines() |
| | 181 | | // { |
| | 182 | | // var parts = new List<string>(); |
| | 183 | | // for (var i = 0; i < this.Shape.Count - 1; i++) { |
| | 184 | | // var meta = Meta[i].Attributes.Select(attr => $"\"{attr.key}\": \"{attr.value}\"").ToList(); |
| | 185 | | // meta.Add($"\"index\": {i}"); |
| | 186 | | // |
| | 187 | | // var coordinates = |
| | 188 | | // $"[ [{Shape[i].longitude}, {Shape[i].latitude}], [{Shape[i + 1].longitude}, {Shape[i + 1].latitude}] |
| | 189 | | // var part = |
| | 190 | | // "\n{\"type\":\"Feature\"," + |
| | 191 | | // "\n \"properties\":{" + string.Join(",", meta) + "}," + |
| | 192 | | // $"\n \"geometry\":{{\"type\":\"LineString\",\"coordinates\": {coordinates} }}" + |
| | 193 | | // "\n}"; |
| | 194 | | // |
| | 195 | | // parts.Add(part); |
| | 196 | | // } |
| | 197 | | // |
| | 198 | | // return InGeoJson(string.Join(",\n", parts)); |
| | 199 | | // } |
| | 200 | | // |
| | 201 | | // private static string InGeoJson(string contents) |
| | 202 | | // { |
| | 203 | | // return "{\"type\": \"FeatureCollection\",\"features\": [\n\n" + contents + "\n\n]}"; |
| | 204 | | // } |
| | 205 | | } |