| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Linq; |
| | 5 | | using Itinero.Geo; |
| | 6 | | using Itinero.IO.Osm.Restrictions; |
| | 7 | | using Itinero.IO.Osm.Restrictions.Barriers; |
| | 8 | | using Itinero.IO.Osm.Streams; |
| | 9 | | using Itinero.Network; |
| | 10 | | using Itinero.Network.Tiles.Standalone; |
| | 11 | | using Itinero.Network.Tiles.Standalone.Writer; |
| | 12 | | using OsmSharp; |
| | 13 | | using OsmSharp.Streams; |
| | 14 | |
|
| | 15 | | namespace Itinero.IO.Osm.Tiles; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Contains extensions methods for the standalone tile writer. |
| | 19 | | /// </summary> |
| | 20 | | public static class StandaloneNetworkTileWriterExtensions |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Adds tile data using the writer and the given tile data. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="writer">The writer.</param> |
| | 26 | | /// <param name="tileData">The OSM data in the tile.</param> |
| | 27 | | /// <param name="configure">The configure function.</param> |
| | 28 | | /// <exception cref="Exception"></exception> |
| | 29 | | public static void AddTileData(this StandaloneNetworkTileWriter writer, IEnumerable<OsmGeo> tileData, |
| | 30 | | Action<DataProviderSettings>? configure = null) |
| 3 | 31 | | { |
| | 32 | | // create settings. |
| 3 | 33 | | var settings = new DataProviderSettings(); |
| 3 | 34 | | configure?.Invoke(settings); |
| | 35 | |
|
| | 36 | | // do filtering. |
| | 37 | | // ReSharper disable once PossibleMultipleEnumeration |
| 3 | 38 | | OsmStreamSource data = new OsmEnumerableStreamSource(tileData); |
| | 39 | |
|
| | 40 | | // 1: complete objects. |
| 3 | 41 | | if (settings.TagsFilter.CompleteFilter != null) |
| 0 | 42 | | { |
| 0 | 43 | | data = data.ApplyCompleteFilter(settings.TagsFilter.CompleteFilter); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | // 2: filter relations. |
| 3 | 47 | | if (settings.TagsFilter.MemberFilter != null) |
| 3 | 48 | | { |
| 3 | 49 | | data = data.ApplyRelationMemberFilter(settings.TagsFilter.MemberFilter); |
| 3 | 50 | | } |
| | 51 | |
|
| | 52 | | // 3: filter tags on ways and relations. |
| 3 | 53 | | if (settings.TagsFilter.Filter != null) |
| 3 | 54 | | { |
| 3 | 55 | | data = data.ApplyFilter(settings.TagsFilter.Filter); |
| 3 | 56 | | } |
| | 57 | |
|
| | 58 | | // setup the edge type map. |
| 3 | 59 | | var edgeTypeMap = writer.EdgeTypeMap.func; |
| 3 | 60 | | var emptyEdgeType = edgeTypeMap(ArraySegment<(string key, string value)>.Empty); |
| | 61 | |
|
| | 62 | | // keep all node locations. |
| 3 | 63 | | var nodeLocations = new Dictionary<long, (double longitude, double latitude, bool inside)>(); |
| | 64 | | // keep a set of used nodes, all nodes inside the tile. |
| 3 | 65 | | var usedNodes = new HashSet<long>(); |
| | 66 | | // keep a set of core nodes, all nodes inside the tile that should be a vertex |
| 3 | 67 | | var coreNodes = new HashSet<long>(); |
| | 68 | | // keep a set of boundary nodes, all boundary nodes (first node outside the tile). |
| 3 | 69 | | var boundaryNodes = new HashSet<long>(); |
| | 70 | |
|
| | 71 | | // first pass to: |
| | 72 | | // - mark nodes are core, they are to be become vertices later. |
| | 73 | | // - parse restrictions and keep restricted edges and mark nodes as core. |
| 3 | 74 | | using var enumerator = data.GetEnumerator(); |
| | 75 | |
|
| 3 | 76 | | var osmTurnRestrictions = new List<OsmTurnRestriction>(); |
| 3 | 77 | | var restrictedEdges = new Dictionary<Guid, BoundaryOrLocalEdgeId?>(); |
| 3 | 78 | | var restrictionMembers = new Dictionary<long, Way?>(); |
| 3 | 79 | | var restrictionParser = new OsmTurnRestrictionParser(); |
| | 80 | |
|
| 3 | 81 | | var osmBarriers = new List<OsmBarrier>(); |
| 3 | 82 | | var barrierParser = new OsmBarrierParser(); |
| 15 | 83 | | while (enumerator.MoveNext()) |
| 12 | 84 | | { |
| 12 | 85 | | var current = enumerator.Current; |
| | 86 | |
|
| 12 | 87 | | switch (current) |
| | 88 | | { |
| | 89 | | case Node node: |
| 7 | 90 | | if (node.Id == null) throw new Exception("Id cannot be null"); |
| 7 | 91 | | if (node.Longitude == null) throw new Exception("Longitude cannot be null"); |
| 7 | 92 | | if (node.Latitude == null) throw new Exception("Latitude cannot be null"); |
| | 93 | |
|
| 7 | 94 | | var nodeInTile = writer.IsInTile(node.Longitude.Value, node.Latitude.Value); |
| 7 | 95 | | nodeLocations[node.Id.Value] = (node.Longitude.Value, node.Latitude.Value, |
| 7 | 96 | | nodeInTile); |
| | 97 | |
|
| 7 | 98 | | if (barrierParser.IsBarrier(node)) |
| 0 | 99 | | { |
| | 100 | | // make sure the barriers are core nodes, they need a turn cost. |
| | 101 | | // log nodes are barriers to be able to detect their ways, |
| | 102 | | // only take nodes in the tile to mark as barrier. |
| 0 | 103 | | coreNodes.Add(node.Id.Value); |
| 0 | 104 | | } |
| | 105 | |
|
| 7 | 106 | | break; |
| 4 | 107 | | case Way way when way.Nodes.Length == 0: |
| 0 | 108 | | break; |
| | 109 | | case Way way: |
| 4 | 110 | | { |
| | 111 | | // calculate edge type and determine if there is relevant data. |
| 8 | 112 | | var attributes = way.Tags?.Select(tag => (tag.Key, tag.Value)).ToArray() ?? |
| 4 | 113 | | ArraySegment<(string key, string value)>.Empty; |
| 4 | 114 | | var edgeTypeId = edgeTypeMap(attributes); |
| 4 | 115 | | if (edgeTypeId == emptyEdgeType) continue; |
| | 116 | |
|
| | 117 | | // mark as core nodes used twice or nodes representing a boundary crossing. |
| 4 | 118 | | bool? previousInTile = null; |
| 24 | 119 | | for (var n = 0; n < way.Nodes.Length; n++) |
| 8 | 120 | | { |
| 8 | 121 | | var wayNode = way.Nodes[n]; |
| | 122 | |
|
| | 123 | | // check if the node is in the tile or not. |
| 8 | 124 | | if (!nodeLocations.TryGetValue(wayNode, out var value)) |
| 0 | 125 | | { |
| 0 | 126 | | throw new Exception( |
| 0 | 127 | | $"Node {wayNode} as part of way {way.Id} not found in source data."); |
| | 128 | | } |
| 8 | 129 | | var (_, _, inTile) = value; |
| | 130 | |
|
| | 131 | | // mark as core if used before and mark as used. |
| 8 | 132 | | if (inTile) |
| 7 | 133 | | { |
| | 134 | | // only mark nodes used when in the tile. |
| 8 | 135 | | if (usedNodes.Contains(wayNode)) coreNodes.Add(wayNode); |
| 7 | 136 | | usedNodes.Add(wayNode); |
| | 137 | |
|
| | 138 | | // if first or last node and inside always core. |
| 7 | 139 | | if (n == 0 || n == way.Nodes.Length - 1) |
| 7 | 140 | | { |
| 7 | 141 | | coreNodes.Add(wayNode); |
| 7 | 142 | | } |
| 7 | 143 | | } |
| | 144 | |
|
| | 145 | | // boundary crossing, from outside to in. |
| 8 | 146 | | if (previousInTile is false && inTile == true) |
| 0 | 147 | | { |
| 0 | 148 | | boundaryNodes.Add(way.Nodes[n - 1]); |
| 0 | 149 | | coreNodes.Add(way.Nodes[n]); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | // boundary crossing, from inside to out. |
| 8 | 153 | | if (previousInTile is true && inTile == false) |
| 1 | 154 | | { |
| 1 | 155 | | coreNodes.Add(way.Nodes[n - 1]); |
| 1 | 156 | | boundaryNodes.Add(way.Nodes[n]); |
| 1 | 157 | | } |
| | 158 | |
|
| 8 | 159 | | previousInTile = inTile; |
| 8 | 160 | | } |
| | 161 | |
|
| 4 | 162 | | break; |
| | 163 | | } |
| | 164 | | case Relation relation: |
| 1 | 165 | | if (!restrictionParser.IsRestriction(relation, out _)) continue; |
| 1 | 166 | | if (relation.Members == null) continue; |
| | 167 | |
|
| | 168 | | // log ways that are members, we need to keep their edge ids ready |
| | 169 | | // or store their global ids when the restriction crosses tile boundaries. |
| 9 | 170 | | foreach (var relationMember in relation.Members) |
| 3 | 171 | | { |
| 4 | 172 | | if (relationMember.Type != OsmGeoType.Way) continue; |
| | 173 | |
|
| 2 | 174 | | restrictionMembers[relationMember.Id] = null; |
| 2 | 175 | | } |
| | 176 | |
|
| 1 | 177 | | break; |
| | 178 | | } |
| 12 | 179 | | } |
| | 180 | |
|
| | 181 | | // a second pass where we add all vertices, core nodes and edges. |
| | 182 | | // we also keep an index of edges that were added and are part of a restriction. |
| 3 | 183 | | var vertices = new Dictionary<long, VertexId>(); |
| 3 | 184 | | enumerator.Reset(); |
| 15 | 185 | | while (enumerator.MoveNext()) |
| 12 | 186 | | { |
| 12 | 187 | | var current = enumerator.Current; |
| | 188 | |
|
| 12 | 189 | | switch (current) |
| | 190 | | { |
| | 191 | | case Node node: |
| 7 | 192 | | if (node.Id == null) throw new Exception("Id cannot be null"); |
| 7 | 193 | | if (node.Longitude == null) throw new Exception("Longitude cannot be null"); |
| 7 | 194 | | if (node.Latitude == null) throw new Exception("Latitude cannot be null"); |
| | 195 | |
|
| | 196 | | // add all core nodes as vertices. |
| 7 | 197 | | if (coreNodes.Contains(node.Id.Value)) |
| 6 | 198 | | { |
| 6 | 199 | | if (!writer.IsInTile(node.Longitude.Value, node.Latitude.Value)) continue; |
| 6 | 200 | | vertices[node.Id.Value] = writer.AddVertex(node.Longitude.Value, node.Latitude.Value); |
| | 201 | |
|
| | 202 | | // a core not can be a barrier, check here. |
| 6 | 203 | | if (barrierParser.TryParse(node, out var barrier)) |
| 0 | 204 | | { |
| 0 | 205 | | osmBarriers.Add(barrier); |
| 0 | 206 | | } |
| 6 | 207 | | } |
| | 208 | |
|
| 7 | 209 | | break; |
| 4 | 210 | | case Way way when way.Nodes.Length == 0: |
| 0 | 211 | | continue; |
| | 212 | | case Way way: |
| 4 | 213 | | { |
| 6 | 214 | | if (restrictionMembers.ContainsKey(way.Id.Value)) restrictionMembers[way.Id.Value] = way; |
| | 215 | |
|
| 8 | 216 | | var attributes = way.Tags?.Select(tag => (tag.Key, tag.Value)).ToArray() ?? |
| 4 | 217 | | ArraySegment<(string key, string value)>.Empty; |
| 4 | 218 | | var edgeTypeId = edgeTypeMap(attributes); |
| 4 | 219 | | if (edgeTypeId == emptyEdgeType) continue; |
| | 220 | |
|
| | 221 | | // add all boundaries, if any. |
| 16 | 222 | | for (var n = 1; n < way.Nodes.Length; n++) |
| 4 | 223 | | { |
| 4 | 224 | | var from = way.Nodes[n - 1]; |
| 4 | 225 | | var to = way.Nodes[n]; |
| 4 | 226 | | var globalEdgeId = way.GenerateGlobalEdgeId(n - 1, n); |
| | 227 | |
|
| 4 | 228 | | var fromLocation = nodeLocations[from]; |
| 4 | 229 | | var toLocation = nodeLocations[to]; |
| | 230 | |
|
| 4 | 231 | | var length = (uint)((fromLocation.longitude, fromLocation.latitude, (float?)null) |
| 4 | 232 | | .DistanceEstimateInMeter( |
| 4 | 233 | | (toLocation.longitude, toLocation.latitude, (float?)null)) * 100); |
| | 234 | |
|
| 4 | 235 | | if (boundaryNodes.Contains(from) && |
| 4 | 236 | | vertices.TryGetValue(to, out var vertexId)) |
| 0 | 237 | | { |
| 0 | 238 | | var boundaryEdgeId = writer.AddBoundaryCrossing(from, (vertexId, to), |
| 0 | 239 | | edgeTypeId, attributes.Concat(globalEdgeId), length); |
| 0 | 240 | | if (restrictionMembers.ContainsKey(way.Id.Value)) |
| 0 | 241 | | restrictedEdges[globalEdgeId] = new BoundaryOrLocalEdgeId(boundaryEdgeId); |
| 0 | 242 | | } |
| 4 | 243 | | else if (boundaryNodes.Contains(to) && |
| 4 | 244 | | vertices.TryGetValue(from, out vertexId)) |
| 1 | 245 | | { |
| 1 | 246 | | var boundaryEdgeId = writer.AddBoundaryCrossing((vertexId, from), to, |
| 1 | 247 | | edgeTypeId, attributes.Concat(globalEdgeId), length); |
| 1 | 248 | | if (restrictionMembers.ContainsKey(way.Id.Value)) |
| 0 | 249 | | restrictedEdges[globalEdgeId] = new BoundaryOrLocalEdgeId(boundaryEdgeId); |
| 1 | 250 | | } |
| 4 | 251 | | } |
| | 252 | |
|
| | 253 | | // add regular edges, if any. |
| 4 | 254 | | var shape = new List<(double longitude, double latitude, float? e)>(); |
| 4 | 255 | | VertexId? previousVertex = null; |
| 4 | 256 | | var previousNode = -1; |
| | 257 | |
|
| 24 | 258 | | for (var n = 0; n < way.Nodes.Length; n++) |
| 8 | 259 | | { |
| 8 | 260 | | var wayNode = way.Nodes[n]; |
| | 261 | |
|
| 8 | 262 | | if (boundaryNodes.Contains(wayNode)) |
| 1 | 263 | | { |
| 1 | 264 | | previousVertex = null; |
| 1 | 265 | | shape.Clear(); |
| 1 | 266 | | continue; |
| | 267 | | } |
| | 268 | |
|
| 7 | 269 | | if (!vertices.TryGetValue(wayNode, out var vertexId)) |
| 0 | 270 | | { |
| 0 | 271 | | var (longitude, latitude, _) = nodeLocations[wayNode]; |
| | 272 | |
|
| 0 | 273 | | shape.Add((longitude, latitude, null)); |
| 0 | 274 | | continue; |
| | 275 | | } |
| | 276 | |
|
| 7 | 277 | | if (previousVertex != null) |
| 3 | 278 | | { |
| 3 | 279 | | var globalEdgeId = way.GenerateGlobalEdgeId(previousNode, n); |
| 3 | 280 | | var edgeId = writer.AddEdge(previousVertex.Value, vertexId, edgeTypeId, shape, |
| 3 | 281 | | attributes.Concat(globalEdgeId)); |
| 3 | 282 | | shape.Clear(); |
| | 283 | |
|
| 3 | 284 | | if (restrictionMembers.ContainsKey(way.Id.Value)) |
| 2 | 285 | | restrictedEdges[globalEdgeId] = new BoundaryOrLocalEdgeId(edgeId); |
| 3 | 286 | | } |
| | 287 | |
|
| 7 | 288 | | previousVertex = vertexId; |
| 7 | 289 | | previousNode = n; |
| 7 | 290 | | } |
| | 291 | |
|
| 4 | 292 | | break; |
| | 293 | | } |
| | 294 | | case Relation relation: |
| 1 | 295 | | var result = restrictionParser.TryParse(relation, (wayId) => |
| 2 | 296 | | restrictionMembers.TryGetValue(wayId, out var member) ? member : null, |
| 1 | 297 | | out var osmTurnRestriction); |
| | 298 | |
|
| 1 | 299 | | if (result.IsError) continue; |
| 1 | 300 | | if (!result.Value) continue; |
| 1 | 301 | | if (osmTurnRestriction == null) |
| 0 | 302 | | throw new Exception("Parsing restriction was successful but not returned"); |
| | 303 | |
|
| 1 | 304 | | osmTurnRestrictions.Add(osmTurnRestriction); |
| | 305 | |
|
| 1 | 306 | | break; |
| | 307 | | } |
| 12 | 308 | | } |
| | 309 | |
|
| | 310 | | // add barriers as turn weights. |
| 3 | 311 | | var tileEnumerator = writer.GetEnumerator(); |
| 3 | 312 | | var networkRestrictions = new List<NetworkRestriction>(); |
| 9 | 313 | | foreach (var osmBarrier in osmBarriers) |
| 0 | 314 | | { |
| 0 | 315 | | var networkBarriersResult = osmBarrier.ToNetworkRestrictions(n => |
| 0 | 316 | | { |
| 0 | 317 | | if (!vertices.TryGetValue(n, out var v)) throw new Exception("Node should exist as vertex"); |
| 0 | 318 | | tileEnumerator.MoveTo(v); |
| 0 | 319 | | return tileEnumerator; |
| 0 | 320 | | }); |
| 0 | 321 | | if (networkBarriersResult.IsError) continue; |
| | 322 | |
|
| 0 | 323 | | networkRestrictions.AddRange(networkBarriersResult.Value); |
| 0 | 324 | | } |
| | 325 | |
|
| | 326 | | // add restrictions as turn weights. |
| 11 | 327 | | foreach (var osmTurnRestriction in osmTurnRestrictions) |
| 1 | 328 | | { |
| 1 | 329 | | var networkRestrictionsResult = osmTurnRestriction.ToNetworkRestrictions((wayId, node1, node2) => |
| 2 | 330 | | { |
| 2 | 331 | | var (globalId, forward) = GlobalEdgeIdExtensions.GenerateGlobalEdgeIdAndDirection(wayId, node1, node2); |
| 1 | 332 | |
|
| 2 | 333 | | if (!restrictedEdges.TryGetValue(globalId, out var boundaryOrLocalEdgeId)) return null; |
| 1 | 334 | |
|
| 4 | 335 | | if (boundaryOrLocalEdgeId?.LocalId != null) return (boundaryOrLocalEdgeId.Value.LocalId.Value, forward); |
| 1 | 336 | |
|
| 0 | 337 | | return null; |
| 3 | 338 | | }); |
| 1 | 339 | | if (networkRestrictionsResult.IsError) continue; |
| | 340 | |
|
| 1 | 341 | | networkRestrictions.AddRange(networkRestrictionsResult.Value); |
| 1 | 342 | | } |
| | 343 | |
|
| | 344 | | // convert network restrictions to turn costs. |
| 11 | 345 | | foreach (var networkRestriction in networkRestrictions) |
| 1 | 346 | | { |
| 1 | 347 | | if (networkRestriction.Count < 2) |
| 0 | 348 | | { |
| | 349 | | // TODO: log something? |
| 0 | 350 | | continue; |
| | 351 | | } |
| | 352 | |
|
| | 353 | | // get last edge and turn cost vertex. |
| 1 | 354 | | var last = networkRestriction[^1]; |
| 1 | 355 | | var lastEdge = writer.GetEdge(last.edge, last.forward); |
| 1 | 356 | | var turnCostVertex = lastEdge.Tail; |
| | 357 | |
|
| | 358 | | // only add turn costs around vertices that are in the current tile. |
| 1 | 359 | | if (turnCostVertex.TileId != writer.TileId) continue; |
| | 360 | |
|
| 1 | 361 | | var secondToLast = networkRestriction[^2]; |
| 1 | 362 | | if (networkRestriction.IsProhibitory) |
| 1 | 363 | | { |
| | 364 | | // easy, we only add a single cost. |
| 1 | 365 | | var costs = new uint[,] { { 0, 1 }, { 0, 0 } }; |
| 1 | 366 | | writer.AddTurnCosts(turnCostVertex, networkRestriction.Attributes, |
| 1 | 367 | | new[] { secondToLast.edge, last.edge }, costs, |
| 1 | 368 | | networkRestriction.Take(networkRestriction.Count - 2).Select(x => x.edge)); |
| 1 | 369 | | } |
| | 370 | | else |
| 0 | 371 | | { |
| | 372 | | // hard, we need to add a cost for every *other* edge than then one in the restriction. |
| 0 | 373 | | tileEnumerator.MoveTo(secondToLast.edge, secondToLast.forward); |
| 0 | 374 | | var to = tileEnumerator.Head; |
| 0 | 375 | | tileEnumerator.MoveTo(to); |
| | 376 | |
|
| 0 | 377 | | while (tileEnumerator.MoveNext()) |
| 0 | 378 | | { |
| 0 | 379 | | if (tileEnumerator.EdgeId == secondToLast.edge || |
| 0 | 380 | | tileEnumerator.EdgeId == lastEdge.EdgeId) continue; |
| | 381 | |
|
| | 382 | | // easy, we only add a single cost. |
| 0 | 383 | | var costs = new uint[,] { { 0, 1 }, { 0, 0 } }; |
| 0 | 384 | | writer.AddTurnCosts(turnCostVertex, networkRestriction.Attributes, |
| 0 | 385 | | new[] { secondToLast.edge, tileEnumerator.EdgeId }, costs, |
| 0 | 386 | | networkRestriction.Take(networkRestriction.Count - 2).Select(x => x.edge)); |
| 0 | 387 | | } |
| 0 | 388 | | } |
| 1 | 389 | | } |
| | 390 | |
|
| | 391 | | // add global ids. |
| 13 | 392 | | foreach (var (globalEdgeId, restrictedEdge) in restrictedEdges) |
| 2 | 393 | | { |
| 2 | 394 | | if (restrictedEdge?.LocalId != null) |
| 2 | 395 | | { |
| 2 | 396 | | writer.AddGlobalIdFor(restrictedEdge.Value.LocalId.Value, globalEdgeId); |
| 2 | 397 | | } |
| 0 | 398 | | else if (restrictedEdge?.BoundaryId != null) |
| 0 | 399 | | { |
| 0 | 400 | | writer.AddGlobalIdFor(restrictedEdge.Value.BoundaryId.Value, globalEdgeId); |
| 0 | 401 | | } |
| 2 | 402 | | } |
| | 403 | |
|
| | 404 | | // we can only add turn restrictions when all their edges |
| | 405 | | // are full within a single tile, when they are not we add them |
| | 406 | | // to the tile as boundary restrictions using global edge ids. |
| 6 | 407 | | } |
| | 408 | |
|
| | 409 | | internal static IStandaloneNetworkTileEnumerator GetEnumerator(this StandaloneNetworkTileWriter writer) |
| 3 | 410 | | { |
| 3 | 411 | | return writer.GetResultingTile().GetEnumerator(); |
| 3 | 412 | | } |
| | 413 | | } |