| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Itinero.Network; |
| | | 5 | | using Itinero.Routes; |
| | | 6 | | using Itinero.Routes.Builders; |
| | | 7 | | using Itinero.Routes.Paths; |
| | | 8 | | |
| | | 9 | | namespace Itinero.MapMatching; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Contains extensions |
| | | 13 | | /// </summary> |
| | | 14 | | public static class MapMatcherExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Returns a route for each segment of the track matched. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="matcher">The matcher.</param> |
| | | 20 | | /// <param name="match">The match.</param> |
| | | 21 | | /// <returns>The resulting route(s).</returns> |
| | | 22 | | public static Result<IEnumerable<Route>> Routes(this MapMatcher matcher, MapMatch match) |
| | 0 | 23 | | { |
| | 0 | 24 | | if (matcher.Settings.Profile == null) throw new Exception("Cannot build routes without a profile"); |
| | | 25 | | |
| | 0 | 26 | | var routes = new List<Route>(); |
| | 0 | 27 | | foreach (var path in match) |
| | 0 | 28 | | { |
| | 0 | 29 | | var route = RouteBuilder.Default.Build(matcher.RoutingNetwork, matcher.Settings.Profile, path); |
| | 0 | 30 | | routes.Add(route); |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | return routes; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static IEnumerable<Route> Routes(this MapMatcher matcher, IEnumerable<MapMatch> matches) |
| | 19 | 37 | | { |
| | 103 | 38 | | foreach (var match in matches) |
| | 23 | 39 | | { |
| | 117 | 40 | | foreach (var r in matcher.Route(match)) |
| | 24 | 41 | | { |
| | 24 | 42 | | yield return r; |
| | 24 | 43 | | } |
| | 23 | 44 | | } |
| | 19 | 45 | | } |
| | | 46 | | |
| | | 47 | | public static IEnumerable<Route> Route(this MapMatcher matcher, MapMatch match) |
| | 23 | 48 | | { |
| | 23 | 49 | | if (matcher.Settings.Profile == null) throw new Exception("Cannot build routes without a profile"); |
| | | 50 | | |
| | 23 | 51 | | var paths = matcher.MergedPaths(match); |
| | | 52 | | |
| | 23 | 53 | | var routes = new List<Route>(); |
| | 117 | 54 | | foreach (var path in paths) |
| | 24 | 55 | | { |
| | 24 | 56 | | var route = RouteBuilder.Default.Build(matcher.RoutingNetwork, matcher.Settings.Profile, path); |
| | 24 | 57 | | routes.Add(route); |
| | 24 | 58 | | } |
| | | 59 | | |
| | 23 | 60 | | return routes; |
| | 23 | 61 | | } |
| | | 62 | | |
| | | 63 | | public static IEnumerable<Path> MergedPaths(this MapMatcher matcher, MapMatch match) |
| | 23 | 64 | | { |
| | 23 | 65 | | if (matcher.Settings.Profile == null) throw new Exception("Cannot build routes without a profile"); |
| | | 66 | | |
| | 23 | 67 | | IEnumerable<Path> current = match; |
| | 47 | 68 | | while (true) |
| | 47 | 69 | | { |
| | 47 | 70 | | var (merged1, hasMerges1) = MapMatcherExtensions.MergePaths(current, |
| | 2020 | 71 | | (p1, p2) => p1.TryAppend(p2)); |
| | | 72 | | |
| | 47 | 73 | | var (merged2, hasMerges2) = MapMatcherExtensions.MergePaths(merged1, |
| | 83 | 74 | | (p1, p2) => p1.TryMergeAsUTurn(p2)); |
| | 47 | 75 | | current = merged2; |
| | | 76 | | |
| | 70 | 77 | | if (!hasMerges1 && !hasMerges2) break; |
| | 24 | 78 | | } |
| | | 79 | | |
| | 23 | 80 | | return current; |
| | 23 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Returns merged paths with the from/to track point indices preserved through merging. |
| | | 85 | | /// Each returned tuple contains the merged path and the original track point index range it spans. |
| | | 86 | | /// </summary> |
| | | 87 | | public static IEnumerable<(Path path, int fromTrackPointIndex, int toTrackPointIndex)> MergedPathsWithTrackPoints( |
| | | 88 | | this MapMatcher matcher, MapMatch match) |
| | 0 | 89 | | { |
| | 0 | 90 | | if (matcher.Settings.Profile == null) throw new Exception("Cannot build routes without a profile"); |
| | | 91 | | |
| | | 92 | | // Build initial list pairing each raw path with its track point index range. |
| | 0 | 93 | | var items = new List<(Path path, int from, int to)>(); |
| | 0 | 94 | | for (var i = 0; i < match.Count; i++) |
| | 0 | 95 | | { |
| | 0 | 96 | | items.Add((match[i], match.MatchedTrackPointIndices[i], match.MatchedTrackPointIndices[i + 1])); |
| | 0 | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | while (true) |
| | 0 | 100 | | { |
| | 0 | 101 | | var (merged1, hasMerges1) = MergePathsWithIndices(items, |
| | 0 | 102 | | (p1, p2) => p1.TryAppend(p2)); |
| | | 103 | | |
| | 0 | 104 | | var (merged2, hasMerges2) = MergePathsWithIndices(merged1, |
| | 0 | 105 | | (p1, p2) => p1.TryMergeAsUTurn(p2)); |
| | 0 | 106 | | items = merged2; |
| | | 107 | | |
| | 0 | 108 | | if (!hasMerges1 && !hasMerges2) break; |
| | 0 | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | return items; |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | private static (List<(Path path, int from, int to)> paths, bool merged) MergePathsWithIndices( |
| | | 115 | | List<(Path path, int from, int to)> items, Func<Path, Path, Path?> merge) |
| | 0 | 116 | | { |
| | 0 | 117 | | var result = new List<(Path path, int from, int to)>(); |
| | | 118 | | |
| | 0 | 119 | | (Path path, int from, int to)? current = null; |
| | 0 | 120 | | var hasMerges = false; |
| | 0 | 121 | | foreach (var item in items) |
| | 0 | 122 | | { |
| | 0 | 123 | | item.path.Trim(); |
| | 0 | 124 | | if (!item.path.HasLength()) continue; |
| | | 125 | | |
| | 0 | 126 | | if (current == null) |
| | 0 | 127 | | { |
| | 0 | 128 | | current = item; |
| | 0 | 129 | | continue; |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | var merged = merge(current.Value.path, item.path); |
| | 0 | 133 | | if (merged == null) |
| | 0 | 134 | | { |
| | 0 | 135 | | result.Add(current.Value); |
| | 0 | 136 | | current = item; |
| | 0 | 137 | | continue; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | hasMerges = true; |
| | 0 | 141 | | current = (merged, current.Value.from, item.to); |
| | 0 | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | if (current != null) |
| | 0 | 145 | | { |
| | 0 | 146 | | result.Add(current.Value); |
| | 0 | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | return (result, hasMerges); |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | private static (IEnumerable<Path> paths, bool merged) MergePaths(IEnumerable<Path> paths, Func<Path, Path, Path?> me |
| | 94 | 153 | | { |
| | 94 | 154 | | var mergedPaths = new List<Path>(); |
| | | 155 | | |
| | 94 | 156 | | Path? currentPath = null; |
| | 94 | 157 | | var hasMerges = false; |
| | 4570 | 158 | | foreach (var path in paths) |
| | 2144 | 159 | | { |
| | 2144 | 160 | | path.Trim(); |
| | 2185 | 161 | | if (!path.HasLength()) continue; |
| | | 162 | | |
| | 2103 | 163 | | if (currentPath == null) |
| | 94 | 164 | | { |
| | 94 | 165 | | currentPath = path; |
| | 94 | 166 | | continue; |
| | | 167 | | } |
| | | 168 | | |
| | 2009 | 169 | | var merged = merge(currentPath, path); |
| | 2009 | 170 | | if (merged == null) |
| | 50 | 171 | | { |
| | 50 | 172 | | mergedPaths.Add(currentPath); |
| | 50 | 173 | | currentPath = path; |
| | 50 | 174 | | continue; |
| | | 175 | | } |
| | | 176 | | |
| | 1959 | 177 | | hasMerges = true; |
| | 1959 | 178 | | currentPath = merged; |
| | 1959 | 179 | | } |
| | | 180 | | |
| | 94 | 181 | | if (currentPath != null) |
| | 94 | 182 | | { |
| | 94 | 183 | | mergedPaths.Add(currentPath); |
| | 94 | 184 | | } |
| | | 185 | | |
| | 94 | 186 | | return (mergedPaths, hasMerges); |
| | 94 | 187 | | } |
| | | 188 | | |
| | | 189 | | private static Path? TryAppend(this Path path, Path next) |
| | 1973 | 190 | | { |
| | 2009 | 191 | | if (!path.IsNext(next)) return null; |
| | | 192 | | |
| | 1937 | 193 | | return new[] { path, next }.Merge(); |
| | 1973 | 194 | | } |
| | | 195 | | } |