< Summary

Class:Itinero.Network.Search.Islands.IslandLabelsExtensions
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Search/Islands/IslandLabelsExtensions.cs
Covered lines:15
Uncovered lines:70
Coverable lines:85
Total lines:128
Line coverage:17.6% (15 of 85)
Covered branches:7
Total branches:30
Branch coverage:23.3% (7 of 30)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
GetOrCreateLabel(...)87.5%883.33%
ToGeoJson()0%60%
WriteEdgeFeatureWithIslandDetails(...)0%160%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Search/Islands/IslandLabelsExtensions.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Linq;
 4using System.Text;
 5using System.Text.Json;
 6using System.Threading.Tasks;
 7using Itinero.IO.Json.GeoJson;
 8using Itinero.Network.Attributes;
 9using Itinero.Network.Enumerators.Edges;
 10
 11namespace Itinero.Network.Search.Islands;
 12
 13internal static class IslandLabelsExtensions
 14{
 15    internal static (uint label, uint size, bool final) GetOrCreateLabel(this IslandLabels labels, RoutingNetworkEdgeEnu
 16        Func<IEdgeEnumerator, bool?>? isOnIslandAlready = null)
 2317    {
 3318        if (labels.TryGetWithDetails(edgeEnumerator.EdgeId, out var neighbourLabelDetails)) return neighbourLabelDetails
 19
 20        // check if the neighbour has a status already we can use.
 1321        var onIslandAlready = isOnIslandAlready?.Invoke(edgeEnumerator);
 1322        if (onIslandAlready != null)
 123        {
 24            // check and verify status
 125            if (onIslandAlready.Value)
 026            {
 27                // neighbour has a known status and is on an island.
 028                neighbourLabelDetails = labels.AddNew(edgeEnumerator.EdgeId, true);
 029            }
 30            else
 131            {
 32                // neighbour has a known status and is not on an island.
 133                neighbourLabelDetails = labels.AddTo(IslandLabels.NotAnIslandLabel, edgeEnumerator.EdgeId);
 134            }
 135        }
 36        else
 1237        {
 38            // no label was found, assign a new one.
 1239            neighbourLabelDetails = labels.AddNew(edgeEnumerator.EdgeId);
 1240        }
 41
 1342        return neighbourLabelDetails;
 2343    }
 44
 45    public static async Task<string> ToGeoJson(this IslandLabels islandLabels, RoutingNetwork network)
 046    {
 047        using var stream = new MemoryStream();
 048        using (var jsonWriter = new Utf8JsonWriter(stream))
 049        {
 050            jsonWriter.WriteFeatureCollectionStart();
 51
 052            var edgeEnumerator = network.GetEdgeEnumerator();
 053            foreach (var (edge, label) in islandLabels)
 054            {
 055                if (!edgeEnumerator.MoveTo(edge)) continue;
 056                if (!islandLabels.TryGetWithDetails(edge, out var edgeDetails)) continue;
 57
 058                jsonWriter.WriteEdgeFeatureWithIslandDetails(network, edgeEnumerator, edgeDetails);
 059            }
 60
 061            jsonWriter.WriteFeatureCollectionEnd();
 062        }
 63
 064        return Encoding.UTF8.GetString(stream.ToArray());
 065    }
 66
 67    internal static void WriteEdgeFeatureWithIslandDetails(this Utf8JsonWriter jsonWriter,
 68        RoutingNetwork routingNetwork, RoutingNetworkEdgeEnumerator enumerator, (uint label, uint size, bool final) isla
 069    {
 070        jsonWriter.WriteFeatureStart();
 071        var attributes = enumerator.Attributes.ToList();
 072        if (enumerator.Forward)
 073        {
 074            attributes.AddRange([
 075                ("_tail_tile_id", enumerator.Tail.TileId.ToString()),
 076                ("_tail_local_id", enumerator.Tail.LocalId.ToString()),
 077                ("_head_tile_id", enumerator.Head.TileId.ToString()),
 078                ("_head_local_id", enumerator.Head.LocalId.ToString()),
 079                ("_edge_id", enumerator.EdgeId.ToString())
 080            ]);
 081        }
 82        else
 083        {
 084            attributes.AddRange([
 085                ("_head_tile_id", enumerator.Tail.TileId.ToString()),
 086                ("_head_local_id", enumerator.Tail.LocalId.ToString()),
 087                ("_tail_tile_id", enumerator.Head.TileId.ToString()),
 088                ("_tail_local_id", enumerator.Head.LocalId.ToString()),
 089                ("_edge_id", enumerator.EdgeId.ToString())
 090            ]);
 091        }
 92
 093        if (enumerator.TailOrder.HasValue) attributes.AddOrReplace("_tail_order", enumerator.TailOrder.Value.ToString())
 094        if (enumerator.HeadOrder.HasValue) attributes.AddOrReplace("_head_order", enumerator.HeadOrder.Value.ToString())
 95
 096        foreach (var profileName in routingNetwork.RouterDb.ProfileConfiguration.GetProfileNames())
 097        {
 098            if (!routingNetwork.RouterDb.ProfileConfiguration.TryGetProfileHandlerEdgeTypesCache(profileName, out var ed
 099                out _)) continue;
 100
 0101            if (edgeFactorCache == null) continue;
 0102            if (!enumerator.EdgeTypeId.HasValue) continue;
 103
 0104            var factor = edgeFactorCache.Get(enumerator.EdgeTypeId.Value);
 0105            if (factor == null) continue;
 106
 0107            attributes.AddOrReplace($"_{profileName}_factor_forward",
 0108                factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture));
 0109            attributes.AddOrReplace($"_{profileName}_factor_backward",
 0110                factor.Value.ForwardFactor.ToString(System.Globalization.CultureInfo.InvariantCulture));
 0111            attributes.AddOrReplace($"_{profileName}_speed_forward",
 0112                factor.Value.ForwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture));
 0113            attributes.AddOrReplace($"_{profileName}_speed_backward",
 0114                factor.Value.BackwardSpeedMeterPerSecond.ToString(System.Globalization.CultureInfo.InvariantCulture));
 0115        }
 116
 0117        var isIsland = islandDetails.size < routingNetwork.IslandManager.MaxIslandSize;
 0118        attributes.AddOrReplace("_island_details_label", islandDetails.label.ToString(System.Globalization.CultureInfo.I
 0119        attributes.AddOrReplace("_island_details_size", islandDetails.size.ToString(System.Globalization.CultureInfo.Inv
 0120        attributes.AddOrReplace("_island_details_final", islandDetails.final.ToString(System.Globalization.CultureInfo.I
 0121        attributes.AddOrReplace("_island_is_island", isIsland.ToString(System.Globalization.CultureInfo.InvariantCulture
 122
 0123        jsonWriter.WriteProperties(attributes);
 0124        jsonWriter.WritePropertyName("geometry");
 0125        jsonWriter.WriteLineString(enumerator.GetCompleteShape());
 0126        jsonWriter.WriteFeatureEnd();
 0127    }
 128}