| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text; |
| | 5 | | using System.Text.Json; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Itinero.IO.Json.GeoJson; |
| | 8 | | using Itinero.Network.Attributes; |
| | 9 | | using Itinero.Network.Enumerators.Edges; |
| | 10 | |
|
| | 11 | | namespace Itinero.Network.Search.Islands; |
| | 12 | |
|
| | 13 | | internal static class IslandLabelsExtensions |
| | 14 | | { |
| | 15 | | internal static (uint label, uint size, bool final) GetOrCreateLabel(this IslandLabels labels, RoutingNetworkEdgeEnu |
| | 16 | | Func<IEdgeEnumerator, bool?>? isOnIslandAlready = null) |
| 23 | 17 | | { |
| 33 | 18 | | if (labels.TryGetWithDetails(edgeEnumerator.EdgeId, out var neighbourLabelDetails)) return neighbourLabelDetails |
| | 19 | |
|
| | 20 | | // check if the neighbour has a status already we can use. |
| 13 | 21 | | var onIslandAlready = isOnIslandAlready?.Invoke(edgeEnumerator); |
| 13 | 22 | | if (onIslandAlready != null) |
| 1 | 23 | | { |
| | 24 | | // check and verify status |
| 1 | 25 | | if (onIslandAlready.Value) |
| 0 | 26 | | { |
| | 27 | | // neighbour has a known status and is on an island. |
| 0 | 28 | | neighbourLabelDetails = labels.AddNew(edgeEnumerator.EdgeId, true); |
| 0 | 29 | | } |
| | 30 | | else |
| 1 | 31 | | { |
| | 32 | | // neighbour has a known status and is not on an island. |
| 1 | 33 | | neighbourLabelDetails = labels.AddTo(IslandLabels.NotAnIslandLabel, edgeEnumerator.EdgeId); |
| 1 | 34 | | } |
| 1 | 35 | | } |
| | 36 | | else |
| 12 | 37 | | { |
| | 38 | | // no label was found, assign a new one. |
| 12 | 39 | | neighbourLabelDetails = labels.AddNew(edgeEnumerator.EdgeId); |
| 12 | 40 | | } |
| | 41 | |
|
| 13 | 42 | | return neighbourLabelDetails; |
| 23 | 43 | | } |
| | 44 | |
|
| | 45 | | public static async Task<string> ToGeoJson(this IslandLabels islandLabels, RoutingNetwork network) |
| 0 | 46 | | { |
| 0 | 47 | | using var stream = new MemoryStream(); |
| 0 | 48 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| 0 | 49 | | { |
| 0 | 50 | | jsonWriter.WriteFeatureCollectionStart(); |
| | 51 | |
|
| 0 | 52 | | var edgeEnumerator = network.GetEdgeEnumerator(); |
| 0 | 53 | | foreach (var (edge, label) in islandLabels) |
| 0 | 54 | | { |
| 0 | 55 | | if (!edgeEnumerator.MoveTo(edge)) continue; |
| 0 | 56 | | if (!islandLabels.TryGetWithDetails(edge, out var edgeDetails)) continue; |
| | 57 | |
|
| 0 | 58 | | jsonWriter.WriteEdgeFeatureWithIslandDetails(network, edgeEnumerator, edgeDetails); |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | jsonWriter.WriteFeatureCollectionEnd(); |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | internal static void WriteEdgeFeatureWithIslandDetails(this Utf8JsonWriter jsonWriter, |
| | 68 | | RoutingNetwork routingNetwork, RoutingNetworkEdgeEnumerator enumerator, (uint label, uint size, bool final) isla |
| 0 | 69 | | { |
| 0 | 70 | | jsonWriter.WriteFeatureStart(); |
| 0 | 71 | | var attributes = enumerator.Attributes.ToList(); |
| 0 | 72 | | if (enumerator.Forward) |
| 0 | 73 | | { |
| 0 | 74 | | attributes.AddRange([ |
| 0 | 75 | | ("_tail_tile_id", enumerator.Tail.TileId.ToString()), |
| 0 | 76 | | ("_tail_local_id", enumerator.Tail.LocalId.ToString()), |
| 0 | 77 | | ("_head_tile_id", enumerator.Head.TileId.ToString()), |
| 0 | 78 | | ("_head_local_id", enumerator.Head.LocalId.ToString()), |
| 0 | 79 | | ("_edge_id", enumerator.EdgeId.ToString()) |
| 0 | 80 | | ]); |
| 0 | 81 | | } |
| | 82 | | else |
| 0 | 83 | | { |
| 0 | 84 | | attributes.AddRange([ |
| 0 | 85 | | ("_head_tile_id", enumerator.Tail.TileId.ToString()), |
| 0 | 86 | | ("_head_local_id", enumerator.Tail.LocalId.ToString()), |
| 0 | 87 | | ("_tail_tile_id", enumerator.Head.TileId.ToString()), |
| 0 | 88 | | ("_tail_local_id", enumerator.Head.LocalId.ToString()), |
| 0 | 89 | | ("_edge_id", enumerator.EdgeId.ToString()) |
| 0 | 90 | | ]); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | if (enumerator.TailOrder.HasValue) attributes.AddOrReplace("_tail_order", enumerator.TailOrder.Value.ToString()) |
| 0 | 94 | | if (enumerator.HeadOrder.HasValue) attributes.AddOrReplace("_head_order", enumerator.HeadOrder.Value.ToString()) |
| | 95 | |
|
| 0 | 96 | | foreach (var profileName in routingNetwork.RouterDb.ProfileConfiguration.GetProfileNames()) |
| 0 | 97 | | { |
| 0 | 98 | | if (!routingNetwork.RouterDb.ProfileConfiguration.TryGetProfileHandlerEdgeTypesCache(profileName, out var ed |
| 0 | 99 | | out _)) continue; |
| | 100 | |
|
| 0 | 101 | | if (edgeFactorCache == null) continue; |
| 0 | 102 | | if (!enumerator.EdgeTypeId.HasValue) continue; |
| | 103 | |
|
| 0 | 104 | | var factor = edgeFactorCache.Get(enumerator.EdgeTypeId.Value); |
| 0 | 105 | | if (factor == null) continue; |
| | 106 | |
|
| 0 | 107 | | attributes.AddOrReplace($"_{profileName}_factor_forward", |
| 0 | 108 | | factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 109 | | attributes.AddOrReplace($"_{profileName}_factor_backward", |
| 0 | 110 | | factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 111 | | attributes.AddOrReplace($"_{profileName}_speed_forward", |
| 0 | 112 | | factor.Value.ForwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 113 | | attributes.AddOrReplace($"_{profileName}_speed_backward", |
| 0 | 114 | | factor.Value.BackwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture)); |
| 0 | 115 | | } |
| | 116 | |
|
| 0 | 117 | | var isIsland = islandDetails.size < routingNetwork.IslandManager.MaxIslandSize; |
| 0 | 118 | | attributes.AddOrReplace("_island_details_label", islandDetails.label.ToString(System.Globalization.CultureInfo.I |
| 0 | 119 | | attributes.AddOrReplace("_island_details_size", islandDetails.size.ToString(System.Globalization.CultureInfo.Inv |
| 0 | 120 | | attributes.AddOrReplace("_island_details_final", islandDetails.final.ToString(System.Globalization.CultureInfo.I |
| 0 | 121 | | attributes.AddOrReplace("_island_is_island", isIsland.ToString(System.Globalization.CultureInfo.InvariantCulture |
| | 122 | |
|
| 0 | 123 | | jsonWriter.WriteProperties(attributes); |
| 0 | 124 | | jsonWriter.WritePropertyName("geometry"); |
| 0 | 125 | | jsonWriter.WriteLineString(enumerator.GetCompleteShape()); |
| 0 | 126 | | jsonWriter.WriteFeatureEnd(); |
| 0 | 127 | | } |
| | 128 | | } |