| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Text.Json; |
| | | 7 | | using Itinero.Network; |
| | | 8 | | using Itinero.Network.Attributes; |
| | | 9 | | using Itinero.Network.Enumerators.Edges; |
| | | 10 | | using Itinero.Network.Search; |
| | | 11 | | using Itinero.Network.Search.Edges; |
| | | 12 | | using Itinero.Network.TurnCosts; |
| | | 13 | | using Itinero.Profiles; |
| | | 14 | | |
| | | 15 | | namespace Itinero.IO.Json.GeoJson; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Contains router db geojson extensions. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class RouterDbExtensions |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets a geojson representation of the data in the router db. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="routerDb">The router db.</param> |
| | | 26 | | /// <param name="box">The bbox of the part of the network to extract.</param> |
| | | 27 | | /// <param name="profiles">The profiles, if output related to profiles is needed.</param> |
| | | 28 | | /// <returns>A string with geojson.</returns> |
| | | 29 | | public static string ToGeoJson(this RoutingNetwork routerDb, |
| | | 30 | | ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, float? e) |
| | | 31 | | bottomRight)? box = null, IEnumerable<Profile>? profiles = null) |
| | 0 | 32 | | { |
| | 0 | 33 | | profiles ??= ArraySegment<Profile>.Empty; |
| | | 34 | | |
| | 0 | 35 | | using var stream = new MemoryStream(); |
| | 0 | 36 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| | 0 | 37 | | { |
| | 0 | 38 | | jsonWriter.WriteFeatureCollectionStart(); |
| | 0 | 39 | | jsonWriter.WriteFeatures(routerDb, box); |
| | 0 | 40 | | jsonWriter.WriteFeatureCollectionEnd(); |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets a geojson representation of given edge in the routing network. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="routerDb">The routing network.</param> |
| | | 50 | | /// <param name="edgeId">The edge id.</param> |
| | | 51 | | /// <returns>A string with geojson.</returns> |
| | | 52 | | public static string ToGeoJson(this RoutingNetwork routerDb, EdgeId edgeId) |
| | 0 | 53 | | { |
| | 0 | 54 | | var edgeEnumerator = routerDb.GetEdgeEnumerator(); |
| | 0 | 55 | | if (!edgeEnumerator.MoveTo(edgeId)) throw new Exception("Edge not found"); |
| | | 56 | | |
| | 0 | 57 | | using var stream = new MemoryStream(); |
| | 0 | 58 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| | 0 | 59 | | { |
| | 0 | 60 | | jsonWriter.WriteFeatureCollectionStart(); |
| | 0 | 61 | | jsonWriter.WriteEdgeFeature(edgeEnumerator); |
| | 0 | 62 | | jsonWriter.WriteFeatureCollectionEnd(); |
| | 0 | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Writes features to the given json writer. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 72 | | /// <param name="routingNetwork">The routing network.</param> |
| | | 73 | | /// <param name="box">The bounding box.</param> |
| | | 74 | | public static void WriteFeatures(this Utf8JsonWriter jsonWriter, RoutingNetwork routingNetwork, |
| | | 75 | | ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, float? e) |
| | | 76 | | bottomRight)? box) |
| | 0 | 77 | | { |
| | 0 | 78 | | var vertices = new HashSet<VertexId>(); |
| | 0 | 79 | | var edges = new HashSet<EdgeId>(); |
| | | 80 | | |
| | 0 | 81 | | if (box == null) |
| | 0 | 82 | | { |
| | 0 | 83 | | var vertexEnumerator = routingNetwork.GetVertexEnumerator(); |
| | 0 | 84 | | var edgeEnumerator = routingNetwork.GetEdgeEnumerator(); |
| | 0 | 85 | | while (vertexEnumerator.MoveNext()) |
| | 0 | 86 | | { |
| | 0 | 87 | | edgeEnumerator.MoveTo(vertexEnumerator.Current); |
| | | 88 | | |
| | 0 | 89 | | while (edgeEnumerator.MoveNext()) |
| | 0 | 90 | | { |
| | 0 | 91 | | var tail = edgeEnumerator.Tail; |
| | 0 | 92 | | if (!vertices.Contains(tail)) |
| | 0 | 93 | | { |
| | 0 | 94 | | jsonWriter.WriteVertexFeature(tail, routingNetwork); |
| | 0 | 95 | | vertices.Add(tail); |
| | 0 | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | var head = edgeEnumerator.Head; |
| | 0 | 99 | | if (!vertices.Contains(head)) |
| | 0 | 100 | | { |
| | 0 | 101 | | jsonWriter.WriteVertexFeature(head, routingNetwork); |
| | 0 | 102 | | vertices.Add(head); |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | var edge = edgeEnumerator.EdgeId; |
| | 0 | 106 | | if (!edges.Contains(edge)) |
| | 0 | 107 | | { |
| | 0 | 108 | | jsonWriter.WriteEdgeFeature(routingNetwork, edgeEnumerator); |
| | 0 | 109 | | edges.Add(edge); |
| | 0 | 110 | | } |
| | 0 | 111 | | } |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | else |
| | 0 | 115 | | { |
| | 0 | 116 | | var edgeEnumerator = routingNetwork.SearchEdgesInBox(box.Value); |
| | 0 | 117 | | while (edgeEnumerator.MoveNext()) |
| | 0 | 118 | | { |
| | 0 | 119 | | var tail = edgeEnumerator.Tail; |
| | 0 | 120 | | if (!vertices.Contains(tail)) |
| | 0 | 121 | | { |
| | 0 | 122 | | jsonWriter.WriteVertexFeature(tail, routingNetwork); |
| | 0 | 123 | | vertices.Add(tail); |
| | 0 | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | var head = edgeEnumerator.Head; |
| | 0 | 127 | | if (!vertices.Contains(head)) |
| | 0 | 128 | | { |
| | 0 | 129 | | jsonWriter.WriteVertexFeature(head, routingNetwork); |
| | 0 | 130 | | vertices.Add(head); |
| | 0 | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | var edge = edgeEnumerator.EdgeId; |
| | 0 | 134 | | if (!edges.Contains(edge)) |
| | 0 | 135 | | { |
| | 0 | 136 | | jsonWriter.WriteEdgeFeature(routingNetwork, edgeEnumerator); |
| | 0 | 137 | | edges.Add(edge); |
| | 0 | 138 | | } |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | // write turn cost features. |
| | 0 | 143 | | jsonWriter.WriteTurnCostFeatures(routingNetwork, edges); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Writes a vertex as a feature. |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 150 | | /// <param name="routerDb">The router db.</param> |
| | | 151 | | /// <param name="vertexId">The vertex id.</param> |
| | | 152 | | public static void WriteVertexFeature(this Utf8JsonWriter jsonWriter, VertexId vertexId, |
| | | 153 | | RoutingNetwork routerDb) |
| | 0 | 154 | | { |
| | 0 | 155 | | jsonWriter.WriteVertexFeature(vertexId, routerDb.GetVertex(vertexId)); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Writes a vertex as a feature. |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 162 | | /// <param name="vertexId">The vertex id.</param> |
| | | 163 | | /// <param name="location">The location.</param> |
| | | 164 | | public static void WriteVertexFeature(this Utf8JsonWriter jsonWriter, VertexId vertexId, |
| | | 165 | | (double longitude, double latitude, float? e) location) |
| | 0 | 166 | | { |
| | 0 | 167 | | jsonWriter.WriteFeatureStart(); |
| | 0 | 168 | | jsonWriter.WriteProperties(new (string key, string value)[] |
| | 0 | 169 | | { |
| | 0 | 170 | | ("_tile_id", vertexId.TileId.ToString()), ("_local_id", vertexId.LocalId.ToString()) |
| | 0 | 171 | | }); |
| | 0 | 172 | | jsonWriter.WritePropertyName("geometry"); |
| | 0 | 173 | | jsonWriter.WritePoint(location); |
| | 0 | 174 | | jsonWriter.WriteFeatureEnd(); |
| | 0 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Writes an edge as a feature. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 181 | | /// <param name="routingNetwork">The routing network db.</param> |
| | | 182 | | /// <param name="enumerator">The enumerator.</param> |
| | | 183 | | public static void WriteEdgeFeature(this Utf8JsonWriter jsonWriter, |
| | | 184 | | RoutingNetwork routingNetwork, IEdgeEnumerator enumerator) |
| | 0 | 185 | | { |
| | 0 | 186 | | jsonWriter.WriteFeatureStart(); |
| | 0 | 187 | | var attributes = enumerator.Attributes.ToList(); |
| | 0 | 188 | | if (enumerator.Forward) |
| | 0 | 189 | | { |
| | 0 | 190 | | attributes.AddRange(new (string key, string value)[] |
| | 0 | 191 | | { |
| | 0 | 192 | | ("_tail_tile_id", enumerator.Tail.TileId.ToString()), |
| | 0 | 193 | | ("_tail_local_id", enumerator.Tail.LocalId.ToString()), |
| | 0 | 194 | | ("_head_tile_id", enumerator.Head.TileId.ToString()), |
| | 0 | 195 | | ("_head_local_id", enumerator.Head.LocalId.ToString()), |
| | 0 | 196 | | ("_edge_id", enumerator.EdgeId.ToString()) |
| | 0 | 197 | | }); |
| | 0 | 198 | | } |
| | | 199 | | else |
| | 0 | 200 | | { |
| | 0 | 201 | | attributes.AddRange(new (string key, string value)[] |
| | 0 | 202 | | { |
| | 0 | 203 | | ("_head_tile_id", enumerator.Tail.TileId.ToString()), |
| | 0 | 204 | | ("_head_local_id", enumerator.Tail.LocalId.ToString()), |
| | 0 | 205 | | ("_tail_tile_id", enumerator.Head.TileId.ToString()), |
| | 0 | 206 | | ("_tail_local_id", enumerator.Head.LocalId.ToString()), |
| | 0 | 207 | | ("_edge_id", enumerator.EdgeId.ToString()) |
| | 0 | 208 | | }); |
| | 0 | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if (enumerator.TailOrder.HasValue) attributes.AddOrReplace("_tail_order", enumerator.TailOrder.Value.ToString()) |
| | 0 | 212 | | if (enumerator.HeadOrder.HasValue) attributes.AddOrReplace("_head_order", enumerator.HeadOrder.Value.ToString()) |
| | | 213 | | |
| | 0 | 214 | | foreach (var profileName in routingNetwork.RouterDb.ProfileConfiguration.GetProfileNames()) |
| | 0 | 215 | | { |
| | 0 | 216 | | if (!routingNetwork.RouterDb.ProfileConfiguration.TryGetProfileHandlerEdgeTypesCache(profileName, out var ed |
| | 0 | 217 | | out _)) continue; |
| | | 218 | | |
| | 0 | 219 | | if (edgeFactorCache == null) continue; |
| | 0 | 220 | | if (!enumerator.EdgeTypeId.HasValue) continue; |
| | | 221 | | |
| | 0 | 222 | | var factor = edgeFactorCache.Get(enumerator.EdgeTypeId.Value); |
| | 0 | 223 | | if (factor == null) continue; |
| | | 224 | | |
| | 0 | 225 | | attributes.AddOrReplace($"_{profileName}_factor_forward", |
| | 0 | 226 | | factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| | 0 | 227 | | attributes.AddOrReplace($"_{profileName}_factor_backward", |
| | 0 | 228 | | factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| | 0 | 229 | | attributes.AddOrReplace($"_{profileName}_speed_forward", |
| | 0 | 230 | | factor.Value.ForwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| | 0 | 231 | | attributes.AddOrReplace($"_{profileName}_speed_backward", |
| | 0 | 232 | | factor.Value.BackwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| | | 233 | | |
| | 0 | 234 | | if (!routingNetwork.IslandManager.TryGetIslandsFor(profileName, out var islands)) continue; |
| | | 235 | | |
| | 0 | 236 | | if (factor.Value.ForwardFactor > 0 || factor.Value.BackwardFactor > 0) |
| | 0 | 237 | | { |
| | 0 | 238 | | if (islands.GetTileDone(enumerator.Tail.TileId)) |
| | 0 | 239 | | { |
| | 0 | 240 | | attributes.AddOrReplace($"_{profileName}_island", |
| | 0 | 241 | | islands.IsEdgeOnIsland(enumerator.EdgeId).ToString().ToLowerInvariant()); |
| | 0 | 242 | | } |
| | | 243 | | else |
| | 0 | 244 | | { |
| | 0 | 245 | | var status = routingNetwork.IslandManager.IsEdgeOnIsland(profileName, enumerator.EdgeId); |
| | 0 | 246 | | attributes.AddOrReplace($"_{profileName}_island", |
| | 0 | 247 | | status switch |
| | 0 | 248 | | { |
| | 0 | 249 | | true => "true", |
| | 0 | 250 | | false => "false", |
| | 0 | 251 | | null => "unknown" |
| | 0 | 252 | | }); |
| | 0 | 253 | | } |
| | 0 | 254 | | } |
| | 0 | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | jsonWriter.WriteProperties(attributes); |
| | 0 | 258 | | jsonWriter.WritePropertyName("geometry"); |
| | 0 | 259 | | jsonWriter.WriteLineString(enumerator.GetCompleteShape()); |
| | 0 | 260 | | jsonWriter.WriteFeatureEnd(); |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Writes an edge as a feature. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 267 | | /// <param name="enumerator">The enumerator.</param> |
| | | 268 | | public static void WriteEdgeFeature(this Utf8JsonWriter jsonWriter, |
| | | 269 | | IEdgeEnumerator enumerator) |
| | 0 | 270 | | { |
| | 0 | 271 | | jsonWriter.WriteFeatureStart(); |
| | 0 | 272 | | var attributes = enumerator.Attributes.ToList(); |
| | 0 | 273 | | attributes.AddRange(new (string key, string value)[] |
| | 0 | 274 | | { |
| | 0 | 275 | | ("tail_tile_id", enumerator.Tail.TileId.ToString()), |
| | 0 | 276 | | ("tail_local_id", enumerator.Tail.LocalId.ToString()), |
| | 0 | 277 | | ("head_tile_id", enumerator.Head.TileId.ToString()), |
| | 0 | 278 | | ("head_local_id", enumerator.Head.LocalId.ToString()), ("edge_id", enumerator.EdgeId.ToString()) |
| | 0 | 279 | | }); |
| | 0 | 280 | | if (enumerator.TailOrder.HasValue) attributes.AddOrReplace("tail_order", enumerator.TailOrder.Value.ToString()); |
| | 0 | 281 | | if (enumerator.HeadOrder.HasValue) attributes.AddOrReplace("head_order", enumerator.HeadOrder.Value.ToString()); |
| | 0 | 282 | | jsonWriter.WriteProperties(attributes); |
| | 0 | 283 | | jsonWriter.WritePropertyName("geometry"); |
| | 0 | 284 | | jsonWriter.WriteLineString(enumerator.GetCompleteShape()); |
| | 0 | 285 | | jsonWriter.WriteFeatureEnd(); |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Writes turn cost features for all edges in the set. |
| | | 290 | | /// </summary> |
| | | 291 | | public static void WriteTurnCostFeatures(this Utf8JsonWriter jsonWriter, RoutingNetwork routingNetwork, |
| | | 292 | | HashSet<EdgeId> edges) |
| | 0 | 293 | | { |
| | 0 | 294 | | var edgeEnumerator = routingNetwork.GetEdgeEnumerator(); |
| | 0 | 295 | | var secondEdgeEnumerator = routingNetwork.GetEdgeEnumerator(); |
| | | 296 | | |
| | 0 | 297 | | foreach (var edgeId in edges) |
| | 0 | 298 | | { |
| | 0 | 299 | | if (!edgeEnumerator.MoveTo(edgeId)) continue; |
| | | 300 | | |
| | | 301 | | // turn costs from head (edge traversed tail→head, then turning). |
| | 0 | 302 | | if (edgeEnumerator.HeadOrder != null) |
| | 0 | 303 | | { |
| | 0 | 304 | | secondEdgeEnumerator.MoveTo(edgeEnumerator.Head); |
| | 0 | 305 | | while (secondEdgeEnumerator.MoveNext()) |
| | 0 | 306 | | { |
| | 0 | 307 | | if (secondEdgeEnumerator.EdgeId == edgeEnumerator.EdgeId) continue; |
| | 0 | 308 | | if (secondEdgeEnumerator.TailOrder == null) continue; |
| | | 309 | | |
| | 0 | 310 | | foreach (var turnCost in |
| | 0 | 311 | | edgeEnumerator.GetTurnCostFromHead(secondEdgeEnumerator.TailOrder.Value)) |
| | 0 | 312 | | { |
| | 0 | 313 | | if (turnCost.cost == 0) continue; |
| | | 314 | | |
| | 0 | 315 | | var shape = edgeEnumerator.GetCompleteShape() |
| | 0 | 316 | | .Concat(secondEdgeEnumerator.GetCompleteShape()); |
| | 0 | 317 | | jsonWriter.WriteTurnCostFeature(edgeEnumerator.EdgeId, secondEdgeEnumerator.EdgeId, |
| | 0 | 318 | | turnCost, OffsetRight(shape, 5.0)); |
| | 0 | 319 | | } |
| | 0 | 320 | | } |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | // turn costs from tail (edge traversed head→tail, then turning). |
| | 0 | 324 | | if (edgeEnumerator.TailOrder != null) |
| | 0 | 325 | | { |
| | 0 | 326 | | secondEdgeEnumerator.MoveTo(edgeEnumerator.Tail); |
| | 0 | 327 | | while (secondEdgeEnumerator.MoveNext()) |
| | 0 | 328 | | { |
| | 0 | 329 | | if (secondEdgeEnumerator.EdgeId == edgeEnumerator.EdgeId) continue; |
| | 0 | 330 | | if (secondEdgeEnumerator.TailOrder == null) continue; |
| | | 331 | | |
| | 0 | 332 | | foreach (var turnCost in |
| | 0 | 333 | | edgeEnumerator.GetTurnCostFromTail(secondEdgeEnumerator.TailOrder.Value)) |
| | 0 | 334 | | { |
| | 0 | 335 | | if (turnCost.cost == 0) continue; |
| | | 336 | | |
| | 0 | 337 | | var shape = edgeEnumerator.GetCompleteShape().Reverse() |
| | 0 | 338 | | .Concat(secondEdgeEnumerator.GetCompleteShape()); |
| | 0 | 339 | | jsonWriter.WriteTurnCostFeature(edgeEnumerator.EdgeId, secondEdgeEnumerator.EdgeId, |
| | 0 | 340 | | turnCost, OffsetRight(shape, 5.0)); |
| | 0 | 341 | | } |
| | 0 | 342 | | } |
| | 0 | 343 | | } |
| | 0 | 344 | | } |
| | 0 | 345 | | } |
| | | 346 | | |
| | | 347 | | private static void WriteTurnCostFeature(this Utf8JsonWriter jsonWriter, |
| | | 348 | | EdgeId fromEdge, EdgeId toEdge, |
| | | 349 | | (uint turnCostType, IEnumerable<(string key, string value)> attributes, uint cost, |
| | | 350 | | IEnumerable<EdgeId> prefixEdges) turnCost, |
| | | 351 | | IEnumerable<(double longitude, double latitude, float? e)> shape) |
| | 0 | 352 | | { |
| | 0 | 353 | | jsonWriter.WriteFeatureStart(); |
| | 0 | 354 | | var attributes = turnCost.attributes.ToList(); |
| | 0 | 355 | | attributes.AddRange(new (string key, string value)[] |
| | 0 | 356 | | { |
| | 0 | 357 | | ("_type", "turn_cost"), |
| | 0 | 358 | | ("_from_edge", fromEdge.ToString()), |
| | 0 | 359 | | ("_to_edge", toEdge.ToString()), |
| | 0 | 360 | | ("_prefix", string.Join(",", turnCost.prefixEdges.Select(x => x.ToString()))), |
| | 0 | 361 | | ("_cost", turnCost.cost.ToString()), |
| | 0 | 362 | | ("_turn_cost_type", turnCost.turnCostType.ToString()) |
| | 0 | 363 | | }); |
| | 0 | 364 | | jsonWriter.WriteProperties(attributes); |
| | 0 | 365 | | jsonWriter.WritePropertyName("geometry"); |
| | 0 | 366 | | jsonWriter.WriteLineString(shape); |
| | 0 | 367 | | jsonWriter.WriteFeatureEnd(); |
| | 0 | 368 | | } |
| | | 369 | | |
| | | 370 | | private static IEnumerable<(double longitude, double latitude, float? e)> OffsetRight( |
| | | 371 | | IEnumerable<(double longitude, double latitude, float? e)> coordinates, double offsetMeters) |
| | 0 | 372 | | { |
| | 0 | 373 | | var coords = coordinates.ToList(); |
| | 0 | 374 | | if (coords.Count < 2) |
| | 0 | 375 | | { |
| | 0 | 376 | | foreach (var c in coords) yield return c; |
| | 0 | 377 | | yield break; |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | for (var i = 0; i < coords.Count; i++) |
| | 0 | 381 | | { |
| | 0 | 382 | | var (lon, lat, e) = coords[i]; |
| | | 383 | | |
| | | 384 | | // get direction from adjacent points. |
| | | 385 | | double dx, dy; |
| | 0 | 386 | | if (i == 0) |
| | 0 | 387 | | { |
| | 0 | 388 | | dx = coords[i + 1].longitude - lon; |
| | 0 | 389 | | dy = coords[i + 1].latitude - lat; |
| | 0 | 390 | | } |
| | 0 | 391 | | else if (i == coords.Count - 1) |
| | 0 | 392 | | { |
| | 0 | 393 | | dx = lon - coords[i - 1].longitude; |
| | 0 | 394 | | dy = lat - coords[i - 1].latitude; |
| | 0 | 395 | | } |
| | | 396 | | else |
| | 0 | 397 | | { |
| | 0 | 398 | | dx = coords[i + 1].longitude - coords[i - 1].longitude; |
| | 0 | 399 | | dy = coords[i + 1].latitude - coords[i - 1].latitude; |
| | 0 | 400 | | } |
| | | 401 | | |
| | | 402 | | // convert to meters for proper normalization. |
| | 0 | 403 | | var latRad = lat * Math.PI / 180.0; |
| | 0 | 404 | | var cosLat = Math.Cos(latRad); |
| | 0 | 405 | | var dxM = dx * 111320.0 * cosLat; |
| | 0 | 406 | | var dyM = dy * 111320.0; |
| | | 407 | | |
| | 0 | 408 | | var lenM = Math.Sqrt(dxM * dxM + dyM * dyM); |
| | 0 | 409 | | if (lenM < 0.001) |
| | 0 | 410 | | { |
| | 0 | 411 | | yield return (lon, lat, e); |
| | 0 | 412 | | continue; |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | // right perpendicular in meters (rotate 90° clockwise). |
| | 0 | 416 | | var perpXM = dyM / lenM * offsetMeters; |
| | 0 | 417 | | var perpYM = -dxM / lenM * offsetMeters; |
| | | 418 | | |
| | | 419 | | // convert back to degrees. |
| | 0 | 420 | | yield return (lon + perpXM / (111320.0 * cosLat), lat + perpYM / 111320.0, e); |
| | 0 | 421 | | } |
| | 0 | 422 | | } |
| | | 423 | | } |