| | 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.Profiles; |
| | 13 | |
|
| | 14 | | namespace Itinero.IO.Json.GeoJson; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Contains router db geojson extensions. |
| | 18 | | /// </summary> |
| | 19 | | public static class RouterDbExtensions |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Gets a geojson representation of the data in the router db. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="routerDb">The router db.</param> |
| | 25 | | /// <param name="box">The bbox of the part of the network to extract.</param> |
| | 26 | | /// <param name="profiles">The profiles, if output related to profiles is needed.</param> |
| | 27 | | /// <returns>A string with geojson.</returns> |
| | 28 | | public static string ToGeoJson(this RoutingNetwork routerDb, |
| | 29 | | ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, float? e) |
| | 30 | | bottomRight)? box = null, IEnumerable<Profile>? profiles = null) |
| 0 | 31 | | { |
| 0 | 32 | | profiles ??= ArraySegment<Profile>.Empty; |
| | 33 | |
|
| 0 | 34 | | using var stream = new MemoryStream(); |
| 0 | 35 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| 0 | 36 | | { |
| 0 | 37 | | jsonWriter.WriteFeatureCollectionStart(); |
| 0 | 38 | | jsonWriter.WriteFeatures(routerDb, box); |
| 0 | 39 | | jsonWriter.WriteFeatureCollectionEnd(); |
| 0 | 40 | | } |
| | 41 | |
|
| 0 | 42 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Gets a geojson representation of given edge in the routing network. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="routerDb">The routing network.</param> |
| | 49 | | /// <param name="edgeId">The edge id.</param> |
| | 50 | | /// <returns>A string with geojson.</returns> |
| | 51 | | public static string ToGeoJson(this RoutingNetwork routerDb, EdgeId edgeId) |
| 0 | 52 | | { |
| 0 | 53 | | var edgeEnumerator = routerDb.GetEdgeEnumerator(); |
| 0 | 54 | | if (!edgeEnumerator.MoveTo(edgeId)) throw new Exception("Edge not found"); |
| | 55 | |
|
| 0 | 56 | | using var stream = new MemoryStream(); |
| 0 | 57 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| 0 | 58 | | { |
| 0 | 59 | | jsonWriter.WriteFeatureCollectionStart(); |
| 0 | 60 | | jsonWriter.WriteEdgeFeature(edgeEnumerator); |
| 0 | 61 | | jsonWriter.WriteFeatureCollectionEnd(); |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Writes features to the given json writer. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="jsonWriter">The json writer.</param> |
| | 71 | | /// <param name="routingNetwork">The routing network.</param> |
| | 72 | | /// <param name="box">The bounding box.</param> |
| | 73 | | public static void WriteFeatures(this Utf8JsonWriter jsonWriter, RoutingNetwork routingNetwork, |
| | 74 | | ((double longitude, double latitude, float? e) topLeft, (double longitude, double latitude, float? e) |
| | 75 | | bottomRight)? box) |
| 0 | 76 | | { |
| 0 | 77 | | var vertices = new HashSet<VertexId>(); |
| 0 | 78 | | var edges = new HashSet<EdgeId>(); |
| | 79 | |
|
| 0 | 80 | | if (box == null) |
| 0 | 81 | | { |
| 0 | 82 | | var vertexEnumerator = routingNetwork.GetVertexEnumerator(); |
| 0 | 83 | | var edgeEnumerator = routingNetwork.GetEdgeEnumerator(); |
| 0 | 84 | | while (vertexEnumerator.MoveNext()) |
| 0 | 85 | | { |
| 0 | 86 | | edgeEnumerator.MoveTo(vertexEnumerator.Current); |
| | 87 | |
|
| 0 | 88 | | while (edgeEnumerator.MoveNext()) |
| 0 | 89 | | { |
| 0 | 90 | | var tail = edgeEnumerator.Tail; |
| 0 | 91 | | if (!vertices.Contains(tail)) |
| 0 | 92 | | { |
| 0 | 93 | | jsonWriter.WriteVertexFeature(tail, routingNetwork); |
| 0 | 94 | | vertices.Add(tail); |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | var head = edgeEnumerator.Head; |
| 0 | 98 | | if (!vertices.Contains(head)) |
| 0 | 99 | | { |
| 0 | 100 | | jsonWriter.WriteVertexFeature(head, routingNetwork); |
| 0 | 101 | | vertices.Add(head); |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | var edge = edgeEnumerator.EdgeId; |
| 0 | 105 | | if (!edges.Contains(edge)) |
| 0 | 106 | | { |
| 0 | 107 | | jsonWriter.WriteEdgeFeature(routingNetwork, edgeEnumerator); |
| 0 | 108 | | edges.Add(edge); |
| 0 | 109 | | } |
| 0 | 110 | | } |
| 0 | 111 | | } |
| 0 | 112 | | } |
| | 113 | | else |
| 0 | 114 | | { |
| 0 | 115 | | var edgeEnumerator = routingNetwork.SearchEdgesInBox(box.Value); |
| 0 | 116 | | while (edgeEnumerator.MoveNext()) |
| 0 | 117 | | { |
| 0 | 118 | | var tail = edgeEnumerator.Tail; |
| 0 | 119 | | if (!vertices.Contains(tail)) |
| 0 | 120 | | { |
| 0 | 121 | | jsonWriter.WriteVertexFeature(tail, routingNetwork); |
| 0 | 122 | | vertices.Add(tail); |
| 0 | 123 | | } |
| | 124 | |
|
| 0 | 125 | | var head = edgeEnumerator.Head; |
| 0 | 126 | | if (!vertices.Contains(head)) |
| 0 | 127 | | { |
| 0 | 128 | | jsonWriter.WriteVertexFeature(head, routingNetwork); |
| 0 | 129 | | vertices.Add(head); |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | var edge = edgeEnumerator.EdgeId; |
| 0 | 133 | | if (!edges.Contains(edge)) |
| 0 | 134 | | { |
| 0 | 135 | | jsonWriter.WriteEdgeFeature(routingNetwork, edgeEnumerator); |
| 0 | 136 | | edges.Add(edge); |
| 0 | 137 | | } |
| 0 | 138 | | } |
| 0 | 139 | | } |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Writes a vertex as a feature. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="jsonWriter">The json writer.</param> |
| | 146 | | /// <param name="routerDb">The router db.</param> |
| | 147 | | /// <param name="vertexId">The vertex id.</param> |
| | 148 | | public static void WriteVertexFeature(this Utf8JsonWriter jsonWriter, VertexId vertexId, |
| | 149 | | RoutingNetwork routerDb) |
| 0 | 150 | | { |
| 0 | 151 | | jsonWriter.WriteVertexFeature(vertexId, routerDb.GetVertex(vertexId)); |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Writes a vertex as a feature. |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="jsonWriter">The json writer.</param> |
| | 158 | | /// <param name="vertexId">The vertex id.</param> |
| | 159 | | /// <param name="location">The location.</param> |
| | 160 | | public static void WriteVertexFeature(this Utf8JsonWriter jsonWriter, VertexId vertexId, |
| | 161 | | (double longitude, double latitude, float? e) location) |
| 0 | 162 | | { |
| 0 | 163 | | jsonWriter.WriteFeatureStart(); |
| 0 | 164 | | jsonWriter.WriteProperties(new (string key, string value)[] |
| 0 | 165 | | { |
| 0 | 166 | | ("_tile_id", vertexId.TileId.ToString()), ("_local_id", vertexId.LocalId.ToString()) |
| 0 | 167 | | }); |
| 0 | 168 | | jsonWriter.WritePropertyName("geometry"); |
| 0 | 169 | | jsonWriter.WritePoint(location); |
| 0 | 170 | | jsonWriter.WriteFeatureEnd(); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Writes an edge as a feature. |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="jsonWriter">The json writer.</param> |
| | 177 | | /// <param name="routingNetwork">The routing network db.</param> |
| | 178 | | /// <param name="enumerator">The enumerator.</param> |
| | 179 | | public static void WriteEdgeFeature(this Utf8JsonWriter jsonWriter, |
| | 180 | | RoutingNetwork routingNetwork, IEdgeEnumerator enumerator) |
| 0 | 181 | | { |
| 0 | 182 | | jsonWriter.WriteFeatureStart(); |
| 0 | 183 | | var attributes = enumerator.Attributes.ToList(); |
| 0 | 184 | | if (enumerator.Forward) |
| 0 | 185 | | { |
| 0 | 186 | | attributes.AddRange(new (string key, string value)[] |
| 0 | 187 | | { |
| 0 | 188 | | ("_tail_tile_id", enumerator.Tail.TileId.ToString()), |
| 0 | 189 | | ("_tail_local_id", enumerator.Tail.LocalId.ToString()), |
| 0 | 190 | | ("_head_tile_id", enumerator.Head.TileId.ToString()), |
| 0 | 191 | | ("_head_local_id", enumerator.Head.LocalId.ToString()), |
| 0 | 192 | | ("_edge_id", enumerator.EdgeId.ToString()) |
| 0 | 193 | | }); |
| 0 | 194 | | } |
| | 195 | | else |
| 0 | 196 | | { |
| 0 | 197 | | attributes.AddRange(new (string key, string value)[] |
| 0 | 198 | | { |
| 0 | 199 | | ("_head_tile_id", enumerator.Tail.TileId.ToString()), |
| 0 | 200 | | ("_head_local_id", enumerator.Tail.LocalId.ToString()), |
| 0 | 201 | | ("_tail_tile_id", enumerator.Head.TileId.ToString()), |
| 0 | 202 | | ("_tail_local_id", enumerator.Head.LocalId.ToString()), |
| 0 | 203 | | ("_edge_id", enumerator.EdgeId.ToString()) |
| 0 | 204 | | }); |
| 0 | 205 | | } |
| | 206 | |
|
| 0 | 207 | | if (enumerator.TailOrder.HasValue) attributes.AddOrReplace("_tail_order", enumerator.TailOrder.Value.ToString()) |
| 0 | 208 | | if (enumerator.HeadOrder.HasValue) attributes.AddOrReplace("_head_order", enumerator.HeadOrder.Value.ToString()) |
| | 209 | |
|
| 0 | 210 | | foreach (var profileName in routingNetwork.RouterDb.ProfileConfiguration.GetProfileNames()) |
| 0 | 211 | | { |
| 0 | 212 | | if (!routingNetwork.RouterDb.ProfileConfiguration.TryGetProfileHandlerEdgeTypesCache(profileName, out var ed |
| 0 | 213 | | out _)) continue; |
| | 214 | |
|
| 0 | 215 | | if (edgeFactorCache == null) continue; |
| 0 | 216 | | if (!enumerator.EdgeTypeId.HasValue) continue; |
| | 217 | |
|
| 0 | 218 | | var factor = edgeFactorCache.Get(enumerator.EdgeTypeId.Value); |
| 0 | 219 | | if (factor == null) continue; |
| | 220 | |
|
| 0 | 221 | | attributes.AddOrReplace($"_{profileName}_factor_forward", |
| 0 | 222 | | factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 223 | | attributes.AddOrReplace($"_{profileName}_factor_backward", |
| 0 | 224 | | factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 225 | | attributes.AddOrReplace($"_{profileName}_speed_forward", |
| 0 | 226 | | factor.Value.ForwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 227 | | attributes.AddOrReplace($"_{profileName}_speed_backward", |
| 0 | 228 | | factor.Value.BackwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| | 229 | |
|
| 0 | 230 | | if (!routingNetwork.IslandManager.TryGetIslandsFor(profileName, out var islands)) continue; |
| 0 | 231 | | if (!islands.GetTileDone(enumerator.Tail.TileId)) continue; |
| | 232 | |
|
| 0 | 233 | | if (factor.Value.ForwardFactor > 0 || factor.Value.BackwardFactor > 0) |
| 0 | 234 | | { |
| 0 | 235 | | attributes.AddOrReplace($"_{profileName}_island", |
| 0 | 236 | | islands.IsEdgeOnIsland(enumerator.EdgeId).ToString().ToLowerInvariant()); |
| 0 | 237 | | } |
| 0 | 238 | | } |
| | 239 | |
|
| 0 | 240 | | jsonWriter.WriteProperties(attributes); |
| 0 | 241 | | jsonWriter.WritePropertyName("geometry"); |
| 0 | 242 | | jsonWriter.WriteLineString(enumerator.GetCompleteShape()); |
| 0 | 243 | | jsonWriter.WriteFeatureEnd(); |
| 0 | 244 | | } |
| | 245 | |
|
| | 246 | | /// <summary> |
| | 247 | | /// Writes an edge as a feature. |
| | 248 | | /// </summary> |
| | 249 | | /// <param name="jsonWriter">The json writer.</param> |
| | 250 | | /// <param name="enumerator">The enumerator.</param> |
| | 251 | | public static void WriteEdgeFeature(this Utf8JsonWriter jsonWriter, |
| | 252 | | IEdgeEnumerator enumerator) |
| 0 | 253 | | { |
| 0 | 254 | | jsonWriter.WriteFeatureStart(); |
| 0 | 255 | | var attributes = enumerator.Attributes.ToList(); |
| 0 | 256 | | attributes.AddRange(new (string key, string value)[] |
| 0 | 257 | | { |
| 0 | 258 | | ("tail_tile_id", enumerator.Tail.TileId.ToString()), |
| 0 | 259 | | ("tail_local_id", enumerator.Tail.LocalId.ToString()), |
| 0 | 260 | | ("head_tile_id", enumerator.Head.TileId.ToString()), |
| 0 | 261 | | ("head_local_id", enumerator.Head.LocalId.ToString()), ("edge_id", enumerator.EdgeId.ToString()) |
| 0 | 262 | | }); |
| 0 | 263 | | if (enumerator.TailOrder.HasValue) attributes.AddOrReplace("tail_order", enumerator.TailOrder.Value.ToString()); |
| 0 | 264 | | if (enumerator.HeadOrder.HasValue) attributes.AddOrReplace("head_order", enumerator.HeadOrder.Value.ToString()); |
| 0 | 265 | | jsonWriter.WriteProperties(attributes); |
| 0 | 266 | | jsonWriter.WritePropertyName("geometry"); |
| 0 | 267 | | jsonWriter.WriteLineString(enumerator.GetCompleteShape()); |
| 0 | 268 | | jsonWriter.WriteFeatureEnd(); |
| 0 | 269 | | } |
| | 270 | | } |